Commit 5d5e2334 authored by Jean Michel Rouly's avatar Jean Michel Rouly
Browse files

Parse URL registration form.

parent 9aa64644
from go.models import URL
from go.forms import URLForm
from datetime import timedelta
from django.http import Http404
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
# Homepage view.
@login_required
def index(request):
return render(request, 'index.html', {
url_form = URLForm() # unbound form
errors = []
if request.method == 'POST':
url_form = URLForm( request.POST ) # bind dat form
if url_form.is_valid():
url = url_form.save(commit=False)
url.owner = request.user
expires = url_form.cleaned_data['expires']
if expires == URLForm.DAY:
url.expires = timezone.now() + timedelta(days=1)
elif expires == URLForm.WEEK:
url.expires = timezone.now() + timedelta(weeks=1)
elif expires == URLForm.MONTH:
url.expires = timezone.now() + timedelta(weeks=3)
else:
pass # leave the field NULL
url.full_clean()
url.save()
return redirect('success', url.target, url.short)
return render(request, 'index.html', {
'form': url_form,
},
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment