예제 #1
0
 def test_votes(self):
     u = User('user', 'applez1')
     c = Community('Powerlifting', None, None, None, None)
     post = Posts('This is a Title', 'This is a body', author=u, community=c)
     comment1 = Comments("This is a comment", author=u, post=post)
     v = Vote(u)
     self.assertEqual(u, v.user)
     v = Vote(u, post=post)
     self.assertEqual(v.post, post)
     v = Vote(u, comment=comment1)
     self.assertEqual(v.comment, comment1)
     v.vote(u, post, 1, post)
     self.assertEqual(post.value, 1)
예제 #2
0
파일: app.py 프로젝트: spncrlkt/emoji-be
def vote(definition_id):
    body = request.get_json()

    try:
        definition = db.session.query(Definition).filter_by(id=definition_id).one()
    except NoResultFound as ex:
        return jsonify({'error': 'Definition does not exist'})

    try:
        user = db.session.query(User).filter_by(twitter_id=body.get('userId')).one()
    except NoResultFound as ex:
        return jsonify({'error': 'Auth Error'})

    posted_auth_token = body.get('authToken')
    token = encode_value(
        user.oauth_token,
        app.config.get('AUTH_SALT')
    )
    if token != posted_auth_token:
        return jsonify({'error': 'Auth Error'})

    try:
        vote = db.session.query(Vote).filter_by(definition_id=definition.id).\
                filter_by(user_id=user.id).one()
    except NoResultFound as ex:
        vote = Vote()
        vote.definition_id = definition.id
        vote.user_id = user.id

    vote.vote = 1 if body.get('isUpvote') else -1
    db.session.add(vote)
    db.session.commit()

    return jsonify({'vote': { 'id': vote.id}})
예제 #3
0
파일: views.py 프로젝트: amyrlam/earthmd
def vote():
	print "We're here!"
	if request.form.get("vote") == "up": # expression == returns True, if != returns False
		vote_input = 1 # upvote
	else:
		vote_input = -1 # downvote
	post_id = request.form.get("post_id")
	
	vote = Vote.query.filter_by(post_id=post_id, user_id=g.user.id).first()
	
	if vote is None:
		vote = Vote(post_id=post_id, user_id=g.user.id, vote=vote_input) 
		db.session.add(vote)
		db.session.commit()
		db.session.refresh(vote)
	else:
		vote.vote = vote_input
		db.session.commit()

	vote.post.score += vote_input
	# TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'

	db.session.commit()

	# if vote, Post(score += 1)
	# manual in sql: UPDATE post SET score = (SELECT SUM(vote) FROM vote WHERE post_id = Post.id);

	# change upvote to green if successful?

	# if user upvotes a post, can change to downvote/cancel how?
	# see unique constraint in models.Vote > no that didn't work, see \d vote UNIQUE in psql

	return "Vote recorded!"
예제 #4
0
파일: test_item.py 프로젝트: wgilpin/shout
 def load_votes(self, user, vote_score, vote_comment):
     vote = Vote()
     vote.item = self.item
     vote.vote = vote_score
     vote.comment = vote_comment
     vote.voter = user.key.id()
     vote.put()
예제 #5
0
def update_votes():
    '''
    Updates the Vote table with the json retrieved from Kimono.
    If a vote is not in the table it creates one.
    Returns the number of new votes and new orphan players added to the db
    '''
    logger.info('Updating votes...')
    url = settings.KIMONO['votes_url']
    votes = _get_results_collection1(url)
    # Keeping a list of players with votes but not present in the Player table
    # so that they could be added later
    logger.info(' - Updating database...')
    no_new_votes = 0
    no_new_orphans = 0
    for vote in votes:
        p_id = _id_from_url(vote['name']['href'])
        v_day = _day_from_url(vote['url'])
        # Checking if the vote already exists. If not, creates a new one, if it
        # exists it will get the current vote and update it
        try:
            v = Vote.objects.get(player__pk=p_id, day=v_day)
        except Vote.DoesNotExist:
            v = Vote()
            no_new_votes += 1
        # Creating a orphan player if there is not a player for this vote
        try:
            p = Player.objects.get(pk=p_id)
        except Player.DoesNotExist:
            p = Player(pk=p_id)
            p.role = _fix_role(vote['role'])
            p.save()
            no_new_orphans += 1
        v.player = p
        v.vote = _fix_zero(vote['vote'])
        v.gol = _fix_zero(vote['gol'])
        v.assist = _fix_zero(vote['assists'])
        v.penalties_scored_saved = _fix_zero(vote['penalties_scored_saved'])
        v.penalties_missed = _fix_zero(vote['penalties_missed'])
        v.own_gol = _fix_zero(vote['own_gol'])
        v.yellow_cards = _fix_zero(vote['yellow_cards'])
        v.red_cards = _fix_zero(vote['red_cards'])
        v.magicvote = _fix_zero(vote['own_gol'])
        v.day = v_day
        v.sub_in = _sub_in(vote['in']['class'])
        v.sub_out = _sub_out(vote['out']['class'])
        # Storing on the db
        v.save()
    return no_new_votes, no_new_orphans
예제 #6
0
def update_votes():
    '''
    Updates the Vote table with the json retrieved from Kimono.
    If a vote is not in the table it creates one.
    Returns the number of new votes and new orphan players added to the db
    '''
    logger.info('Updating votes...')
    url = settings.KIMONO['votes_url']
    votes = _get_results_collection1(url)
    # Keeping a list of players with votes but not present in the Player table
    # so that they could be added later
    logger.info(' - Updating database...')
    no_new_votes = 0
    no_new_orphans = 0
    for vote in votes:
        p_id = _id_from_url(vote['name']['href'])
        v_day = _day_from_url(vote['url'])
        # Checking if the vote already exists. If not, creates a new one, if it
        # exists it will get the current vote and update it
        try:
            v = Vote.objects.get(player__pk=p_id, day=v_day)
        except Vote.DoesNotExist:
            v = Vote()
            no_new_votes += 1
        # Creating a orphan player if there is not a player for this vote
        try:
            p = Player.objects.get(pk=p_id)
        except Player.DoesNotExist:
            p = Player(pk=p_id)
            p.role = _fix_role(vote['role'])
            p.save()
            no_new_orphans += 1
        v.player = p
        v.vote = _fix_zero(vote['vote'])
        v.gol = _fix_zero(vote['gol'])
        v.assist = _fix_zero(vote['assists'])
        v.penalties_scored_saved = _fix_zero(vote['penalties_scored_saved'])
        v.penalties_missed = _fix_zero(vote['penalties_missed'])
        v.own_gol = _fix_zero(vote['own_gol'])
        v.yellow_cards = _fix_zero(vote['yellow_cards'])
        v.red_cards = _fix_zero(vote['red_cards'])
        v.magicvote = _fix_zero(vote['own_gol'])
        v.day = v_day
        v.sub_in = _sub_in(vote['in']['class'])
        v.sub_out = _sub_out(vote['out']['class'])
        # Storing on the db
        v.save()
    return no_new_votes, no_new_orphans
예제 #7
0
def handle_vote(party_id, resturaunt_id):
    form = VoteForm()
    if form.validate_on_submit():
        yay_or_nay = form.yay_or_nay.data
        vote = Vote.vote(member=g.user,
                         party_id=party_id,
                         resturaunt_id=resturaunt_id,
                         yay=yay_or_nay)
        if vote:
            if vote.party.done_voting():
                return redirect(f'/done_voting/{party_id}')
            return redirect(f'/vote/{party_id}')
        else:
            flash("""You're done voting for now, refresh this page
                         to see when the resturaunt is chosen""",
                  category='success')
            return redirect(f'/parties/{party_id}')
    return redirect(f'/vote/{party_id}')
예제 #8
0
파일: views.py 프로젝트: shaond/whichone
def add_vote(request, uuid, polluuid, choice):
    if request.method == 'GET':
        try:
            vote = Vote.objects.get(voter=uuid, poll=polluuid)
            return HttpResponse(json.dumps('fail: vote exists'),
                        mimetype='application/json')
        except Vote.DoesNotExist:
            vote = Vote()
            _poll = Poll.objects.get(uuid=polluuid)
            vote.voter = User.objects.get(uuid=uuid)
            vote.poll = _poll
            vote.vote = choice
            vote.save()
            _poll.votes = _poll.votes + 1
            _poll.save()
            return HttpResponse(json.dumps('ok'),
                        mimetype='application/json')
    else:
        raise Http404
예제 #9
0
파일: migrate.py 프로젝트: wgilpin/shout
 def at_least_one_vote_per_item(self):
   # make sure each item has 1 vote
   items = Item.query()
   for it in items:
     vote = it.votes.filter("voter =", it.owner).get()
     if not vote:
       vote = Vote()
       vote.item = it
       vote.vote = VoteValue.VOTE_LIKED
       vote.voter = it.owner
       vote.comment = "blah"
       it.upVotes = 1
       vote.put()
       it.save()
     if it.votesUp == it.votesDown == 0:
       if vote.vote == VoteValue.VOTE_LIKED:
         it.votesUp = 1
       elif vote.vote == VoteValue.VOTE_DISLIKED:
         it.votesDown = -1
       it.save()
예제 #10
0
def vote(request):
    if request.session['score']==0:
        return HR(u'需要登录后才可以投票!')
    else:
        did=request.POST['did']
        score=request.POST['score']
         #查询是否已投票此(did)discussid
        if Vote.objects.filter(user_id=request.session['uid'],discuss_id=did).count()==1:
            return HR(u'亲,只能投票一次噢!')

        d=Discuss.objects.get(id=did)

        if d.user_id==request.session['uid']:
            return HR(u'亲,不可以为自己投票的哦!')

        #更新vote表
        v=Vote()
        v.user_id=request.session['uid']
        v.discuss_id=did
        v.vote =score
        v.save()
        #更新用户表,增加score
        u=User.objects.get(id=d.user_id)

        if score=='1':
            d.support +=1
            u.score+=1
        else:
            d.oppose +=1
            u.score-=1
            if u.score <1:
                u.score=1
        d.save()
        u.save()
        
        return HR(u'success')
예제 #11
0
db.drop_all()
db.create_all()

me = User.create(email='*****@*****.**', name='david', password='******')
kynsi = User.create(email='*****@*****.**', name='kynsi', password='******')
chris = User.create(email='*****@*****.**', name='chris', password='******')
tori = User.create(email='*****@*****.**', name='tori', password='******')

valentine = Party.create(address="12248 Hunter's Knoll Dr",
                         city="Burleson",
                         state="Texas",
                         zip_code=76028,
                         leader_id=me.id,
                         name="Valentine's day dinner")

Resturaunt.get_resturaunts(party_id=valentine.id)

valentine.add_member(kynsi.id)

valentine.add_member(chris.id)

valentine.add_member(tori.id)

for member in valentine.members:
    for resturaunt in valentine.resturaunts:
        vote = True if random.randint(0, 1) else False
        new_vote = Vote.vote(member=member,
                             party_id=valentine.id,
                             resturaunt_id=resturaunt.id,
                             yay=vote)