In this commit, I have made a few changes and tightened up some of my models

code. I added in two major pieces of functionality: table foundations and a
decorator to confirm whether or not a user is a member of a certain group.

Table Foundations are default rows that should be present in a given table as
soon as the database is initialized. For example, I am using these to populate
the core__groups table with all of the necessary groups ('moderator', 'com-
menter', etc). Right now, this is achieved by adding a dictionary of parameters
(with the parameters as lists) to the constant FOUNDATIONS in
mediagoblin.db.models. The keys to this dictionary are uninstantiated classes.
The classes which require foundations also have must have a constructor so that
the list of parameters can be passed appropriately like so:
        Model(*parameters)
In order to implement these foundations, I added the method populate_table_fou-
-ndations to MigrationManager in mediagoblin.db.migration_tools.

The decorator, called user_in_group, accepts as a parameter a unicode string,
and then decides whether to redirect to 403 or let the user access the page. The
identifier is the Group.group_name string, because I believe that will allow for
the most readable code.

I also added in the simple decorator require_admin_login.

In terms of tightening up my code, I made many minor changes to my use of white
space and made a few small documentation additions. I removed a vestigial class
(ReportForm) from mediagoblin.user_pages.forms. I moved all of my migrations in-
to one registered Migration.

Setting up Foundations
==============================

--\ mediagoblin/db/migration_tools.py
--| created: MigrationManager.populate_table_foundations
--| modified: MigrationManager.init_or_migrate to run
  |     self.populate_table_foundations on init

--\ mediagoblin/db/models.py
--| created: FOUNDATIONS
----| created: group_foundations

Working With Permissions
==============================
--\ mediagoblin/decorators.py
--| created: user_in_group
--| created: require_admin_login

--\ mediagoblin/user_pages/views.py
--| modified: added decorator user_in_group to file_a_report

--\ mediagoblin/admin/views.py
--| modified: added decorator require_admin_login to all views functions

General Code Tidying
=============================

--/ mediagoblin/admin/views.py
--/ mediagoblin/user_pages/forms.py
--/ mediagoblin/db/models.py
--/ mediagoblin/user_pages/lib.py
--/ mediagoblin/user_pages/views.py
--/ mediagoblin/db/migrations.py
This commit is contained in:
tilly-Q
2013-06-27 14:13:42 -07:00
parent 30a9fe7c1c
commit 9b8ef022ef
8 changed files with 175 additions and 87 deletions

View File

@@ -59,9 +59,3 @@ class MediaReportForm(wtforms.Form):
report_reason = wtforms.TextAreaField('Reason for Reporting')
media_entry_id = wtforms.IntegerField()
reporter_id = wtforms.IntegerField()
class ReportForm(wtforms.Form):
report_reason = wtforms.TextAreaField('Reason for Reporting')
media_entry_id = wtforms.IntegerField()
reporter_id = wtforms.IntegerField()
comment_id = wtforms.IntegerField()

View File

@@ -79,12 +79,15 @@ def add_media_to_collection(collection, media, note=None, commit=True):
def build_report_form(form_dict):
"""
:param form_dict should be an ImmutableMultiDict object which is what is
returned from 'request.form.' The Object should have valid keys
matching the fields in either MediaReportForm or CommentReportForm
This function is used to convert a form dictionary (from a User filing a
report) into either a MediaReport or CommentReport object.
:returns either of MediaReport or a CommentReport object that has not been saved.
In case of an improper form_dict, returns None
:param form_dict should be an ImmutableMultiDict object as is returned from
'request.form.' The Object should have valid keys matching the fields
in either MediaReportForm or CommentReportForm
:returns either of MediaReport or a CommentReport object that has not been
saved. In case of an improper form_dict, returns None
"""
if 'comment_id' in form_dict.keys():
report_form = user_forms.CommentReportForm(form_dict)
@@ -92,6 +95,7 @@ def build_report_form(form_dict):
report_form = user_forms.MediaReportForm(form_dict)
else:
return None
if report_form.validate() and 'comment_id' in form_dict.keys():
report_model = CommentReport()
report_model.comment_id = report_form.comment_id.data
@@ -100,6 +104,7 @@ def build_report_form(form_dict):
report_model.media_entry_id = report_form.media_entry_id.data
else:
return None
report_model.report_content = report_form.report_reason.data or u''
report_model.reporter_id = report_form.reporter_id.data
return report_model

View File

@@ -20,7 +20,7 @@ import datetime
from mediagoblin import messages, mg_globals
from mediagoblin.db.models import (MediaEntry, MediaTag, Collection,
CollectionItem, User, MediaComment,
CommentReport, MediaReport)
CommentReport, MediaReport, Group)
from mediagoblin.tools.response import render_to_response, render_404, \
redirect, redirect_obj
from mediagoblin.tools.translate import pass_to_ugettext as _
@@ -30,7 +30,7 @@ from mediagoblin.user_pages.lib import (send_comment_email, build_report_form,
add_media_to_collection)
from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
get_media_entry_by_id,
get_media_entry_by_id, user_in_group,
require_active_login, user_may_delete_media, user_may_alter_collection,
get_user_collection, get_user_collection_item, active_user_from_url,
get_media_comment_by_id)
@@ -621,22 +621,26 @@ def processing_panel(request):
@require_active_login
@get_user_media_entry
def file_a_report(request, media, comment=None):
@user_in_group(u'reporter')
def file_a_report(request, media, comment=None, required_group=1):
if request.method == "POST":
report_form = build_report_form(request.form)
report_form.save()
return redirect(
request,
'index')
request,
'index')
if comment is not None:
context = {'media': media,
'comment':comment}
'comment':comment}
else:
context = {'media': media}
return render_to_response(
request,
'mediagoblin/user_pages/report.html',
context)
request,
'mediagoblin/user_pages/report.html',
context)
@require_active_login
@get_user_media_entry