Skip to content

Commit a9411d0

Browse files
Implemented visualization of event on the place page (if there are upcoming events) and event details page.
1 parent afc544d commit a9411d0

File tree

8 files changed

+318
-120
lines changed

8 files changed

+318
-120
lines changed

.idea/workspace.xml

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

engine/export_data_to_django.py

+49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csv
2+
from datetime import datetime, date
23

34
import psycopg2
45

@@ -8,6 +9,7 @@ def execute_sql(s):
89
with con:
910
cur = con.cursor()
1011
cur.execute(s)
12+
return cur
1113

1214

1315
def single_quote(s):
@@ -137,8 +139,55 @@ def import_distanze():
137139
continue
138140

139141

142+
def import_eventi():
143+
with open('data/eventi.csv') as csvfile:
144+
file_reader = csv.reader(csvfile, delimiter=',')
145+
for row in file_reader:
146+
event_id = int(row[0])
147+
title = row[2]
148+
place_name = row[4]
149+
date_from = row[5]
150+
date_to = row[6]
151+
location = row[7]
152+
description = row[23]
153+
popularity = row[24]
154+
155+
date_converted = datetime.strptime(date_from.split(" ")[0], '%Y-%m-%d').date()
156+
date_from_events_query = date(2018, 8, 1)
157+
158+
if place_name != '' and date_converted > date_from_events_query:
159+
try:
160+
query_django_places = (
161+
'''SELECT "placeId" FROM recommender_webapp_place p WHERE p.name LIKE '{}' AND p.location LIKE '{}' '''.format(
162+
single_quote(place_name),
163+
single_quote(location)
164+
))
165+
cur = execute_sql(query_django_places)
166+
result = cur.fetchone()
167+
if result:
168+
place_id = result[0]
169+
170+
sql = (
171+
'''INSERT INTO recommender_webapp_event VALUES ({},\'{}\',\'{}\',\'{}\',{},\'{}\',\'{}\',{})'''.format(
172+
event_id,
173+
single_quote(title),
174+
single_quote(date_from),
175+
single_quote(date_to),
176+
popularity,
177+
single_quote(description),
178+
single_quote(location),
179+
place_id
180+
))
181+
execute_sql(sql)
182+
print("Inserted event " + title + " for location: " + location)
183+
except Exception as e:
184+
print("Error Insert event " + title + " for location: " + location)
185+
continue
186+
187+
140188
if __name__ == "__main__":
141189
import_places()
142190
import_sample_ratings()
143191
import_comuni()
144192
import_distanze()
193+
import_eventi()

pugliaeventi/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
url(r'^my_places/$', views.my_places, name='my_places'),
3333
url(r'^my_profile/$', views.user_profile, name='my_profile'),
3434
path('place/<int:place_id>/', views.place_details, name='place_details'),
35+
path('event/<int:event_id>/', views.event_details, name='event_details'),
3536
path('ratings/<int:place_id>/<int:mood>/<int:companionship>/', views.add_rating_config, name='add_rating_conf'),
3637

3738
# place it at whatever base url you like

recommender_webapp/models.py

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
DEFAULT_RATING = 3
13+
DEFAULT_EVENT_PLACE = 0
1314

1415

1516
class UserManager(BaseUserManager):
@@ -128,6 +129,21 @@ def __str__(self):
128129
return str(self.placeId) + '|' + self.name + '|' + self.location
129130

130131

132+
class Event(models.Model):
133+
eventId = models.IntegerField(primary_key=True)
134+
title = models.TextField(blank=False)
135+
location = models.CharField(max_length=40, blank=True)
136+
place = models.ForeignKey(Place, on_delete=models.PROTECT, blank=False, default=DEFAULT_EVENT_PLACE)
137+
date_from = models.DateField(blank=False)
138+
date_to = models.DateField(blank=False)
139+
popularity = models.IntegerField()
140+
description = models.TextField()
141+
142+
def __str__(self):
143+
return str(self.eventId) + '|' + self.title + '|' + self.place_name + '|' \
144+
+ self.date_from.strftime('%d/%m/%Y') + '|' + self.date_to.strftime('%d/%m/%Y')
145+
146+
131147
class Mood(ChoiceEnum):
132148
__order__ = 'angry joyful sad'
133149
angry = 1

recommender_webapp/views.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from datetime import datetime
23

34
from django.contrib.auth import authenticate, login, logout
45
from django.shortcuts import render, redirect
@@ -9,7 +10,7 @@
910
from recommender_webapp.common import lightfm_manager, constant
1011
from recommender_webapp.forms import ProfileForm, UserRegisterForm, SearchNearPlacesForm, DistanceRange, \
1112
SearchRecommendationForm, FullProfileForm
12-
from recommender_webapp.models import Comune, Distanza, Place, Mood, Companionship, Rating, User, Profile
13+
from recommender_webapp.models import Comune, Distanza, Place, Mood, Companionship, Rating, User, Profile, Event
1314

