Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d15d156f75
Fetching contributors…

Cannot retrieve contributors at this time

executable file 85 lines (74 sloc) 3.629 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect
from mainapp.models import Report, ReportUpdate, Ward, FixMyStreetMap, ReportCategory
from mainapp.forms import ReportForm,ReportUpdateForm
from django.template import Context, RequestContext
from django.contrib.gis.geos import *
from fixmystreet import settings
from django.utils.translation import ugettext as _

def create_report( request, is_confirmed = False):
    """
Helper method used by both internal and Open311 API.
"""
    pnt = fromstr("POINT(" + request.POST["lon"] + " " + request.POST["lat"] + ")", srid=4326)
    update_form = ReportUpdateForm( request.POST )
    report_form = ReportForm( request.POST, request.FILES )
        
    # this is a lot more complicated than it has to be because of the information
    # spread across two forms.
        
    if request.POST['category_id'] != "" and update_form.is_valid() and report_form.is_valid():
        report = report_form.save( commit = False )
        update = update_form.save(commit=False)
        #these are in the form for 'update'
        report.desc = update.desc
        report.author = update.author
        #this is in neither form
        report.category_id = request.POST['category_id']
        #this info is custom
        report.point = pnt
        report.ward = Ward.objects.get(geom__contains=pnt)
        report.is_confirmed = is_confirmed
        update.report = report
        update.first_update = True
        update.is_confirmed = is_confirmed
        update.created_at = report.created_at
        report.save()
        report.reportupdate_set.add(update)
        report.save()
        return( report )
    else:
        return None

def new( request ):
    category_error = None

    if request.method == "POST":
        # TOFIX:category ID is checked for separately as it's not part of the report form
        if request.POST['category_id']:
            report = create_report(request)
            if report:
                return( HttpResponseRedirect( report.get_absolute_url() ))
            # otherwise, there was an error with one of the forms.
        else:
            category_error = _("Please select a category")
            
    else:
        pnt = fromstr("POINT(" + request.GET["lon"] + " " + request.GET["lat"] + ")", srid=4326)
        report_form = ReportForm()
        update_form = ReportUpdateForm()
    
    
    return render_to_response("reports/new.html",
                { "lat": pnt.y,
                  "lon": pnt.x,
                  "google": FixMyStreetMap(pnt, True),
                  "categories": ReportCategory.objects.all().order_by("category_class"),
                  "report_form": report_form,
                  "update_form": update_form,
                  "category_error" : category_error, },
                context_instance=RequestContext(request))
    
def show( request, report_id ):
    report = get_object_or_404(Report, id=report_id)
    subscribers = report.reportsubscriber_set.count() + 1
    return render_to_response("reports/show.html",
                { "report": report,
                  "subscribers": subscribers,
                  "ward":report.ward,
                  "updates": ReportUpdate.objects.filter(report=report, is_confirmed=True).order_by("created_at")[1:],
                  "update_form": ReportUpdateForm(),
                  "google": FixMyStreetMap((report.point)) },
                context_instance=RequestContext(request))


Something went wrong with that request. Please try again.