Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.

Changed html forms into django forms #55

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions scanapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'}),
}
58 changes: 36 additions & 22 deletions scanapp/templates/scanapp/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,52 @@ <h1>
</div>
</div>
</form>
<form id="register-form" action="{% url 'signup' %}" method="post"
role="form" style="display: none;" onsubmit="return myFunction()">
{% csrf_token %}
<form id="register-form" method="post" action="." style="display: none;"
>


<center>

<div class="form-group">
<p><strong>Username :</strong></p>
{{ form.username }}

</div>
<div class="form-group">
<p><strong>Email Address :</strong></p>
{{ form.email }}

</div>
<div class="form-group">
<p><strong>Password :</strong></p>
{{ form.password1 }}
</div>
<div class="form-group">
<p><strong>Confirm Password :</strong></p>
{{ form.password2 }}

</div>

</center>


<br>

<div class="form-group">
<input type="text" name="username" id="username" tabindex="1"
class="form-control"
placeholder="Username" value="">
</div>
<div class="form-group">
<input type="email" name="email" id="email" tabindex="1" class="form-control"
placeholder="Email Address" value="">
</div>
<div class="form-group">
<input type="password" name="password" id="password" tabindex="2"
class="form-control" placeholder="Password" onkeyup='check();'>
</div>
<div class="form-group">
<input type="password" name="confirm-password" id="confirm-password"
tabindex="2"
class="form-control" placeholder="Confirm Password" onkeyup='check();'>
</div>
<div class="form-group">

<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="register-submit" id="register-submit"
tabindex="4" class="form-control btn btn-register"
value="Register Now">
value="Register">
</div>
</div>
</div>


</form>


</div>

</div>
Expand Down
6 changes: 3 additions & 3 deletions scanapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -41,9 +42,8 @@
url(r'^localscan/', LocalUploadView.as_view(), name='localuploadview'),
url(r'^urlscan/', URLFormViewCelery.as_view(), name='urlceleryformview'),
url(r'^resultscan/(?P<pk>[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")),

]
54 changes: 35 additions & 19 deletions scanapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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}
)