コード例 #1
0
ファイル: project.py プロジェクト: sidharthkuruvila/hacknight
def project_view(profile, event, project):
    if not profile:
        abort(404)
    if not event:
        abort(404)
    if not project:
        abort(404)

    userIsMember = False
    if g.user:
        user = User.query.filter_by(userid=g.user.userid).first()
        if user:
            participant = Participant.query.filter_by(user_id=user.id, event_id=event.id).first()
        if participant:
            project_member = ProjectMember.query.filter_by(project_id=project.id, participant_id=participant.id).first()
            if project_member:
                userIsMember = True
    # Fix the join query below and replace the cascaded if conditions.
    # if g.user:
    #   query = (ProjectMember
    #       .query.filter(ProjectMember.project_id == project.id)
    #       .join(Participant).filter(Participant.event_id == event.id)
    #       .join(User).filter(User.userid == g.user.userid))
    #   print "user id = %s, event id = %s, project id = %s" % (
    #       g.user.userid, event.id, project.id)
    #   print query.statement
    #   project_member = query.first()
    #   print project_member.project.name

    comments = sorted(Comment.query.filter_by(commentspace=project.comments, reply_to=None).order_by('created_at').all(),
        key=lambda c: c.votes.count, reverse=True)
    commentform = CommentForm()
    delcommentform = DeleteCommentForm()
    commentspace = project.comments
    if request.method == 'POST':
        if request.form.get('form.id') == 'newcomment' and commentform.validate():
            if commentform.edit_id.data:
                comment = commentspace.get_comment(int(commentform.edit_id.data))
                if comment:
                    if comment.user == g.user:
                        comment.message = commentform.message.data
                        comment.message_html = markdown(comment.message)
                        comment.edited_at = datetime.utcnow()
                        flash("Your comment has been edited", "info")
                    else:
                        flash("You can only edit your own comments", "info")
                else:
                    flash("No such comment", "error")
            else:
                comment = Comment(user=g.user, commentspace=project.comments, message=commentform.message.data)
                if commentform.reply_to_id.data:
                    reply_to = commentspace.get_comment(int(commentform.reply_to_id.data))
                    if reply_to and reply_to.commentspace == project.comments:
                        comment.reply_to = reply_to
                comment.message_html = markdown(comment.message)
                project.comments.count += 1
                comment.votes.vote(g.user)  # Vote for your own comment
                comment.make_id()
                db.session.add(comment)
                flash("Your comment has been posted", "info")
            db.session.commit()
            # Redirect despite this being the same page because HTTP 303 is required to not break
            # the browser Back button
            return redirect(url_for('project_view', profile=profile.name, event=event.name, project=project.url_name))

        elif request.form.get('form.id') == 'delcomment' and delcommentform.validate():
            comment = commentspace.get_comment(int(delcommentform.comment_id.data))
            if comment:
                if comment.user == g.user:
                    comment.delete()
                    project.comments.count -= 1
                    db.session.commit()
                    flash("Your comment was deleted.", "info")
                else:
                    flash("You did not post that comment.", "error")
            else:
                flash("No such comment.", "error")
            return redirect(url_for('project_view', profile=profile.name, event=event.name, project=project.url_name))
    return render_template('project.html', event=event, project=project, profile=profile,
        comments=comments, commentform=commentform, delcommentform=delcommentform,
        breadcrumbs=[(url_for('index'), "home")], userIsMember=userIsMember)
