예제 #1
0
def update_edit_proposal(law_id, proposal_id):
    if 'user' not in session:
        flash('you need to be logged in')
        return redirect(url_for('users.login'))

    logged_user = Users.query.filter_by(id=session['user']).first()
    if not is_moderator(logged_user):
        abort(403)

    proposal = Proposal.query.get_or_404(proposal_id)
    if not proposal.posted_at.date_posted >= ServerState.get_state(
    )['discussion-start']:
        flash("Cannot update edit-proposal when law isn't under discussion",
              'info')
        return redirect(url_for('laws.law', _id_=proposal.posted_at.id))

    update_edit_proposal_form = UpdateEditProposal()

    if request.method == 'POST':
        proposal.title = update_edit_proposal_form.title.data
        proposal.explanation = update_edit_proposal_form.explanation.data
        proposal.info = update_edit_proposal_form.info.data
        db.session.commit()
        return redirect(
            url_for('proposals.edit_proposal',
                    law_id=law_id,
                    proposal_id=proposal_id))
    else:
        update_edit_proposal_form.info.data = proposal.info
        update_edit_proposal_form.title.data = proposal.title
        update_edit_proposal_form.explanation.data = proposal.explanation
        return render_template(
            'update_edit_proposal.html',
            update_edit_proposal_form=update_edit_proposal_form)
예제 #2
0
def vote():
    """
    up-vote a proposal displayed in a law page.
    :return:
    """
    if 'user' not in session:
        flash("need to be logged in", 'info')
        return url_for('users.login')

    proposal_id = request.get_json()['proposal_id']

    proposal = Proposal.query.filter_by(id=proposal_id).first()
    if proposal.posted_at.date_posted < ServerState.get_state()['archive-end']:
        flash("Cannot vote on Proposal when it's archived", 'info')
        return ""

    user = Users.query.filter_by(id=session['user']).first()

    if user in proposal.voters:
        proposal.voters.remove(user)
        proposal.up_votes -= 1
        db.session.commit()
        return ""
    else:
        proposal.up_votes += 1
        proposal.voters.append(user)
        db.session.commit()
        return ""
예제 #3
0
def archive(week_duel_num):
    """
    :param week_duel_num: it is a string so that we can interpret negative numbers.
        - if '-1': returns the latest week-duel archive
        - else: return the week-duel number `week_duel_num`
    :return:
        - 404: week_duel_num is not reached yet.
        - redirect to vote: if week_duel_num is the one that's being voted.
    """
    server_state = ServerState.get_state()

    if week_duel_num == "-1":
        week_duel_num = server_state['week-duel'] - 2
        if week_duel_num <= 0:
            return abort(404)
        else:
            return redirect(
                url_for('main.archive', week_duel_num=week_duel_num))
    else:
        week_duel_num = int(week_duel_num)

    if week_duel_num == server_state['week-duel'] - 1:
        return redirect(url_for('laws.vote_laws'))
    elif week_duel_num == server_state['week-duel']:
        return redirect(url_for('main.discussion'))
    elif week_duel_num > server_state['week-duel']:
        return abort(404)

    # pagination
    has_next = week_duel_num < server_state['week-duel'] - 2
    has_prev = week_duel_num > 1

    start_date, end_date = ServerState.archive_date(week_duel_num)
    print('\nfrom ', start_date.strftime('%Y-%m-%d %H:%M'), '\t to',
          end_date.strftime('%Y-%m-%d %H:%M'))
    laws_found = Law.query.filter(Law.date_posted >= start_date).filter(
        Law.date_posted <= end_date).all()

    # if laws_found.__len__() != 6:
    #     abort(500)

    return render_template('law_week.html',
                           laws=laws_found,
                           week_duel=week_duel_num,
                           has_next=has_next,
                           has_prev=has_prev)
예제 #4
0
def discussion():
    """
    :return: number of laws being proposed and moved to discussion.
    """
    server_state = ServerState.get_state()
    laws = Law.query.filter(Law.date_posted > server_state['discussion-start'])\
        .filter(Law.date_posted <= server_state['discussion-end']).all()
    return render_template('discussion.html', laws=laws)
