Beispiel #1
0
 def test_rack_form_clean__bound(self):
     data = self.data.copy()
     form = RackForm(data, {})
     form.is_bound = True
     form.instance.source = Source()
     form.is_valid()
     self.assertEqual(form.cleaned_data, form.clean())
Beispiel #2
0
def newrack_form(request):
    if request.method == 'POST':
        _maybe_geocode(request)
        form = RackForm(request.POST,request.FILES)
        if form.is_valid():
            new_rack = form.save()
            # create steps status for new rack suggestion
            size_up = Steps(step_rack=new_rack,name="size-up",status="todo")
            size_up.save()
            photo_status = Steps(step_rack=new_rack,name="photo",status='todo')
            photo_status.save()
            statement = Steps(step_rack=new_rack,name="statement",status='todo')
            statement.save()
            message = '''
            Thank you for your suggestion! Racks can take six months
            or more for the DOT to install, but we\'ll be in touch
            about its progress. <a href="/rack/new/">Add another
            rack</a> or continue to see other suggestions.
            '''
            flash(message, request)
            return HttpResponseRedirect('/verify/')
        else:
            flash('Please correct the following errors.', request)
    else:
        form = RackForm()
    return render_to_response('newrack.html', { 
            'form': form,
           },
           context_instance=RequestContext(request, processors=[user_context])) 
Beispiel #3
0
def rack_edit(request,rack_id):
    rack = get_object_or_404(Rack, id=rack_id)
    form = RackForm()
    if rack.locked:
        flash_error('This rack is locked and cannot be edited.', request)
    elif request.method == 'POST':
        # For now, preserve the original creator.
        request.POST[u'email'] = rack.email
        request.POST[u'user'] = rack.user
        _preprocess_rack_form(request.POST)
        form = RackForm(request.POST, request.FILES, instance=rack)
        if form.is_valid():
            x = form.save()
            flash('Your changes have been saved.', request)
            return HttpResponseRedirect(
                urlresolvers.reverse(rack_edit, kwargs={'rack_id': rack.id}))
        else:
            flash_error('Please correct the following errors.', request)

    # Who created this rack?
    if rack.user == request.user.username or rack.email == request.user.email:
        creator = rack.user
    else:
        # Don't reveal email address to other users.
        # Instead show a username, or a truncated address if submitted
        # anonymously.
        creator = rack.user or "anonymous" # (%s@...)" % (rack.email.split('@', 1)[0]))
    return render_to_response('update_rack.html',
          {"rack": rack,
           "form": form ,
           "creator": creator,
           },
          context_instance=RequestContext(request))
Beispiel #4
0
    def test_rack_form_clean__unbound_with_email(self):
        data = self.data.copy()
        form = RackForm(data, {})
        form = RackForm(data, {})
        form.is_bound = False
        form.cleaned_data = data
        form._errors = {}

        self.assertEqual(form.cleaned_data, form.clean())
Beispiel #5
0
    def test_rack_form_bound__verified(self):
        data = self.data.copy()
        data['verify_access'] = 'on'
        data['verify_objects'] = 'on'
        data['verify_surface'] = 'on'

        form = RackForm(data, {})
        form.is_bound = True
        self.assertEqual(form.is_valid(), True)
        self.assertEqual(form.cleaned_data['status'], 'verified')
Beispiel #6
0
    def test_rack_form_clean__unbound_with_no_email_or_source(self):
        data = self.data.copy()
        del(data['email'])
        form = RackForm(data, {})
        form.is_bound = False
        form.cleaned_data = data
        form._errors = {}

        # Can't validate without an email or source
        self.assertRaises(ValidationError, form.clean)
Beispiel #7
0
def _newrack(data, files):
    form = RackForm(data, files)
    new_rack = None
    message = ''
    if form.is_valid():
        new_rack = form.save()
        message = '''
        Thank you for your suggestion! Racks can take six months
        or more for the DOT to install, but we\'ll be in touch
        about its progress.
        '''
    return {'rack': new_rack, 'message': message, 'form': form,
            'errors': form.errors}
Beispiel #8
0
def _newrack(data, files):
    """Thin wrapper around RackForm, returning a dict with some
    info useful for UIs."""
    form = RackForm(data, files)
    new_rack = None
    message = ''
    if form.is_valid():
        new_rack = form.save()
        message = '''
        Thank you for your suggestion! Racks can take six months
        or more for the DOT to install, but we\'ll be in touch
        about its progress.
        '''
    return {'rack': new_rack, 'message': message, 'form': form,
            'errors': form.errors}
Beispiel #9
0
def rack_edit(request,rack_id):
    rack = Rack.objects.get(id=rack_id)
    if request.method == 'POST':
        _maybe_geocode(request)
        form = RackForm(request.POST, request.FILES, instance=rack)
        if form.is_valid():
            form.save()
            flash('Your changes have been saved.', request)
            return HttpResponseRedirect('/rack/%s/edit' % rack.id)
        else:
            flash('Please correct the following errors.', request)
    else: 
        form = RackForm()
    return render_to_response('update_rack.html', 
          {"rack": rack,
           "form": form },
          context_instance=RequestContext(request, processors=[user_context]))
Beispiel #10
0
    def test_rack_form_clean_photo(self):
        from fixcity.exif_utils import get_exif_info
        from PIL import Image
        import os.path

        data = self.data.copy()
        # Jump through a few hoops to simulate a real upload.
        HERE = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(HERE, 'files', 'test_exif.jpg')
        content = open(path).read()
        photofile = TemporaryUploadedFile('test_exif.jpg', 'image/jpeg',
                                          len(content), None)
        photofile.write(content)
        photofile.seek(0)
        # Okay, now we have something like a file upload.
        data['photo'] = photofile
        form = RackForm(data, {'photo': photofile})
        self.assert_(form.is_valid())
        # Make sure it doesn't have a bad rotation.        
        self.assertEqual({},
                         get_exif_info(Image.open(photofile.temporary_file_path())))
Beispiel #11
0
    def test_rack_form_clean__unbound_with_source(self):
        data = self.data.copy()
        del(data['email'])
        form = RackForm(data, {})
        form.is_bound = False
        form.cleaned_data = data
        form._errors = {}
        self.assertRaises(ValidationError, form.clean)

        # A source is sufficient.
        form.cleaned_data['source'] = 'something'
        self.assertEqual(form.cleaned_data, form.clean())
Beispiel #12
0
 def test_rack_form_bound__unverified(self):
     data = self.data.copy()
     form = RackForm(data, {})
     form.is_bound = True
     self.assertEqual(form.is_valid(), True)
     self.assertEqual(form.cleaned_data['status'], 'new')