コード例 #2
0
def project_view(profile, event, project):
    if not profile:
        abort(404)
    if not event:
        abort(404)
    if not project:
        abort(404)

    userIsMember = False
    if g.user:
        user = User.query.filter_by(userid=g.user.userid).first()
        if user:
            participant = Participant.query.filter_by(
                user_id=user.id, event_id=event.id).first()
        if participant:
            project_member = ProjectMember.query.filter_by(
                project_id=project.id, participant_id=participant.id).first()
            if project_member:
                userIsMember = True
    # Fix the join query below and replace the cascaded if conditions.
    # if g.user:
    #   query = (ProjectMember
    #       .query.filter(ProjectMember.project_id == project.id)
    #       .join(Participant).filter(Participant.event_id == event.id)
    #       .join(User).filter(User.userid == g.user.userid))
    #   print "user id = %s, event id = %s, project id = %s" % (
    #       g.user.userid, event.id, project.id)
    #   print query.statement
    #   project_member = query.first()
    #   print project_member.project.name

    comments = sorted(
        Comment.query.filter_by(commentspace=project.comments,
                                reply_to=None).order_by('created_at').all(),
        key=lambda c: c.votes.count,
        reverse=True)
    commentform = CommentForm()
    delcommentform = DeleteCommentForm()
    commentspace = project.comments
    if request.method == 'POST':
        if request.form.get(
                'form.id') == 'newcomment' and commentform.validate():
            if commentform.edit_id.data:
                comment = commentspace.get_comment(
                    int(commentform.edit_id.data))
                if comment:
                    if comment.user == g.user:
                        comment.message = commentform.message.data
                        comment.message_html = markdown(comment.message)
                        comment.edited_at = datetime.utcnow()
                        flash("Your comment has been edited", "info")
                    else:
                        flash("You can only edit your own comments", "info")
                else:
                    flash("No such comment", "error")
            else:
                comment = Comment(user=g.user,
                                  commentspace=project.comments,
                                  message=commentform.message.data)
                if commentform.reply_to_id.data:
                    reply_to = commentspace.get_comment(
                        int(commentform.reply_to_id.data))
                    if reply_to and reply_to.commentspace == project.comments:
                        comment.reply_to = reply_to
                comment.message_html = bleach.linkify(
                    markdown(commentform.message.data))
                project.comments.count += 1
                comment.votes.vote(g.user)  # Vote for your own comment
                comment.make_id()
                db.session.add(comment)
                flash("Your comment has been posted", "info")
            db.session.commit()
            # Redirect despite this being the same page because HTTP 303 is required to not break
            # the browser Back button
            return redirect(
                url_for('project_view',
                        profile=profile.name,
                        event=event.name,
                        project=project.url_name))

        elif request.form.get(
                'form.id') == 'delcomment' and delcommentform.validate():
            comment = commentspace.get_comment(
                int(delcommentform.comment_id.data))
            if comment:
                if comment.user == g.user:
                    comment.delete()
                    project.comments.count -= 1
                    db.session.commit()
                    flash("Your comment was deleted.", "info")
                else:
                    flash("You did not post that comment.", "error")
            else:
                flash("No such comment.", "error")
            return redirect(
                url_for('project_view',
                        profile=profile.name,
                        event=event.name,
                        project=project.url_name))
    return render_template('project.html',
                           event=event,
                           project=project,
                           profile=profile,
                           comments=comments,
                           commentform=commentform,
                           delcommentform=delcommentform,
                           breadcrumbs=[(url_for('index'), "home")],
                           userIsMember=userIsMember)
