def show(request, issue, party): """Shows information about a :class:`View`. The view may not yet be present in the database. :param request: The HTTP request. :type request: ``django.http.HttpRequest`` :param issue: The issue. :type issue: ``politics.apps.core.models.Issue`` :param party: The party. :type party: ``politics.apps.core.models.Party`` """ reference_form = ReferenceForm() view = ViewManager().get_view(issue, party) # Are they trying to create a reference? if request.method == "POST" and request.user.is_authenticated(): view.save() # The view must exist in the database. instance = Reference(author=request.user, view=view) reference_form = ReferenceForm(request.POST, instance=instance) if reference_form.is_valid(): # Version the reference. with reversion.create_revision(): reversion.set_user(request.user) reference_form.save() reference_form = ReferenceForm() logging.getLogger("email").info("Reference Created", extra={"body": "%s created a reference.\n\nhttp://govmonitor.org%s" % ( request.user.get_full_name(), view_url(view) ) }) # The currently winning reference. current_reference = view.get_current_reference() current_reference_pk = getattr(current_reference, "pk", None) # Sort the references in descending order of score. Break ties between # references by ordering randomly (taking advantage of tuple comparisons). references = view.reference_set.exclude(pk=current_reference_pk) references = sorted(references, None, lambda r: (r.score, random()), True) return { "current_reference": current_reference, "reference_form": reference_form, "references": references, "view": view }
def index(request): # Fetch the user's current task. If they don't have one, select one for # them. GET requests modifying state isn't ideal, but it's better UX. task = Task.objects.get_for_user(request.user) if task is None: task = _assign_task(request.user) completed_task = None form = ReferenceForm() if request.method == "POST": reference = Reference(author=request.user, view=task.view) form = ReferenceForm(request.POST, instance=reference) if form.is_valid(): # Version the reference. with reversion.create_revision(): reversion.set_user(request.user) reference = form.save() completed_task = task completed_task.is_complete = True completed_task.save() form = ReferenceForm() task = _assign_task(request.user) view = reference.view logging.getLogger("email").info("Reference Saved", extra={"body": "%s created a reference.\n\nhttp://govmonitor.org%s" % ( request.user.get_full_name(), view_url(view) ) }) return {"completed_task": completed_task, "form": form, "task": task}