diff --git a/events/admin.py b/events/admin.py index 400fd89..5956063 100644 --- a/events/admin.py +++ b/events/admin.py @@ -5,7 +5,7 @@ from django.utils.safestring import mark_safe from .models.locale import Language, Continent, Country, SPR, City from .models.profiles import UserProfile, Organization, Team, Member, Category, Topic from .models.search import Searchable -from .models.events import Place, Event, Attendee +from .models.events import Place, Event, EventPhoto, Attendee admin.site.register(Language) admin.site.register(Continent) @@ -59,6 +59,8 @@ class EventAdmin(admin.ModelAdmin): attendee_count.short_description = 'Attendees' admin.site.register(Event, EventAdmin) +admin.site.register(EventPhoto) + class MemberAdmin(admin.ModelAdmin): list_display = ('__str__', 'role') list_filter = ('role', 'team') diff --git a/events/forms.py b/events/forms.py index 4ea1808..9fe6f79 100644 --- a/events/forms.py +++ b/events/forms.py @@ -5,7 +5,7 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User from .models.profiles import Team, UserProfile -from .models.events import Event, Place +from .models.events import Event, Place, EventPhoto from datetime import time from time import strptime, strftime @@ -196,6 +196,11 @@ class NewTeamEventForm(forms.ModelForm): class DeleteEventForm(forms.Form): confirm = forms.BooleanField(label="Yes, delete event", required=True) +class UploadEventPhotoForm(forms.ModelForm): + class Meta: + model = EventPhoto + fields = ['src', 'title', 'caption'] + class NewPlaceForm(forms.ModelForm): class Meta: model = Place diff --git a/events/migrations/0017_add_event_images.py b/events/migrations/0017_add_event_images.py new file mode 100644 index 0000000..7ef9692 --- /dev/null +++ b/events/migrations/0017_add_event_images.py @@ -0,0 +1,31 @@ +# Generated by Django 2.0 on 2018-03-17 20:50 + +from django.db import migrations, models +import django.db.models.deletion +import imagekit.models.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0016_make_searchable_uri_unique'), + ] + + operations = [ + migrations.CreateModel( + name='EventPhoto', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=256)), + ('caption', models.TextField(blank=True, null=True)), + ('src', models.ImageField(upload_to='event_photos')), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='events.Event')), + ], + ), + migrations.AlterField( + model_name='userprofile', + name='avatar', + field=imagekit.models.fields.ProcessedImageField(default='http://www.gravatar.com/avatar/00000000000000000000000000000000.jpg?d=mm', upload_to='avatars', verbose_name='Photo Image'), + preserve_default=False, + ), + ] diff --git a/events/models/events.py b/events/models/events.py index 90cc63f..a0d96f4 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -6,6 +6,9 @@ from django.shortcuts import reverse from rest_framework import serializers +from imagekit.models import ImageSpecField +from imagekit.processors import ResizeToFill + from .locale import * from .profiles import * from .search import * @@ -183,4 +186,13 @@ class Attendee(models.Model): def __str__(self): return "%s at %s" % (self.user, self.event) - + +class EventPhoto(models.Model): + event = models.ForeignKey(Event, related_name='photos', on_delete=models.CASCADE) + title = models.CharField(max_length=256) + caption = models.TextField(null=True, blank=True) + src = models.ImageField(verbose_name=_('Photo'), upload_to='event_photos') + thumbnail = ImageSpecField(source='src', + processors=[ResizeToFill(250, 187)], + format='JPEG', + options={'quality': 60}) diff --git a/get_together/templates/get_together/events/add_photo.html b/get_together/templates/get_together/events/add_photo.html new file mode 100644 index 0000000..847e974 --- /dev/null +++ b/get_together/templates/get_together/events/add_photo.html @@ -0,0 +1,14 @@ +{% extends "get_together/base.html" %} + +{% block content %} +

Add photo to {{event.name}}

+
+{% csrf_token %} + +{{ photo_form }} +
+
+ +
+{% endblock %} + diff --git a/get_together/templates/get_together/events/show_event.html b/get_together/templates/get_together/events/show_event.html index 3b8f223..a1e993a 100644 --- a/get_together/templates/get_together/events/show_event.html +++ b/get_together/templates/get_together/events/show_event.html @@ -12,6 +12,7 @@ {% endblock %} {% block styles %} + {% endblock %} @@ -62,6 +65,31 @@
+
+
+

Photos


+
+
+ {% for photo in event.photos.all %} +
+
+ + {{photo.title}} + +
+
+

{{photo.title}}

+
+ {{photo.caption}} +
+
+
+ {% endfor %} +
+
+ +
+
diff --git a/get_together/urls.py b/get_together/urls.py index ce5d3b6..4fc6d63 100644 --- a/get_together/urls.py +++ b/get_together/urls.py @@ -62,6 +62,7 @@ urlpatterns = [ path('events//+delete/', views.delete_event, name='delete-event'), path('events//+add_place/', views.add_place_to_event, name='add-place'), path('events//+share/', views.share_event, name='share-event'), + path('events//+photo/', views.add_event_photo, name='add-event-photo'), path('events///', views.show_event, name='show-event'), path('places/', views.places_list, name='places'), diff --git a/get_together/views/events.py b/get_together/views/events.py index 5b89875..3104d1d 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -6,9 +6,9 @@ from django.shortcuts import render, redirect from django.http import HttpResponse, JsonResponse from events.models.profiles import Team, UserProfile, Member -from events.forms import TeamEventForm, NewTeamEventForm, DeleteEventForm, NewPlaceForm +from events.forms import TeamEventForm, NewTeamEventForm, DeleteEventForm, NewPlaceForm, UploadEventPhotoForm -from events.models.events import Event, Place, Attendee +from events.models.events import Event, EventPhoto, Place, Attendee import datetime import simplejson @@ -62,6 +62,35 @@ def create_event(request, team_id): else: return redirect('home') +def add_event_photo(request, event_id): + event = Event.objects.get(id=event_id) + if not request.user.profile.can_edit_event(event): + messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.')) + return redirect(event.get_absolute_url()) + + if request.method == 'GET': + form = UploadEventPhotoForm() + + context = { + 'event': event, + 'photo_form': form, + } + return render(request, 'get_together/events/add_photo.html', context) + elif request.method == 'POST': + new_photo = EventPhoto(event=event) + form = UploadEventPhotoForm(request.POST, request.FILES, instance=new_photo) + if form.is_valid(): + form.save() + return redirect(event.get_absolute_url()) + else: + context = { + 'event': event, + 'photo_form': form, + } + return render(request, 'get_together/events/add_photo.html', context) + else: + return redirect('home') + def add_place_to_event(request, event_id): event = Event.objects.get(id=event_id) if not request.user.profile.can_edit_event(event):