diff --git a/scanapp/forms.py b/scanapp/forms.py
index 4a589ecc..43dbd7a9 100644
--- a/scanapp/forms.py
+++ b/scanapp/forms.py
@@ -21,7 +21,10 @@
# scancode-server is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-server/ for support and download.
+import re
from django import forms
+from django.contrib.auth.models import User
+from django.utils.translation import ugettext_lazy as _
class URLScanForm(forms.Form):
@@ -30,3 +33,33 @@ class URLScanForm(forms.Form):
class LocalScanForm(forms.Form):
upload_from_local = forms.FileField(label='Upload from Local')
+
+
+class RegistrationForm(forms.Form):
+ username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)),
+ label=_("Username"), error_messages={
+ 'invalid': _("This value must contain only letters, numbers and underscores.")})
+ email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address"))
+ password1 = forms.CharField(
+ widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
+ password2 = forms.CharField(
+ widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)),
+ label=_("Password (again)"))
+
+ def clean_username(self):
+ try:
+ user = User.objects.get(username__iexact=self.cleaned_data['username'])
+ except User.DoesNotExist:
+ return self.cleaned_data['username']
+ raise forms.ValidationError(_("The username already exists. Please try a different one."))
+
+ def clean(self):
+ if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
+ if self.cleaned_data['password1'] != self.cleaned_data['password2']:
+ raise forms.ValidationError(_("The two password fields did not match."))
+ return self.cleaned_data
+
+ class Meta:
+ widgets = {
+ 'myfield': forms.TextInput(attrs={'class': 'form-group'}),
+ }
diff --git a/scanapp/templates/scanapp/login.html b/scanapp/templates/scanapp/login.html
index f42a7621..217d6e8f 100644
--- a/scanapp/templates/scanapp/login.html
+++ b/scanapp/templates/scanapp/login.html
@@ -87,38 +87,52 @@
-
+
+
diff --git a/scanapp/urls.py b/scanapp/urls.py
index 04025a76..275b4603 100644
--- a/scanapp/urls.py
+++ b/scanapp/urls.py
@@ -30,8 +30,9 @@
from scanapp.views import URLFormViewCelery
from rest_framework.authtoken import views as rest_views
-from scanapp.views import RegisterView
+# from scanapp.views import RegisterView
from scanapp.views import LoginView
+from scanapp.views import *
from . import views
@@ -41,9 +42,8 @@
url(r'^localscan/', LocalUploadView.as_view(), name='localuploadview'),
url(r'^urlscan/', URLFormViewCelery.as_view(), name='urlceleryformview'),
url(r'^resultscan/(?P[0-9]+)', ScanResults.as_view(), name='resultview'),
- url(r'^login/', LoginView.as_view(), name='login'),
+ url(r'^login/', csrf_exempt(RegisterView.as_view()), name='login'),
url(r'^signin/', rest_views.obtain_auth_token, name='signin'),
- url(r'^signup/?', RegisterView.as_view(), name='signup'),
url(r'^home/', TemplateView.as_view(template_name="scanapp/home.html")),
]
diff --git a/scanapp/views.py b/scanapp/views.py
index dde3218c..9c3d0eaf 100644
--- a/scanapp/views.py
+++ b/scanapp/views.py
@@ -45,6 +45,12 @@
from django.db import transaction
from django.contrib.auth.models import User
from django.views import View
+from scanapp.forms import *
+from django.template import RequestContext
+from django.shortcuts import render_to_response
+from django.views.decorators.csrf import csrf_protect
+from django.utils.decorators import method_decorator
+
class LocalUploadView(FormView):
template_name = 'scanapp/localupload.html'
@@ -126,28 +132,38 @@ def get(self, request, *args, **kwargs):
return render(request, 'scanapp/scanresults.html', context={'result': result})
+
class LoginView(TemplateView):
template_name = "scanapp/login.html"
-class RegisterView(View):
+class RegisterView(FormView):
def post(self, request):
- if request.POST.get('password') != request.POST.get('confirm-password'):
- return HttpResponse("Unauthorized- Password doesn't match", status=401)
-
- with transaction.atomic():
- user = User.objects.create_user(
- username=request.POST.get('username'),
- password=request.POST.get('password'),
- email=request.POST.get('email')
- )
-
- user.save()
-
- return HttpResponse(
- json.dumps(
- {
- 'token': Token.objects.get(user=user).key
- }
- )
+ if request.method == 'POST':
+ form = RegistrationForm(request.POST)
+ if form.is_valid():
+ user = User.objects.create_user(
+ username=form.cleaned_data['username'],
+ password=form.cleaned_data['password1'],
+ email=form.cleaned_data['email']
+ )
+ return HttpResponse(
+ json.dumps(
+ {
+ 'token': Token.objects.get(user=user).key
+ }
+ )
+ )
+
+ else:
+ form = RegistrationForm()
+
+ variables = RequestContext(request, {
+ 'form': form
+ })
+
+ return render_to_response(
+ 'scanapp/login.html',
+ context={'form': form}
)
+