def setUp(self): basedir = os.path.abspath(os.path.dirname(__file__)) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join( basedir, "test.db") self.app = app.test_client() # create virtual test env db.create_all() s1 = User(username="******", email="*****@*****.**") s1.set_password("Hello") s2 = User(username="******", email="*****@*****.**") s2.set_password("World") poll1 = Poll(title="My First Poll", user_id=1, expiry_date=datetime.now()) poll2 = Poll(title="Hello World", user_id=2, expiry_date=datetime.now() + timedelta(days=30)) resp1 = Responses(value=datetime.now(), poll_id=1) vote1 = Votes(response_id=1, user_id=1, poll_id=1) vote2 = Votes(response_id=1, user_id=2, poll_id=1) db.session.add(s1) db.session.add(s2) db.session.add(poll1) db.session.add(resp1) db.session.add(vote1) db.session.add(vote2) db.session.add(poll2) db.session.commit()
def get(self, restaurant_id): restaurant = Restaurant.get_restaurant(restaurant_id) if restaurant: Votes.increment(restaurant, self.today) self.voter.broadcast_votes(self.today) return self.ok()
def upvote(id, vote_type): """ View function that adds one to the vote_number column in the votes table """ # Query for user votes = Votes.query.filter_by(user_id=current_user.id).all() to_str = f'{vote_type}:{current_user.id}:{id}' if not votes: new_vote = Votes(vote=vote_type, user_id=current_user.id, posts_id=id) new_vote.save_vote() flash('YOU HAVE VOTED', 'success') for vote in votes: if f'{vote}' == to_str: break else: new_vote = Votes(vote=vote_type, user_id=current_user.id, posts_id=id) new_vote.save_vote() break return redirect(url_for('.view_post', id=id))
def poll(id): poll = Poll.query.filter_by(id = id).first_or_404() options = poll.poll_options options_list = {} for option in options: options_list[str(option.id)] = str(option.value) option_limit = poll.option_limit form = generate_poll_form(options_list) if(form.validate_on_submit()): voted_options = form.get_responses() if(not can_vote(current_user, poll)): flash("You have already voted you sneaky devil", category = "error") return(render_template("poll-page.html", poll = poll, form = form, title = poll.title)) if(valid_vote(voted_options, option_limit)): for key in voted_options.keys(): if(voted_options[key]): vote = Votes(response_id = key, user_id = current_user.id, poll_id = id) db.session.add(vote) db.session.commit() flash("Vote counted!", category = "info") return(redirect(url_for("results", id = poll.id))) return(render_template("poll-page.html", poll = poll, form = form, title = poll.title))
def vote_movie(): if not request.json or not 'movie' in request.json or not 'action' in request.json: return u'No movie title or action defined', 400 action = request.json['action'] movie_name = request.json['movie'] movie = Movies.query.filter_by(movie=movie_name).first() if not movie: return u'Could not find movie: '+movie_name username = auth.username() user = User.query.filter_by(username=username).first() if user.points > 0 and action == "up": user.remove_point() user.last_vote = movie.movie movie.votes = movie.votes + 1 new_vote = Votes(username=user.username, movie=movie.movie, category='current') db.session.add(new_vote) db.session.commit() elif action == 'down' and movie.votes != 0: user.points = user.points + 1 user.last_vote = None movie.votes = movie.votes - 1 existing_vote = db.session.query(Votes).filter_by(movie=movie.movie) existing_vote = existing_vote.filter_by(username=user.username).first() db.session.delete(existing_vote) db.session.commit() else: return u'Not enough points', 400 return jsonify({'Movie ID':movie.id,'Movie Name':movie.movie,'Updated Movie Votes':movie.votes,'Updated User Points':user.points})
def vote(): movie_name = request.form['movie_name'] vote_point = request.form['vote_point'] current_user = request.form['current_user'] movie = Movies.query.filter_by(movie=movie_name).first() user = User.query.filter_by(username=current_user).first() if user.points > 0 or vote_point == 'minus': if vote_point == 'plus': user.remove_point() user.last_vote = movie_name movie.votes = movie.votes + 1 new_vote = Votes(username=current_user, movie=movie_name, category='current') db.session.add(new_vote) elif vote_point == 'minus' and movie.votes != 0: user.points = user.points + 1 user.last_vote = None movie.votes = movie.votes - 1 existing_vote = db.session.query(Votes).filter_by(movie=movie_name) existing_vote = existing_vote.filter_by( username=current_user).first() db.session.delete(existing_vote) db.session.commit() else: return u'Not enough points', 400 return jsonify({'votes': movie.votes, 'points': user.points})
def score_recived(data): # handle the sent if card_info.wait_card is False: score = data['score'] if score != '': print('score pressed') # Card.next_card() current_card_id = Card.query.filter_by(current_selected=True).first().id current_user_id = User.query.filter_by(username=current_user.username).first().id tracker_obj = Votes.query.filter((and_(Votes.card_id == current_card_id, Votes.user_id == current_user_id))).first() if tracker_obj: print('updated: ' +str(current_user_id) + ':' + str(current_card_id)) tracker_obj.vote_score = score else: print('added: ' +str(current_user_id) + ':' + str(current_card_id)) db.session.add(Votes(card_id=current_card_id, user_id=current_user_id, vote_score=score)) db.session.commit() emit('vote_bar_message', {'button_disabled': True, 'current_votes': '', 'last_vote': ''})