コード例 #3
0
ファイル: project.py プロジェクト: iambibhas/hacknight
def project_view(profile, event, project):
    user_is_member = False
    if g.user:
        project_member = ProjectMember.query.filter_by(project_id=project.id, user_id=g.user.id).first()
        project_members = ProjectMember.query.filter_by(project_id=project.id).all()
        email_ids = [member.user.email for member in project_members]
        if project_member:
            user_is_member = True
    # Fix the join query below and replace the cascaded if conditions.
    # if g.user:
    #   query = (ProjectMember
    #       .query.filter(ProjectMember.project_id == project.id)
    #       .join(Participant).filter(Participant.event_id == event.id)
    #       .join(User).filter(User.userid == g.user.userid))
    #   print "user id = %s, event id = %s, project id = %s" % (
    #       g.user.userid, event.id, project.id)
    #   print query.statement
    #   project_member = query.first()
    #   print project_member.project.name

    comments = sorted(Comment.query.filter_by(commentspace=project.comments, reply_to=None).order_by('created_at').all(),
        key=lambda c: c.votes.count, reverse=True)
    commentform = CommentForm()
    delcommentform = DeleteCommentForm()
    commentspace = project.comments
    if request.method == 'POST':
        if request.form.get('form.id') == 'newcomment' and commentform.validate():
            if commentform.edit_id.data:
                comment = commentspace.get_comment(int(commentform.edit_id.data))
                if comment:
                    if comment.user == g.user:
                        comment.message = commentform.message.data
                        comment.message_html = markdown(comment.message)
                        comment.edited_at = datetime.utcnow()
                        flash("Your comment has been edited", "info")
                    else:
                        flash("You can only edit your own comments", "info")
                else:
                    flash("No such comment", "error")
            else:
                comment = Comment(user=g.user, commentspace=project.comments, message=commentform.message.data)
                send_email_info = []
                if commentform.reply_to_id.data:
                    reply_to = commentspace.get_comment(int(commentform.reply_to_id.data))
                    if reply_to and reply_to.commentspace == project.comments:
                        comment.reply_to = reply_to
                        if not reply_to.user == g.user:
                            send_email_info.append({"to": reply_to.user.email,
                                "subject": "Hacknight: %s " % (project.title),
                                "template": 'comment_owner_email.md'})
                        try:
                            email_ids.remove(project.user.email)
                            email_ids.remove(reply_to.user.email)
                        except ValueError:
                            pass
                        if project.user != reply_to.user:
                            send_email_info.append({"to": project.user.email,
                                "subject": "Hacknight: %s " % (project.title),
                                "template": 'project_owner_email.md'})
                        try:
                            email_ids.remove(g.user.email)
                        except ValueError:
                            pass
                        for email_id in email_ids:
                            send_email_info.append({"to": email_id,
                            "subject": "Hacknight: %s " % (project.title),
                            "template": 'project_team_email.md'})
                else:
                    if not g.user == project.user:
                        try:
                            email_ids.remove(project.user.email)
                        except ValueError:
                            pass
                        send_email_info.append({"to": project.user.email,
                            "subject": "Hacknight: %s " % (project.title),
                            "template": 'project_owner_email.md'})
                    try:
                        email_ids.remove(g.user.email)
                    except ValueError:
                        pass
                    for email_id in email_ids:
                        send_email_info.append({"to": email_id,
                            "subject": "Hacknight: %s " % (project.title),
                            "template": 'project_team_email.md'})

                comment.message_html = bleach.linkify(markdown(commentform.message.data))
                project.comments.count += 1
                comment.votes.vote(g.user)  # Vote for your own comment
                comment.make_id()
                db.session.add(comment)
                flash("Your comment has been posted", "info")
            db.session.commit()
            link = project.url_for("view", _external=True) + "#c" + str(comment.id)
            for item in send_email_info:
                email_body = render_template(item.pop('template'), project=project, comment=comment, link=link)
                if item['to']:
                    send_email(sender=None, html=markdown(email_body), body=email_body, **item)
            # Redirect despite this being the same page because HTTP 303 is required to not break
            # the browser Back button
            return redirect(project.url_for())

        elif request.form.get('form.id') == 'delcomment' and delcommentform.validate():
            comment = commentspace.get_comment(int(delcommentform.comment_id.data))
            if comment:
                if comment.user == g.user:
                    comment.delete()
                    project.comments.count -= 1
                    db.session.commit()
                    flash("Your comment was deleted.", "info")
                else:
                    flash("You did not post that comment.", "error")
            else:
                flash("No such comment.", "error")
            return redirect(project.url_for())
    return render_template('project.html', event=event, project=project, profile=profile,
        comments=comments, commentform=commentform, delcommentform=delcommentform,
        breadcrumbs=[(url_for('index'), "home")], user_is_member=user_is_member)
