예제 #1
0
def newHop(request):
    title = "New Hop"
    if request.method=='POST':
        form = HopForm(request.POST)
        if form.is_valid():
            secretID = form.cleaned_data['secretID']
            oid = request.POST['object']
            object = get_object_or_404(Object, id=int(oid))
            if secretID == object.secretID:
                country = form.cleaned_data['country']
                city = form.cleaned_data['city']
                zipcode = form.cleaned_data['zipcode']
                street = form.cleaned_data['street']
                housenumber = form.cleaned_data['housenumber']
                longitude, latitude = util.getLonLat(country, city, zipcode, street, housenumber)
                l = Location(
                            country = country,
                            city = city,
                            zipcode = zipcode,
                            street = street,
                            housenumber = housenumber,
                            longitude = longitude,
                            latitude = latitude
                )
                l.save()
                note = form.cleaned_data['note']
                hop = Hop(objectID=object, user=request.user, location=l, note=note, submissionDate=datetime.datetime.now())
                hop.save()
                return redirect("/object/{0}".format(object.id))
            else:
                message = "The SecretID wasn't correct. Are you shure you have the object?"
        else:
            message = "The entered information wasn't valid. please check it."
        t = loader.get_template('main/newhop.html')
        context = util.generateContext(request, contextType = 'RequestContext', form=form, message=message, title=title)
        return HttpResponseServerError(t.render(context))
    else:
        form = HopForm()
        if request.method == 'GET':
            try:
                object = request.GET['obj']
            except:
                return redirect("/")
    context = util.generateContext(request, contextType = 'RequestContext', form = form, obj=object, title=title)
    return render_to_response('main/newhop.html', context)
예제 #2
0
def newObject(request):
    title = "New Object"
    if request.method == 'POST': # If the form has been submitted...
        form = ObjectForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            country = form.cleaned_data['country']
            city = form.cleaned_data['city']
            zipcode = form.cleaned_data['zipcode']
            street = form.cleaned_data['street']
            housenumber = form.cleaned_data['housenumber']
            lon, lat = util.getLonLat(country, city, zipcode, street, housenumber)
            l = Location(
                        country = country,
                        city = city,
                        zipcode = zipcode,
                        street = street,
                        housenumber = housenumber,
                        longitude = lon,
                        latitude = lat
            )
            l.save()
            randID = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(10))
            o = Object(
                        name = form.cleaned_data['name'],
                        description = form.cleaned_data['description'],
                        secretID = randID,
                        submissionDate = datetime.datetime.now(),
                        sender = request.user,
                        destination = l
            )
            o.save()
            return HttpResponseRedirect('/object/{0}'.format(o.id)) # Redirect after POST
        else:
            message = "The entered information wasn't valid. please check it."
            t = loader.get_template('main/newobject.html')
            ontext = util.generateContext(request, contextType = 'RequestContext', form=form, message=message, title=title)
            return HttpResponseServerError(t.render(context))
    else:
        form = ObjectForm() # An unbound form
    context = util.generateContext(request, contextType = 'RequestContext', form = form, title=title)
    return render_to_response('main/newobject.html', context)