예제 #5
0
def index():
    """
    retrieves the main page. It also displays the latest laws that finished the voting process.
    """
    server_state = ServerState.get_state()
    laws_found = Law.query.filter(Law.date_posted >= server_state['last-voted-start'])\
        .filter(Law.date_posted <= server_state['last-voted-end']).all()
    return render_template('index.html', laws=laws_found)
예제 #6
0
def increment_week_duel():
    """
    JSON data needed:
        - password: hash of SECRET_KEY environmental variable.
        - week_duel: this variable is used to make sure sender is not out-of-sync
    """
    data = request.get_json()
    try:
        password = data['password']
        week_duel = data['week-duel']
    except KeyError:
        return abort(404)

    if password == sk_hash and week_duel == ServerState.get_state(
    )['week-duel']:
        print('\n++ incrementing week duel ++\n')
        ServerState.increment_week_duel()
        db.session.commit()
        return ""
    else:
        return abort(404)
예제 #7
0
def get_data():
    """
    JSON data needed:
        - password: hash of SECRET_KEY environmental variable.
        - encoding: the way `datetime` object must be encoded to a string.
    """
    data = request.get_json()
    try:
        encoding = data['encoding']
        password = data['password']
    except KeyError:
        return abort(403)

    state = ServerState.get_state()
    if password == sk_hash:
        return state['launch-date'].strftime(encoding) + '\r' + \
               str(state['week-duel']) + '\r' + str(state['base-date'].strftime(encoding))

    else:
        return abort(302)
예제 #8
0
def edit_proposal(law_id, proposal_id):
    proposal = Proposal.query.filter_by(id=proposal_id).first()
    comment_form = CommentForm()
    if 'user' in session:
        logged_user = Users.query.filter_by(id=session['user']).first()
    else:
        logged_user = None

    if request.method == "POST":
        if 'user' not in session:
            redirect(url_for('users.login'))

        if proposal.posted_at.date_posted < ServerState.get_state(
        )['archive-end']:
            flash('Cannot write Comment when Proposal is archived', 'info')
            return redirect(
                url_for('proposals.edit_proposal',
                        law_id=law_id,
                        proposal_id=proposal_id))

        if comment_form.validate_on_submit():
            content = comment_form.comment.data

            if content[0] == '@':

                parent_id, content = content.split(' ', 1)
                parent_id = parent_id[1:]
                parent_comment = Comment.query.filter_by(id=parent_id).first()
                if parent_comment is not None:
                    if parent_comment.posted_at == proposal:
                        comment = Comment(content, proposal, logged_user,
                                          parent_comment)
                        db.session.add(comment)
                        db.session.commit()
                    else:
                        return "500"
                else:
                    return "500"
            else:
                logged_user = Users.query.filter_by(id=session['user']).first()
                comment = Comment(content, proposal, logged_user)
                db.session.add(comment)
                db.session.commit()

            return redirect(
                url_for('proposals.edit_proposal',
                        law_id=law_id,
                        proposal_id=proposal_id))
        else:
            return "400"
    else:
        # TODO: Add ajax GET requests to retrieve more replies instead of showing all replies for a comment
        comment_page = request.args.get('comment_page', 1, type=int)
        comments_paginate = Comment.query.filter_by(proposal_id=proposal.id)\
            .filter_by(parent_id=0).order_by(Comment.date_posted.desc()).paginate(page=comment_page, per_page=20)
        # parent_id set to 0 to exclude replies
        return render_template('edit_proposal.html',
                               edit_proposal=proposal,
                               comment_form=comment_form,
                               comments_paginate=comments_paginate,
                               meta_data=proposal.__meta_data__(
                                   request.endpoint))
예제 #9
0
def discussion_arabic():
    server_state = ServerState.get_state()
    laws = Law.query.filter(Law.date_posted > server_state['discussion-start'])\
        .filter(Law.date_posted <= server_state['discussion-end'])\
        .filter(Law.info_arabic.isnot(None)).all()
    return render_template('ar/discussion.html', laws=laws)
예제 #10
0
def inject_server_data():
    return ServerState.get_state()