Permalink
Browse files

misc. errors on testing

  • Loading branch information...
1 parent 556b497 commit 51ceb99c3e9f95cad3f8d9649c9284d07bec01a5 @visiblegovernment committed Jul 13, 2011
View
54 mainapp/forms.py
@@ -97,14 +97,39 @@ class Meta:
class ReportUpdateForm(forms.ModelForm):
class Meta:
model = ReportUpdate
- fields = ( 'desc','author','email','phone')
-
- def __init__(self,data=None,files=None,initial=None, freeze_email=False):
- super(ReportUpdateForm,self).__init__(data,files=files, initial=initial)
- if freeze_email:
+ fields = ('desc','author','email','phone')
+
+ def __init__(self,data=None,files=None,initial={},first_update=False,user = None):
+ self.user = None
+ self.first_update= first_update
+ if user and user.is_authenticated() and UserProfile.objects.filter(user=user).exists():
+ self.user = user
+
+ if self.user:
+ if not data:
+ initial[ 'author' ] = user.first_name + " " + user.last_name
+ initial[ 'phone' ] = user.get_profile().phone
+ initial[ 'email' ] = user.email
+ else:
+ # this can't be overridden.
+ data['email'] = user.email
+
+ super(ReportUpdateForm,self).__init__(data,files=files, initial=initial)
+
+ if self.user and not data:
self.fields['email'].widget.attrs['readonly'] = 'readonly'
-
-
+
+
+ def save(self,commit=True):
+ update = super(ReportUpdateForm,self).save( commit = False )
+ update.first_update = self.first_update
+ if self.user:
+ #update.user = self.user
+ update.is_confirmed = True
+ if commit:
+ update.save()
+ return( update )
+
class ReportForm(forms.ModelForm):
"""
ReportForm --
@@ -122,15 +147,15 @@ class Meta:
lat = forms.fields.CharField(widget=forms.widgets.HiddenInput)
lon = forms.fields.CharField(widget=forms.widgets.HiddenInput)
- def __init__(self,data=None,files=None,initial=None,freeze_email=False):
+ def __init__(self,data=None,files=None,initial=None,user=None):
if data:
d2p = DictToPoint(data,exceptclass=None)
else:
d2p = DictToPoint(initial,exceptclass=None)
self.pnt = d2p.pnt()
self.ward = d2p.ward()
- self.update_form = ReportUpdateForm(data=data,initial=initial,freeze_email=freeze_email)
+ self.update_form = ReportUpdateForm(data=data,initial=initial,user=user,first_update = True)
super(ReportForm,self).__init__(data,files, initial=initial)
self.fields['category'] = CategoryChoiceField(self.ward)
@@ -146,7 +171,8 @@ def is_valid(self):
update_valid = self.update_form.is_valid()
return( update_valid and report_valid )
- def save(self, is_confirmed = False):
+ def save(self):
+
report = super(ReportForm,self).save( commit = False )
update = self.update_form.save(commit=False)
@@ -157,14 +183,12 @@ def save(self, is_confirmed = False):
#this info is custom
report.point = self.pnt
report.ward = self.ward
- report.is_confirmed = is_confirmed
- update.report = report
- update.first_update = True
- update.is_confirmed = is_confirmed
- update.created_at = report.created_at
+ #report.user = update.user
+
report.save()
update.report = report
update.save()
+
return( report )
def all_errors(self):
View
27 mainapp/models.py
@@ -9,6 +9,7 @@
import md5
import urllib
import time
+import datetime
from mainapp import emailrules
from datetime import datetime as dt
from django.utils.safestring import mark_safe
@@ -325,6 +326,7 @@ class Report(models.Model):
# this this report come in from a particular mobile app?
device_id = models.CharField(max_length=100,null=True,blank=True)
+ # who filed this report?
objects = models.GeoManager()
def is_subscribed(self, email):
@@ -430,10 +432,29 @@ def save(self):
# does this update require confirmation?
if not self.is_confirmed:
self.get_confirmation()
+ super(ReportUpdate,self).save()
else:
+ # update parent report
+ if not self.created_at:
+ self.created_at = datetime.datetime.now()
+ if self.is_fixed:
+ self.report.is_fixed = True
+ self.report.fixed_at = self.created_at
+ # we track a last updated time in the report to make statistics
+ # (such as on the front page) easier.
+ if not self.first_update:
+ self.report.updated_at = self.created_at
+ else:
+ self.report.updated_at = self.report.created_at
+ self.report.is_confirmed = True
+ super(ReportUpdate,self).save()
+ self.report.save()
self.notify()
- super(ReportUpdate,self).save()
-
+
+ def confirm(self):
+ self.is_confirmed = True
+ self.save()
+
def get_confirmation(self):
""" Send a confirmation email to the user. """
@@ -471,7 +492,7 @@ class ReportSubscriber(models.Model):
confirm_token = models.CharField(max_length=255, null=True)
is_confirmed = models.BooleanField(default=False)
email = models.EmailField()
-
+
class Meta:
db_table = u'report_subscribers'
View
8 mainapp/tests/account.py
@@ -121,7 +121,7 @@ def test_report_submit(self):
self.assertEquals(response.template[0].name, 'reports/show.html')
# there's a new report
- self.assertEqual(Report.objects.filter(title=CREATE_PARAMS['title'],is_confirmed=True).count(), 1 )
+ self.assertEqual(Report.objects.filter(title=CREATE_PARAMS['title'],is_confirmed=True,is_fixed=False).count(), 1 )
self.assertEqual(ReportUpdate.objects.filter(author="Clark Kent",email='user1@test.com',desc=params['desc'],is_confirmed=True).count(),1)
# email should be sent directly to the city
@@ -151,9 +151,11 @@ def test_update_submit(self):
# file the report
response = c.post('/reports/4/updates/', UPDATE_PARAMS, follow=True )
self.assertEquals( response.status_code, 200 )
+
# there's a new update
- self.assertEqual(ReportUpdate.objects.filter(author="Clark Kent",email='user1@test.com',desc=UPDATE_PARAMS['desc'],is_fixed=True,is_confirmed=True).count(),1)
-
+ self.assertEqual(ReportUpdate.objects.filter(report__id=4,author="Clark Kent",email='user1@test.com',desc=UPDATE_PARAMS['desc'],is_fixed=True,is_confirmed=True).count(),1)
+ self.assertEqual(Report.objects.filter(id=4,is_fixed=True).count(),1)
+
# we're redirected to the report page
self.assertEquals(response.template[0].name, 'reports/show.html')
# and it has our update on it.
View
9 mainapp/tests/base_cases.py
@@ -48,16 +48,19 @@ def test(self):
response = self.c.post('/reports/', CREATE_PARAMS, follow=True )
self.assertEquals( response.status_code, 200 )
self.assertEquals(response.template[0].name, 'reports/show.html')
- self.assertEqual(Report.objects.filter(title=CREATE_PARAMS['title']).count(), 1 )
-
+ self.assertEqual(Report.objects.filter(title=CREATE_PARAMS['title'],is_confirmed=False).count(), 1,"There's a new unconfirmed report." )
+ self.assertEqual(ReportUpdate.objects.filter(report__title=CREATE_PARAMS['title'],is_confirmed=False,first_update=True).count(), 1,"There's an unconfirmed report update." )
+
# a confirmation email should be sent to the user
- self.assertEquals(len(mail.outbox), 1)
+ self.assertEquals(len(mail.outbox), 1, "a confirmation email was sent.")
self.assertEquals(mail.outbox[0].to, [u'testcreator@hotmail.com'])
#test confirmation link
confirm_url = self._get_confirm_url(mail.outbox[0])
response = self.c.get(confirm_url, follow=True)
self.assertEquals( response.status_code, 200 )
+ self.assertEqual(Report.objects.filter(title=CREATE_PARAMS['title'],is_confirmed=True).count(), 1,"The report is confirmed." )
+
#now there should be two emails in our outbox
self.assertEquals(len(mail.outbox), 2)
View
13 mainapp/views/mobile/open311v2.py
@@ -43,12 +43,14 @@ class Meta:
model = Report
fields = ('service_code','description','lat','lon','title', 'category', 'photo','device_id','api_key')
- def __init__(self,data=None,files=None,initial=None, freeze_email=False):
+ def __init__(self,data=None,files=None,initial=None, user=None):
+
if data:
+ data = data.copy() # can't modify request.POST directly
data['desc'] = data.get('description','')
data['category'] = data.get('service_code','1')
data['author'] = (data.get('first_name','') + " " + data.get('last_name','')).strip()
- super(Open311ReportForm,self).__init__(data,files, initial=initial,freeze_email=freeze_email)
+ super(Open311ReportForm,self).__init__(data,files, initial=initial,user=user)
self.fields['device_id'].required = True
self.fields['category'].required = False
self.fields['title'].required = False
@@ -98,15 +100,14 @@ def reports(self,request):
return self._render_reports(request, reports)
else:
# creating a new report
- data = request.POST.copy()
- report_form = Open311ReportForm( data, request.FILES )
+ report_form = Open311ReportForm( request.POST, request.FILES,user=request.user )
try:
if report_form.is_valid():
- report = report_form.save(request.user.is_authenticated())
+ report = report_form.save()
if report:
return( self._render_reports(request, [ report ] ) )
return( self._render_errors(request, report_form.all_errors()))
- except Exception, e:
+ except InvalidAPIKey, e:
return render( request,
'open311/v2/_errors.%s' % (self.content_type),
{ 'errors' : {'403' : str(e) } },
View
33 mainapp/views/reports/main.py
@@ -1,39 +1,33 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect,Http404
-from mainapp.models import DictToPoint,Report, ReportUpdate, Ward, FixMyStreetMap, ReportCategory
+from mainapp.models import UserProfile,DictToPoint,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 new( request ):
d2p = DictToPoint( request.REQUEST )
pnt = d2p.pnt()
if request.method == "POST":
#an UpdateForm is bundled inside ReportForm
- report_form = ReportForm( request.POST, request.FILES )
+ report_form = ReportForm( request.POST, request.FILES, user=request.user )
# this checks update is_valid too
if report_form.is_valid():
# this saves the update as part of the report.
- report = report_form.save(request.user.is_authenticated())
+ report = report_form.save()
if report:
return( HttpResponseRedirect( report.get_absolute_url() ))
else:
- initial={ 'lat': request.GET['lat'],
- 'lon': request.GET['lon'],
- 'address': request.GET.get('address',None)
- }
-
- if request.user.is_authenticated():
- initial[ 'author' ] = request.user.first_name + " " + request.user.last_name
- initial[ 'phone' ] = request.user.get_profile().phone
- initial[ 'email' ] = request.user.email
- report_form = ReportForm( initial=initial, freeze_email=request.user.is_authenticated() )
+ initial = {}
+ initial['lat' ] =request.GET['lat']
+ initial['lon'] = request.GET['lon']
+ initial['address'] = request.GET.get('address',None)
+
+ report_form = ReportForm( initial=initial, user=request.user )
return render_to_response("reports/new.html",
{ "google": FixMyStreetMap(pnt, True),
@@ -47,19 +41,12 @@ def new( request ):
def show( request, report_id ):
report = get_object_or_404(Report, id=report_id)
subscribers = report.reportsubscriber_set.count() + 1
- initial = {}
-
- if request.user.is_authenticated():
- initial[ 'author' ] = request.user.first_name + " " + request.user.last_name
- initial[ 'phone' ] = request.user.get_profile().phone
- initial[ 'email' ] = request.user.email
-
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(initial=initial,freeze_email=request.user.is_authenticated()),
+ "update_form": ReportUpdateForm(user=request.user),
"google": FixMyStreetMap((report.point)) },
context_instance=RequestContext(request))
View
28 mainapp/views/reports/updates.py
@@ -7,21 +7,20 @@
def new( request, report_id ):
report = get_object_or_404(Report, id=report_id)
if request.method == 'POST':
- update_form = ReportUpdateForm( request.POST )
+ update_form = ReportUpdateForm( request.POST, user=request.user )
if update_form.is_valid():
update = update_form.save(commit=False)
+ update.report = report
update.is_fixed = request.POST.has_key('is_fixed')
- update.report=report
- update.is_confirmed = request.user.is_authenticated()
update.save()
# redirect after a POST
- if request.user.is_authenticated():
+ if update.is_confirmed:
return( HttpResponseRedirect( report.get_absolute_url() ) )
else:
return( HttpResponseRedirect( '/reports/updates/create/' ) )
else:
- update_form = ReportUpdateForm()
+ update_form = ReportUpdateForm(initial={},user=request.user)
return render_to_response("reports/show.html",
{ "report": report,
@@ -41,24 +40,7 @@ def confirm( request, confirm_token ):
if update.is_confirmed:
return( HttpResponseRedirect( update.report.get_absolute_url() ))
- # is the update fixed?
- if update.is_fixed:
- update.report.is_fixed = True
- update.report.fixed_at = update.created_at
+ update.confirm()
- update.is_confirmed = True
- update.save()
-
- # we track a last updated time in the report to make statistics
- # (such as on the front page) easier.
-
- if not update.first_update:
- update.report.updated_at = update.created_at
- else:
- update.report.updated_at = update.report.created_at
- update.report.is_confirmed = True
-
- update.report.save()
-
# redirect to report
return( HttpResponseRedirect( update.report.get_absolute_url() ))
View
4 templates/_fixmystreetmap.js
@@ -19,8 +19,8 @@ function url_for_geodata(geodata)
function html_for_no_results()
{
var html = "<div id='error-msg'>";
- html += '<p>{% trans "Sorry, we couldn\'t find the address you entered. Please try again with another intersection, address or postal code, or add the name of the city to the end of the search."%}</p>';
- html += '</div>'
+ html += "<p>{% trans "Sorry, we couldn\'t find the address you entered. Please try again with another intersection, address or postal code, or add the name of the city to the end of the search."%}</p>";
+ html += "</div>";
jQuery("#error").html(html).fadeIn(1000);
}
View
5 urls.py
@@ -98,7 +98,8 @@
urlpatterns += patterns('',
- url('^accounts/register/$', register, { 'form_class': FMSNewRegistrationForm,
+ url('^accounts/register/$', register, {'SSL':SSL_ON ,
+ 'form_class': FMSNewRegistrationForm,
'extra_context':
{ 'providers': SUPPORTED_SOCIAL_PROVIDERS } },name='registration_register'),
url('^accounts/login/$', auth_views.login, {'SSL':SSL_ON,
@@ -116,7 +117,7 @@
url(r'^accounts/home/', 'home',{ 'SSL':SSL_ON }, name='account_home'),
url(r'^accounts/edit/', 'edit', {'SSL':SSL_ON }, name='account_edit'),
(r'^accounts/login/error/$', 'error'),
- url(r'^accounts/complete/(?P<backend>[^/]+)/$', 'socialauth_complete', name='socialauth_complete'),
+ url(r'^accounts/complete/(?P<backend>[^/]+)/$', 'socialauth_complete', {'SSL':SSL_ON }, name='socialauth_complete'),
)
urlpatterns += patterns('',

0 comments on commit 51ceb99

Please sign in to comment.