[copr] master: Make legal flag a separate db entity (147f3c8)

bkabrda at fedoraproject.org bkabrda at fedoraproject.org
Wed Apr 3 08:47:17 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit 147f3c85efd237b611531c2a1da780f5a9d15e80
Author: Bohuslav Kabrda <bkabrda at redhat.com>
Date:   Wed Apr 3 10:47:02 2013 +0200

    Make legal flag a separate db entity
    
    - and rename CoprLegalForm to CoprLegalFlagForm to keep things consistent


>---------------------------------------------------------------

 .../versions/246fd2dbf398_add_legal_flag.py        |   32 ++++++++++++++++++++
 coprs_frontend/coprs/forms.py                      |    2 +-
 coprs_frontend/coprs/models.py                     |   25 +++++++++++++++
 .../coprs/templates/admin/legal-flag.html          |   16 ++++------
 .../coprs/templates/coprs/_coprs_forms.html        |    2 +-
 .../coprs/templates/coprs/detail/overview.html     |    4 +-
 .../coprs/views/admin_ns/admin_general.py          |   28 ++++++-----------
 .../coprs/views/coprs_ns/coprs_general.py          |   17 ++++------
 8 files changed, 84 insertions(+), 42 deletions(-)

diff --git a/coprs_frontend/alembic/versions/246fd2dbf398_add_legal_flag.py b/coprs_frontend/alembic/versions/246fd2dbf398_add_legal_flag.py
new file mode 100644
index 0000000..bea0d38
--- /dev/null
+++ b/coprs_frontend/alembic/versions/246fd2dbf398_add_legal_flag.py
@@ -0,0 +1,32 @@
+"""add_legal_flag
+
+Revision ID: 246fd2dbf398
+Revises: d062c3d9c00
+Create Date: 2013-04-03 10:39:54.837803
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '246fd2dbf398'
+down_revision = 'd062c3d9c00'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('legal_flag', sa.Column('resolved_on', sa.Integer(), nullable=True))
+    op.add_column('legal_flag', sa.Column('raised_on', sa.Integer(), nullable=True))
+    op.drop_column('legal_flag', u'state')
+    op.drop_column('legal_flag', u'resolve_message')
+    ### end Alembic commands ###
+
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('legal_flag', sa.Column(u'resolve_message', sa.TEXT(), nullable=True))
+    op.add_column('legal_flag', sa.Column(u'state', sa.INTEGER(), nullable=True))
+    op.drop_column('legal_flag', 'raised_on')
+    op.drop_column('legal_flag', 'resolved_on')
+    ### end Alembic commands ###
diff --git a/coprs_frontend/coprs/forms.py b/coprs_frontend/coprs/forms.py
index 6b533cd..02272d8 100644
--- a/coprs_frontend/coprs/forms.py
+++ b/coprs_frontend/coprs/forms.py
@@ -138,7 +138,7 @@ class BuildForm(wtf.Form):
                                validators = [wtf.NumberRange(min = constants.MIN_BUILD_TIMEOUT, max = constants.MAX_BUILD_TIMEOUT)],
                                default = constants.DEFAULT_BUILD_TIMEOUT)
 
-class CoprLegalForm(wtf.Form):
+class CoprLegalFlagForm(wtf.Form):
     comment = wtf.TextAreaField('Comment')
 
 class PermissionsApplierFormFactory(object):
diff --git a/coprs_frontend/coprs/models.py b/coprs_frontend/coprs/models.py
index b905831..d6d5aee 100644
--- a/coprs_frontend/coprs/models.py
+++ b/coprs_frontend/coprs/models.py
@@ -268,6 +268,31 @@ class CoprChroot(db.Model, Serializer):
                                                         single_parent=True,
                                                         cascade='all,delete,delete-orphan'))
 
+class LegalFlag(db.Model, Serializer):
+    id = db.Column(db.Integer, primary_key=True)
+    # message from user who raised the flag (what he thinks is wrong)
+    raise_message = db.Column(db.Text)
+    # time of raising the flag as returned by int(time.time())
+    raised_on = db.Column(db.Integer)
+    # time of resolving the flag by admin as returned by int(time.time())
+    resolved_on = db.Column(db.Integer)
+
+    # relations
+    copr_id = db.Column(db.Integer, db.ForeignKey('copr.id'), nullable=True)
+    # cascade='all' means that we want to keep these even if copr is deleted
+    copr = db.relationship('Copr', backref=db.backref('legal_flags', cascade='all'))
+    # user who reported the problem
+    reporter_id = db.Column(db.Integer, db.ForeignKey('user.id'))
+    reporter = db.relationship('User',
+                               backref=db.backref('legal_flags_raised'),
+                               foreign_keys=[reporter_id])
+    # admin who resolved the problem
+    resolver_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
+    resolver = db.relationship('User',
+                               backref=db.backref('legal_flags_resolved'),
+                               foreign_keys=[resolver_id])
+
+
 class Action(db.Model, Serializer):
     """Representation of a custom action that needs backends cooperation/admin attention/..."""
     id = db.Column(db.Integer, primary_key=True)
diff --git a/coprs_frontend/coprs/templates/admin/legal-flag.html b/coprs_frontend/coprs/templates/admin/legal-flag.html
index 4cce6e5..6dc0e2a 100644
--- a/coprs_frontend/coprs/templates/admin/legal-flag.html
+++ b/coprs_frontend/coprs/templates/admin/legal-flag.html
@@ -6,16 +6,12 @@
   {% for flag in legal_flags %}
     <div class="legal-flag">
       <div>
-        {% if flag.copr %}
-          <a href={{ url_for('coprs_ns.copr_detail', username=flag.copr.owner.name, coprname=flag.copr.name) }}>
-            {{ flag.copr.owner.name }}/{{ flag.copr.name }}
-          </a>
-          | <a href="">Freeze</a>
-	  | <a href="{{ url_for('coprs_ns.copr_delete', username=flag.copr.owner.name, coprname=flag.copr.name) }}">Delete</a>
-        {% else %}
-          {{ flag.old_value }} - copr was deleted
-        {% endif %}
-	<form method="post" action="{{ url_for('admin_ns.legal_flag_resolve', action_id=flag.id) }}">
+        <a href={{ url_for('coprs_ns.copr_detail', username=flag.copr.owner.name, coprname=flag.copr.name) }}>
+          {{ flag.copr.owner.name }}/{{ flag.copr.name }}
+        </a>
+        | <a href="">Freeze</a>
+	| <a href="{{ url_for('coprs_ns.copr_delete', username=flag.copr.owner.name, coprname=flag.copr.name) }}">Delete</a>
+	<form method="post" action="{{ url_for('admin_ns.legal_flag_resolve', flag_id=flag.id) }}">
             <input type="submit" value="Resolve">
           </form>
       </div>
diff --git a/coprs_frontend/coprs/templates/coprs/_coprs_forms.html b/coprs_frontend/coprs/templates/coprs/_coprs_forms.html
index e8856e1..dd13b40 100644
--- a/coprs_frontend/coprs/templates/coprs/_coprs_forms.html
+++ b/coprs_frontend/coprs/templates/coprs/_coprs_forms.html
@@ -77,7 +77,7 @@
   {% endif %}
   {% endmacro %}
 
-{% macro copr_legal_form(form, copr) %}
+{% macro copr_legal_flag_form(form, copr) %}
 <form class="legal-flag" method="post" action={{ url_for('coprs_ns.copr_legal_flag', username=copr.owner.name, coprname=copr.name) }}>
   If you think that this copr may be violating our policies, you can mark it for legal review.
   You can add an optional comment describing what you think is wrong here.
diff --git a/coprs_frontend/coprs/templates/coprs/detail/overview.html b/coprs_frontend/coprs/templates/coprs/detail/overview.html
index b0bf4e2..a038913 100644
--- a/coprs_frontend/coprs/templates/coprs/detail/overview.html
+++ b/coprs_frontend/coprs/templates/coprs/detail/overview.html
@@ -1,6 +1,6 @@
 {% extends "coprs/detail.html" %}
 
-{% from "coprs/_coprs_forms.html" import copr_legal_form with context %}
+{% from "coprs/_coprs_forms.html" import copr_legal_flag_form with context %}
 
 {% block overview_selected %}selected{% endblock %}
 
@@ -51,5 +51,5 @@
   {% endif %}
 
   <hr>
-  {{ copr_legal_form(form, copr) }}
+  {{ copr_legal_flag_form(form, copr) }}
 {% endblock %}
diff --git a/coprs_frontend/coprs/views/admin_ns/admin_general.py b/coprs_frontend/coprs/views/admin_ns/admin_general.py
index d094b36..b05058b 100644
--- a/coprs_frontend/coprs/views/admin_ns/admin_general.py
+++ b/coprs_frontend/coprs/views/admin_ns/admin_general.py
@@ -19,29 +19,21 @@ def admin_index():
 @admin_ns.route('/legal-flag/')
 @login_required(role=helpers.RoleEnum('admin'))
 def legal_flag():
-    legal_flags = models.Action.query.filter(models.Action.action_type==helpers.ActionTypeEnum('legal-flag')).\
-                                      filter(models.Action.object_type=='copr').\
-                                      filter(models.Action.ended_on==None).\
-                                      order_by(models.Action.created_on.desc()).\
-                                      all()
-
-    ids = map(lambda x: x.object_id, legal_flags)
-    # if there are no ids, we would trigger "IN" query with empty set, which
-    # is not very good (and sqlalchemy complains about it)
-    coprs = coprs_logic.CoprsLogic.get_multiple(flask.g.user, ids=ids).all() if ids else []
-    for flag in legal_flags:
-        # handle the situation where copr was deleted in the meanwhile
-        copr = filter(lambda x: flag.object_id == x.id, coprs)
-        flag.copr = copr[0] if copr else None
+    legal_flags = models.LegalFlag.query.outerjoin(models.LegalFlag.copr).\
+                                         options(db.contains_eager(models.LegalFlag.copr)).\
+                                         filter(models.LegalFlag.resolved_on==None).\
+                                         order_by(models.LegalFlag.raised_on.desc()).\
+                                         all()
 
     return flask.render_template('admin/legal-flag.html',
                                  legal_flags=legal_flags)
 
- at admin_ns.route('/legal-flag/<int:action_id>/resolve/', methods=['POST'])
+ at admin_ns.route('/legal-flag/<int:flag_id>/resolve/', methods=['POST'])
 @login_required(role=helpers.RoleEnum('admin'))
-def legal_flag_resolve(action_id):
-    action = models.Action.query.filter(models.Action.id==action_id).\
-                                 update({'ended_on': int(time.time())})
+def legal_flag_resolve(flag_id):
+    legal_flag = models.LegalFlag.query.filter(models.LegalFlag.id==flag_id).\
+                                        update({'resolved_on': int(time.time()),
+                                                'resolver_id': flask.g.user.id})
     db.session.commit()
     flask.flash('Legal flag resolved')
     return flask.redirect(flask.url_for('admin_ns.legal_flag'))
diff --git a/coprs_frontend/coprs/views/coprs_ns/coprs_general.py b/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
index 4203af5..9d5be98 100644
--- a/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
+++ b/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
@@ -104,7 +104,7 @@ def copr_new():
 @coprs_ns.route('/detail/<username>/<coprname>/')
 def copr_detail(username, coprname):
     query = coprs_logic.CoprsLogic.get(flask.g.user, username, coprname, with_mock_chroots=True)
-    form = forms.CoprLegalForm()
+    form = forms.CoprLegalFlagForm()
     try:
         copr = query.one()
     except sqlalchemy.orm.exc.NoResultFound:
@@ -261,17 +261,14 @@ def copr_delete(username, coprname):
 @coprs_ns.route('/detail/<username>/<coprname>/legal_flag/', methods=['POST'])
 @login_required
 def copr_legal_flag(username, coprname):
-    form = forms.CoprLegalForm()
+    form = forms.CoprLegalFlagForm()
     copr = coprs_logic.CoprsLogic.get(flask.g.user, username, coprname).first()
 
-    action = models.Action(action_type=helpers.ActionTypeEnum('legal-flag'),
-                           object_type='copr',
-                           object_id=copr.id,
-                           message=form.comment.data,
-                           old_value='{0}/{1}'.format(username, coprname),
-                           new_value='{0}/{1}'.format(username, coprname),
-                           created_on=int(time.time()))
-    db.session.add(action)
+    legal_flag = models.LegalFlag(raise_message=form.comment.data,
+                                  raised_on=int(time.time()),
+                                  copr=copr,
+                                  reporter=flask.g.user)
+    db.session.add(legal_flag)
     db.session.commit()
     flask.flash('Admin was noticed about your report and will investigate the copr shortly.')
     return flask.redirect(flask.url_for('coprs_ns.copr_detail', username=username, coprname=coprname))



More information about the copr-devel mailing list