flask保存cookie中的民意调查

时间:2016-11-22 17:08:37

标签: python session cookies flask

所以我有这个简单的轮询应用程序,不断创建人们可以投票+1或-1的民意调查。但是,由于该网站不需要用户登录,因此人们可以在每次投票时多次投票。

<form name="poll" id='{{ item.id }}' method="post" action='/poll'>
    <label class='lab-pos'>
      <input type="radio" name="points" id='whine-pos' value=1>
      <img class='img-pos'src="/static/item-pos.png">
    </label>
    <label class='lab-neg'>
      <input type="radio" name="points" id='whine-neg' value=-1>
      <img class='img-neg'src="/static/item-neg.png">
    </label>
</form>

我将带有javascript函数的提交发送到我的sqlite3数据库,没有提交按钮而是脚本。

<script type="text/javascript">
  $(document).ready(function() {
    $('input[type=radio]').on('change', function() {
        $(this).closest("form").submit();
    });
});
</script>

是否可以将投票保存在带有烧瓶的cookie中,这样当一个人再次访问该网站时,他们将无法再次投票但只能更改投票? (如果他们想要的话)。我知道他们可以清除饼干,他们可以再次投票,但这并不会让我在这个阶段感到困扰。

我在SQLAlchemy中的数据库结构目前看起来像这样,我的视图如下所示

class pollingresult(db.Model):

    __tablename__ = "pollingresult"

    id = db.Column('id', db.Integer, primary_key=True)
    poll = db.Column('poll', db.Integer)
    cookie = db.Column('cookie', db.String(255))
    feed_id = db.Column('feed_id', db.Integer)

    def __init__(self, poll):
        self.poll = poll

和我在烧瓶中的观点如下

@app.route('/poll', methods=['GET', 'POST'])
def poll():
    polltodb = pollingresult(request.form['points'])
    session['points_session'] = request.form['points']
    db.session.add(polltodb)
    db.session.commit()

    return ('',204)

我已经玩过会议,但似乎在刷新时,民意调查仍然“休息”,以便人们可以再次投票。

编辑161202:

所以,我仍然在努力完成这项任务,我可以将会话['points_session']保存到会话中,但是我需要将会话保存为更像dict,其中dict具有id = item.id和points =点所以我可以用javascript'预填充表格'if id = 1和point = 1'预填充表格,id = 1.我还需要阻止表格根据会话再次提交,所以我想我将不得不为某种会话密钥创建一个有点虚拟的令牌?

编辑161207:

所以我想发送poll_id以及表单提交所以我认为我可以使用ajax post请求,但是,这会引发错误“无法解码JSON对象:无法解码JSON对象”。

<script type="text/javascript">
  $(document).ready(function() {
    $('input[type=radio]').on('change', function() {
        $(this).closest("form").submit();
        var poll_id = $(this).closest("div").attr("id");
        var data = {poll_id};
        console.log(JSON.stringify(data));
        $.post('/poll', {
            data: JSON.stringify(data),
          }, function(data) {
              console.log(data);
            });
      });
});
</script>

与新的民意调查路线一起:

@app.route('/poll', methods=['GET', 'POST'])
def poll():
    polltodb = pollingresult(request.form['points'])
    session['points_session'] = request.form['points']
    db.session.add(polltodb)
    db.session.commit()
    data = request.get_json(force=True)
    print data

    return ('',204)

稍后将与某种会话密钥一起插入数据库。

2 个答案:

答案 0 :(得分:4)

不是保存用户在会话中投票的民意调查,而只需附加&#34; poll_user_id&#34;到会话,以便您可以在数据库中跟踪用户及其投票。

from uuid import uuid1 as uuid
if "poll_user_id" not in session:
    session["poll_user_id"] = uuid()

这里有一些伪代码,因为我不熟悉Flask及其数据库引擎。

old_vote = query(poll_user_id=session["poll_user_id"], poll=poll_id)
if not old_vote:
    insert(poll_user_id=session["poll_user_id"], poll=poll_id, choice=form.choice)
else:
    update(poll_user_id=session["poll_user_id"], poll=poll_id, choice=form.choice)

现在,当用户投票时,无论是新的还是作为更新,它都会检查投票是否已经存在,并使用相同的&#34; poll_user_id&#34;价值,如果是的话,你会做更新。如果它没有插入。

答案 1 :(得分:2)

我建议你根本不使用cookies。我使用浏览器指纹识别来识别用户。优点是,即使他们一次又一次地隐身地打开页面,你也可以识别他们(这会清除你所有的cookie /会话)

https://clientjs.org/#Fingerprints

您最好生成(通常是半独特的)指纹并以这种方式跟踪重复用户。 我一直在使用它取得了很好的成功,我有一个链接,用户可以标记他没有完成投票/行动,我没有