コード例 #4
0
ファイル: project.py プロジェクト: iambibhas/hacknight
def project_view(profile, event, project):
    user_is_member = False
    if g.user:
        project_member = ProjectMember.query.filter_by(
            project_id=project.id, user_id=g.user.id).first()
        project_members = ProjectMember.query.filter_by(
            project_id=project.id).all()
        email_ids = [member.user.email for member in project_members]
        if project_member:
            user_is_member = True
    # Fix the join query below and replace the cascaded if conditions.
    # if g.user:
    #   query = (ProjectMember
    #       .query.filter(ProjectMember.project_id == project.id)
    #       .join(Participant).filter(Participant.event_id == event.id)
    #       .join(User).filter(User.userid == g.user.userid))
    #   print "user id = %s, event id = %s, project id = %s" % (
    #       g.user.userid, event.id, project.id)
    #   print query.statement
    #   project_member = query.first()
    #   print project_member.project.name

    comments = sorted(
        Comment.query.filter_by(commentspace=project.comments,
                                reply_to=None).order_by('created_at').all(),
        key=lambda c: c.votes.count,
        reverse=True)
    commentform = CommentForm()
    delcommentform = DeleteCommentForm()
    commentspace = project.comments
    if request.method == 'POST':
        if request.form.get(
                'form.id') == 'newcomment' and commentform.validate():
            if commentform.edit_id.data:
                comment = commentspace.get_comment(
                    int(commentform.edit_id.data))
                if comment:
                    if comment.user == g.user:
                        comment.message = commentform.message.data
                        comment.message_html = markdown(comment.message)
                        comment.edited_at = datetime.utcnow()
                        flash("Your comment has been edited", "info")
                    else:
                        flash("You can only edit your own comments", "info")
                else:
                    flash("No such comment", "error")
            else:
                comment = Comment(user=g.user,
                                  commentspace=project.comments,
                                  message=commentform.message.data)
                send_email_info = []
                if commentform.reply_to_id.data:
                    reply_to = commentspace.get_comment(
                        int(commentform.reply_to_id.data))
                    if reply_to and reply_to.commentspace == project.comments:
                        comment.reply_to = reply_to
                        if not reply_to.user == g.user:
                            send_email_info.append({
                                "to":
                                reply_to.user.email,
                                "subject":
                                "Hacknight: %s " % (project.title),
                                "template":
                                'comment_owner_email.md'
                            })
                        try:
                            email_ids.remove(project.user.email)
                            email_ids.remove(reply_to.user.email)
                        except ValueError:
                            pass
                        if project.user != reply_to.user:
                            send_email_info.append({
                                "to":
                                project.user.email,
                                "subject":
                                "Hacknight: %s " % (project.title),
                                "template":
                                'project_owner_email.md'
                            })
                        try:
                            email_ids.remove(g.user.email)
                        except ValueError:
                            pass
                        for email_id in email_ids:
                            send_email_info.append({
                                "to":
                                email_id,
                                "subject":
                                "Hacknight: %s " % (project.title),
                                "template":
                                'project_team_email.md'
                            })
                else:
                    if not g.user == project.user:
                        try:
                            email_ids.remove(project.user.email)
                        except ValueError:
                            pass
                        send_email_info.append({
                            "to":
                            project.user.email,
                            "subject":
                            "Hacknight: %s " % (project.title),
                            "template":
                            'project_owner_email.md'
                        })
                    try:
                        email_ids.remove(g.user.email)
                    except ValueError:
                        pass
                    for email_id in email_ids:
                        send_email_info.append({
                            "to":
                            email_id,
                            "subject":
                            "Hacknight: %s " % (project.title),
                            "template":
                            'project_team_email.md'
                        })

                comment.message_html = bleach.linkify(
                    markdown(commentform.message.data))
                project.comments.count += 1
                comment.votes.vote(g.user)  # Vote for your own comment
                comment.make_id()
                db.session.add(comment)
                flash("Your comment has been posted", "info")
            db.session.commit()
            link = project.url_for("view", _external=True) + "#c" + str(
                comment.id)
            for item in send_email_info:
                email_body = render_template(item.pop('template'),
                                             project=project,
                                             comment=comment,
                                             link=link)
                if item['to']:
                    send_email(sender=None,
                               html=markdown(email_body),
                               body=email_body,
                               **item)
            # Redirect despite this being the same page because HTTP 303 is required to not break
            # the browser Back button
            return redirect(project.url_for())

        elif request.form.get(
                'form.id') == 'delcomment' and delcommentform.validate():
            comment = commentspace.get_comment(
                int(delcommentform.comment_id.data))
            if comment:
                if comment.user == g.user:
                    comment.delete()
                    project.comments.count -= 1
                    db.session.commit()
                    flash("Your comment was deleted.", "info")
                else:
                    flash("You did not post that comment.", "error")
            else:
                flash("No such comment.", "error")
            return redirect(project.url_for())
    return render_template('project.html',
                           event=event,
                           project=project,
                           profile=profile,
                           comments=comments,
                           commentform=commentform,
                           delcommentform=delcommentform,
                           breadcrumbs=[(url_for('index'), "home")],
                           user_is_member=user_is_member)