1415

1516
@csrf_protect
@@ -271,6 +272,8 @@ def place_details(request, place_id):
271272
else:
272273
context['form'] = search_rec_form
273274

275+
events = Event.objects.filter(place=place, date_from__gte=datetime.today().date())
276+
274277
if search_rec_form.is_valid():
275278
mood = int(search_rec_form.cleaned_data.get('mood'))
276279
companionship = int(search_rec_form.cleaned_data.get('companionship'))
@@ -288,6 +291,7 @@ def place_details(request, place_id):
288291
context = {
289292
'place': place,
290293
'labels': labels,
294+
'events': events,
291295
'form': search_rec_form,
292296
'ratings': place_ratings,
293297
'email': request.user.email,
@@ -297,6 +301,21 @@ def place_details(request, place_id):
297301
return render(request, 'place.html', context)
298302

299303

304+
def event_details(request, event_id):
305+
context = {}
306+
if request.user.is_authenticated:
307+
308+
event = Event.objects.get(eventId=event_id)
309+
310+
context = {
311+
'event': event,
312+
'email': request.user.email,
313+
'email_splitted': request.user.email.split('@')[0],
314+
}
315+
316+
return render(request, 'event.html', context)
317+
318+
300319
def user_profile(request):
301320

302321
context = {

templates/event.html

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{% load crispy_forms_tags %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
<title>Pugliaeventi Rec.</title>
8+
9+
{% include 'head_css_js.html' %}
10+
11+
</head>
12+
<body>
13+
<div class="container">
14+
15+
{% include 'header.html' %}
16+
17+
<h1 class="my-4">{{ event.title }}</h1>
18+
19+
<!-- Portfolio Item Row -->
20+
<div class="row">
21+
22+
<div class="col-md-4">
23+
<img class="img-fluid" src="http://placehold.it/750x500" alt="">
24+
</div>
25+
26+
<div class="col-md-8">
27+
<h5 class="my-3">Location: <small>{{ event.location }}</small></h5>
28+
<h5 class="my-3">Date from: <small>{{ event.date_from }}</small></h5>
29+
<h5 class="my-3">Date to: <small>{{ event.date_to }}</small></h5>
30+
<h5 class="my-3">Popularity: <small>{{ event.popularity }}</small></h5>
31+
<h5 class="my-3">Description:</h5>
32+
{{ event.description | safe }}
33+
</div>
34+
35+
</div>
36+
<!-- /.row -->
37+
38+
<hr />
39+
40+
</div>
41+
42+
</body>
43+
</html>

templates/header.html

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<div class="collapse navbar-collapse" id="exCollapsingNavbar">
77
<ul class="navbar-nav mr-auto">
88
<li class="nav-item"><a href="/close_places" class="nav-link">Places</a></li>
9-
<li class="nav-item"><a href="#" class="nav-link">Events</a></li>
109
</ul>
1110

1211
{% if user.is_authenticated %}

templates/place.html

+32-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h1 class="my-4">{{ place.name }}</h1>
2424
</div>
2525

2626
<div class="col-md-4">
27-
<h3 class="my-3">Località: <small>{{ place.location }}</small></h3>
27+
<h3 class="my-3">Location: <small>{{ place.location }}</small></h3>
2828
<h3 class="my-3">Labels: </h3>
2929
<ul>
3030
{% for label in labels %}
@@ -66,7 +66,37 @@ <h6 class="my-3"><strong>You could add this place to your profile:</strong></h6>
6666
</div>
6767
<!-- /.row -->
6868

69-
<hr />
69+
<hr />
70+
71+
{% if events %}
72+
73+
<h5 class="text-center">Upcoming events</h5>
74+
<hr />
75+
76+
<div class="table-responsive-sm">
77+
78+
<table class="table table-striped">
79+
<thead>
80+
<tr>
81+
<th scope="col">Name</th>
82+
<th scope="col">Date from</th>
83+
<th scope="col">Date to</th>
84+
</tr>
85+
</thead>
86+
<tbody>
87+
{% for event in events %}
88+
<tr>
89+
<th scope="row"><a href="{% url 'event_details' event_id=event.eventId %}" >{{ event.title }}</a></th>
90+
<td>{{ event.date_from }}</td>
91+
<td>{{ event.date_to }}</td>
92+
</tr>
93+
{% endfor %}
94+
</tbody>
95+
</table>
96+
97+
</div>
98+
99+
{% endif %}
70100

71101
<div class="modal fade bd-example-modal-lg" data-backdrop="static" data-keyboard="false" tabindex="-1">
72102
<div class="modal-dialog modal-sm">

0 commit comments

Comments
 (0)