Skip to content

Commit b7485f1

Browse files
Implemented lookup of locations on user registration
1 parent bf20bc2 commit b7485f1

File tree

10 files changed

+324
-185
lines changed

10 files changed

+324
-185
lines changed

.idea/workspace.xml

+240-166
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pugliaeventi/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'django.contrib.staticfiles',
4040
'recommender_webapp.apps.RecommenderConfig',
4141
'crispy_forms',
42+
'ajax_select',
4243
]
4344

4445
CRISPY_TEMPLATE_PACK = 'bootstrap3'

pugliaeventi/urls.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515
"""
1616
from django.conf.urls import url
1717
from django.contrib import admin
18-
from django.shortcuts import render
18+
from django.urls import include
1919

2020
from recommender_webapp import views
2121
from pugliaeventi import views as main_views
22+
from ajax_select import urls as ajax_select_urls
2223

2324
urlpatterns = [
2425
url(r'^$', main_views.index, name='index'),
2526
url(r'^admin/', admin.site.urls),
2627
url(r'^login/', views.user_login, name='login'),
2728
url(r'^logout/', views.user_logout, name='logout'),
2829
url(r'^register/', views.user_signup, name='register'),
30+
31+
# place it at whatever base url you like
32+
url(r'^ajax_select/', include(ajax_select_urls)),
33+
34+
#path(r'^ajax/load-cities/', views.load_cities, name='ajax_load_cities'),
2935
#url(r'.*', lambda request: render(request, '404.html'), name='404')
3036

3137
]

pugliaeventi/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ def index(request):
1919
user_data['empathy'] = user.profile.empathy
2020

2121
return render(request, 'base.html', user_data)
22+
23+
24+

recommender_webapp/forms.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
from django import forms
22
from django.forms.widgets import PasswordInput
3+
from ajax_select.fields import AutoCompleteField
34

4-
from recommender_webapp.models import User, Profile
5-
6-
7-
class UserForm(forms.ModelForm):
8-
class Meta:
9-
model = User
10-
fields = ('email', 'password', 'first_name', 'last_name')
5+
from recommender_webapp.models import User, Profile, Comune
116

127

138
class ProfileForm(forms.ModelForm):
149
class Meta:
1510
model = Profile
1611
fields = ('location',)
1712

13+
location = AutoCompleteField('cities')
14+
15+
def clean(self, *args, **kwargs):
16+
location = self.cleaned_data.get('location')
17+
location_found = Comune.objects.filter(nome__iexact=location)
18+
if not location_found.exists():
19+
raise forms.ValidationError("Location not found")
20+
1821

1922
class UserRegisterForm(forms.ModelForm):
2023
email = forms.EmailField(label='Email address')
2124
email2 = forms.EmailField(label='Confirm Email')
2225
password = forms.CharField(widget=PasswordInput)
26+
password2 = forms.CharField(widget=PasswordInput, label='Confirm Password')
2327

2428
class Meta:
2529
model = User
2630
fields = [
2731
'email',
2832
'email2',
2933
'password',
34+
'password2',
3035
'first_name',
3136
'last_name'
3237
]
3338

3439
def clean(self, *args, **kwargs):
3540
email = self.cleaned_data.get('email')
3641
email2 = self.cleaned_data.get('email2')
42+
password = self.cleaned_data.get('password')
43+
password2 = self.cleaned_data.get('password2')
3744
if email != email2:
3845
raise forms.ValidationError("Emails must match")
46+
if password != password2:
47+
raise forms.ValidationError("Passwords must match")
3948

4049
email_qs = User.objects.filter(email=email)
4150
if email_qs.exists():

recommender_webapp/lookups.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from ajax_select import register, LookupChannel
2+
from .models import Comune
3+
4+
5+
@register('cities')
6+
class TagsLookup(LookupChannel):
7+
8+
model = Comune
9+
10+
def check_auth(self, request):
11+
return True
12+
13+
def get_query(self, q, request):
14+
return self.model.objects.filter(nome__icontains=q).order_by('nome')
15+
16+
def get_result(self, obj):
17+
""" result is the simple text that is the completion of what the person typed """
18+
return obj.nome
19+
20+
def format_match(self, obj):
21+
""" (HTML) formatted item for display in the dropdown """
22+
return self.format_item_display(obj)
23+
24+
def format_item_display(self, item):
25+
""" (HTML) formatted item for displaying item in the selected deck area """
26+
return u"<span class='tag'>%s</span>" % item.nome

recommender_webapp/views.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.views.decorators.csrf import csrf_protect
77

88
from recommender_webapp.forms import ProfileForm, UserRegisterForm
9+
from recommender_webapp.models import Comune
910

1011

1112
@csrf_protect
@@ -41,13 +42,14 @@ def user_signup(request):
4142
location = profile_form.cleaned_data.get('location')
4243
user.set_password(password)
4344
user.save()
44-
user.profile.location = location
45+
46+
location_found = Comune.objects.filter(nome__iexact=location)
47+
user.profile.location = location_found.first().nome
4548
user.save()
49+
4650
new_user = authenticate(username=user.email, password=password)
4751
login(request, new_user)
48-
4952
return redirect('/')
50-
# return render(request, 'base.html', {'message': 'Registered successfully, congratulations! Please login'})
5153

5254
context = {
5355
'user_form': user_form,

templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
{% load static %}
99
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet" id="bootstrap-css">
1010
<link href="{% static "css/navbar.css" %}" rel="stylesheet">
11-
<script src="{% static "js/jquery-1.10.2.min.js.download" %}"></script>
12-
<script src="{% static "js/bootstrap.min.js.download" %}"></script>
11+
<script src="{% static "js/jquery-1.10.2.min.js" %}"></script>
12+
<script src="{% static "js/bootstrap.min.js" %}"></script>
1313
<link href="{% static "css/font-awesome.min.css" %}" rel="stylesheet">
1414
<link href="{% static "css/bootstrap-social.css" %}" rel="stylesheet">
1515
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<option value="">---------</option>
2+
{% for city in cities %}
3+
<option value="{{ city.name }}">{{ city.name }}</option>
4+
{% endfor %}

templates/signup.html

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{% load crispy_forms_tags %}
2-
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
3-
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
42
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
3+
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
4+
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
55
<!------ Include the above in your HEAD tag ---------->
66

77
<!DOCTYPE html>
88
<html lang="en">
99
<head>
1010
<meta name="viewport" content="width=device-width, initial-scale=1">
11-
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
1211

1312
<{% load static %}
14-
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet" id="bootstrap-css">
1513
<link href="{% static "css/font-awesome.min.css" %}" rel="stylesheet">
14+
<link href="{% static "css/selectstyle.css" %}" rel="stylesheet">
15+
<script src="{% static "js/selectstyle.js" %}"></script>
1616

1717
<!-- Google Fonts -->
1818
<link href='https://fonts.googleapis.com/css?family=Passion+One' rel='stylesheet' type='text/css'>
@@ -99,6 +99,7 @@ <h1 class="title">Welcome to Join Us!</h1>
9999

100100
{% csrf_token %}
101101
{{ user_form|crispy}}
102+
{{ profile_form.media }}
102103
{{ profile_form|crispy }}
103104

104105
<div class="form-group ">
@@ -109,7 +110,20 @@ <h1 class="title">Welcome to Join Us!</h1>
109110
</div>
110111
</div>
111112
</div>
112-
113-
<script type="text/javascript" src="assets/js/bootstrap.js"></script>
114113
</body>
114+
115+
<script>
116+
117+
// })
118+
/*var url = $("#personForm").attr("data-cities-url"); // get the url of the `load_cities` view
119+
120+
$.ajax({ // initialize an AJAX request
121+
url: url, // set the url of the request (= localhost:8000/hr/ajax/load-cities/)
122+
123+
success: function (data) { // `data` is the return of the `load_cities` view function
124+
$("#id_location").html(data); // replace the contents of the city input with the data that came from the server
125+
}
126+
});*/
127+
</script>
128+
115129
</html>

0 commit comments

Comments
 (0)