This is a very small commit. All that I have done here is to clean up my code

a bit. I made it so that mediagoblin.user_pages.report recieves the report
form as part of it's context. I also made sure I used {% trans %} tags effect-
-ively.
This commit is contained in:
tilly-Q
2013-07-29 15:14:39 -04:00
parent 3aa3871b90
commit f26c21cd5b
6 changed files with 99 additions and 82 deletions

View File

@@ -54,12 +54,10 @@ class CommentReportForm(wtforms.Form):
report_reason = wtforms.TextAreaField(
_('Reason for Reporting'),
[wtforms.validators.Required()])
comment_id = wtforms.IntegerField()
reporter_id = wtforms.IntegerField()
reporter_id = wtforms.HiddenField('')
class MediaReportForm(wtforms.Form):
report_reason = wtforms.TextAreaField(
_('Reason for Reporting'),
[wtforms.validators.Required()])
media_entry_id = wtforms.IntegerField()
reporter_id = wtforms.IntegerField()
reporter_id = wtforms.HiddenField('')

View File

@@ -78,39 +78,33 @@ def add_media_to_collection(collection, media, note=None, commit=True):
if commit:
Session.commit()
def build_report_table(form_dict):
def build_report_object(report_form, media_entry=None, comment=None):
"""
This function is used to convert a form dictionary (from a User filing a
This function is used to convert a form object (from a User filing a
report) into either a MediaReport or CommentReport object.
: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
:param report_form should be a MediaReportForm or a CommentReportForm
object
:param
: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)
elif 'media_entry_id' in form_dict.keys():
report_form = user_forms.MediaReportForm(form_dict)
if report_form.validate() and comment is not None:
report_object = CommentReport()
report_object.comment_id = comment.id
report_object.reported_user_id = MediaComment.query.get(
comment.id).get_author.id
elif report_form.validate() and media_entry is not None:
report_object = MediaReport()
report_object.media_entry_id = media_entry.id
report_object.reported_user_id = MediaEntry.query.get(
media_entry.id).get_uploader.id
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
report_model.reported_user_id = MediaComment.query.get(
report_model.comment_id).get_author.id
elif report_form.validate() and 'media_entry_id' in form_dict.keys():
report_model = MediaReport()
report_model.media_entry_id = report_form.media_entry_id.data
report_model.reported_user_id = MediaEntry.query.get(
report_model.media_entry_id).get_uploader.id
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
report_object.report_content = report_form.report_reason.data
report_object.reporter_id = report_form.reporter_id.data
return report_object

View File

@@ -26,7 +26,7 @@ from mediagoblin.tools.response import render_to_response, render_404, \
from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.tools.pagination import Pagination
from mediagoblin.user_pages import forms as user_forms
from mediagoblin.user_pages.lib import (send_comment_email, build_report_table,
from mediagoblin.user_pages.lib import (send_comment_email, build_report_object,
add_media_to_collection)
from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
@@ -625,19 +625,30 @@ def processing_panel(request):
@get_user_media_entry
@user_has_privilege(u'reporter')
def file_a_report(request, media, comment=None):
if request.method == "POST":
report_table = build_report_table(request.form)
report_table.save()
return redirect(
request,
'index')
if comment is not None:
form = user_forms.CommentReportForm(request.form)
form.reporter_id.data = request.user.id
context = {'media': media,
'comment':comment}
'comment':comment,
'form':form}
else:
context = {'media': media}
form = user_forms.MediaReportForm(request.form)
form.reporter_id.data = request.user.id
context = {'media': media,
'form':form}
if request.method == "POST":
report_table = build_report_object(form,
media_entry=media,
comment=comment)
# if the object was built successfully, report_table will not be None
if report_table:
report_table.save()
return redirect(
request,
'index')
return render_to_response(
request,