# HG changeset patch # User durandn # Date 1466610820 -7200 # Node ID 7ff344b4bf6d2a78ca83e230042168a5e53008b7 # Parent b3768547ad3d5659d16e307dfe6533b8f6c1a6a3 functionality: creation of new annotation from scratch diff -r b3768547ad3d -r 7ff344b4bf6d src/iconolab/models.py --- a/src/iconolab/models.py Tue Jun 21 17:15:54 2016 +0200 +++ b/src/iconolab/models.py Wed Jun 22 17:53:40 2016 +0200 @@ -80,7 +80,7 @@ # Call Annotation.objects.create_annotation to initialize a new Annotation with its associated AnnotationStats and initial AnnotationRevision @transaction.atomic - def create_annotation(self, image, author, title='', description='', fragment='', tags=None): + def create_annotation(self, author, image, title='', description='', fragment='', tags=None): # Create annotation object new_annotation = Annotation( image=image, @@ -106,6 +106,7 @@ new_annotation_stats.save() new_annotation.stats = new_annotation_stats new_annotation.save() + return new_annotation class AnnotationStats(models.Model): annotation = models.OneToOneField('Annotation', related_name='stats', blank=False, null=False) diff -r b3768547ad3d -r 7ff344b4bf6d src/iconolab/templates/iconolab/change_annotation.html --- a/src/iconolab/templates/iconolab/change_annotation.html Tue Jun 21 17:15:54 2016 +0200 +++ b/src/iconolab/templates/iconolab/change_annotation.html Wed Jun 22 17:53:40 2016 +0200 @@ -27,9 +27,9 @@
- {% thumbnail annotation.image.media "x800" crop="center" as im %} + {% thumbnail image.media "x800" crop="center" as im %} - + {% endthumbnail %}
@@ -40,7 +40,7 @@
- {% thumbnail annotation.image.media "x300" crop="center" as im %} + {% thumbnail image.media "x300" crop="center" as im %} @@ -71,9 +71,20 @@
-
+ {% csrf_token %} - {{ form.as_p }} +
+ + +
+
+ + +
diff -r b3768547ad3d -r 7ff344b4bf6d src/iconolab/templates/iconolab/detail_image.html --- a/src/iconolab/templates/iconolab/detail_image.html Tue Jun 21 17:15:54 2016 +0200 +++ b/src/iconolab/templates/iconolab/detail_image.html Wed Jun 22 17:53:40 2016 +0200 @@ -6,4 +6,8 @@ {% load iconolab_tags %} -{% block content %}detailimage{% endblock %} \ No newline at end of file +{% block content %} +

detail_image for {{image_ref}} in the collection {{collection_name}}

+Create Annotation + +{% endblock %} \ No newline at end of file diff -r b3768547ad3d -r 7ff344b4bf6d src/iconolab/views.py --- a/src/iconolab/views.py Tue Jun 21 17:15:54 2016 +0200 +++ b/src/iconolab/views.py Wed Jun 22 17:53:40 2016 +0200 @@ -1,5 +1,5 @@ from django.shortcuts import HttpResponse, get_object_or_404, render -from iconolab.models import Annotation, Collection +from iconolab.models import Annotation, Collection, Image from django.http import Http404 from django.contrib.auth.decorators import login_required from django.views.generic import View, RedirectView @@ -21,12 +21,13 @@ return render(request, 'iconolab/collection_home.html', context); -class ShowImageView(View): +class ShowImageView(View, ContextMixin): + def get(self, request, *args, **kwargs): - context = super(CollectionHomepageView, self).get_context_data(**kwargs) + context = super(ShowImageView, self).get_context_data(**kwargs) context["collection_name"] = self.kwargs.get("collection_name", "") context["image_ref"] = self.kwargs.get("image_ref", "") - return render(request, 'iconolab/collection_home.html', context); + return render(request, 'iconolab/detail_image.html', context); class ShowAnnotationView(View, ContextMixin): @@ -44,29 +45,62 @@ except Collection.DoesNotExist: return RedirectView.as_view(url=reverse("404error")) try: + image = Image.objects.get(image_ref=kwargs.get("image_ref", "")) + except Image.DoesNotExist: + return RedirectView.as_view(url=reverse("404error")) + try: annotation = Annotation.objects.get(annotation_guid=kwargs.get("annotation_guid", "")) except Annotation.DoesNotExist: return RedirectView.as_view(url=reverse("404error")) - return collection, annotation + return collection, image, annotation def get(self, request, *args, **kwargs): - collection, annotation = self.check_kwargs(kwargs) + collection, image, annotation = self.check_kwargs(kwargs) self.check_kwargs(kwargs) context = self.get_context_data(**kwargs) context["annotation"] = annotation - print(annotation) return render(request, 'iconolab/detail_annotation.html', context) -class CreateAnnotationView(View): +class CreateAnnotationView(View, ContextMixin): + + def get_context_data(self, **kwargs): + context = super(CreateAnnotationView, self).get_context_data(**kwargs) + context["collection_name"] = self.kwargs.get("collection_name", "") + context["image_ref"] = self.kwargs.get("image_ref", "") + return context + + def check_kwargs(self, kwargs): + try: + collection = Collection.objects.get(name=kwargs.get("collection_name", "")) + except Collection.DoesNotExist: + return RedirectView.as_view(url=reverse("404error")) + try: + image = Image.objects.get(image_ref=kwargs.get("image_ref", "")) + except Image.DoesNotExist: + return RedirectView.as_view(url=reverse("404error")) + return collection, image def get(self, request, *args, **kwargs): - print("test") - return render(request, 'iconolab/change_annotation.html') + collection, image = self.check_kwargs(kwargs) + annotation_form = AnnotationRevisionForm() + context = self.get_context_data(**kwargs) + context["image"] = image + context["form"] = annotation_form + return render(request, 'iconolab/change_annotation.html', context) def post(self, request, *args, **kwargs): - # Handle annotation submit here - pass + collection, image = self.check_kwargs(kwargs) + collection_name = kwargs["collection_name"] + image_ref = kwargs["image_ref"] + annotation_form = AnnotationRevisionForm(request.POST) + if annotation_form.is_valid(): + author = request.user + title = annotation_form.cleaned_data["title"] + description = annotation_form.cleaned_data["description"] + fragment = annotation_form.cleaned_data["fragment"] + new_annotation = Annotation.objects.create_annotation(author, image, title=title, description=description, fragment=fragment, tags=None) + return RedirectView.as_view(url=reverse("annotation_detail", kwargs={'collection_name': collection_name, 'image_ref': image_ref, 'annotation_guid': new_annotation.annotation_guid}))(request) class EditAnnotationView(View, ContextMixin): @@ -83,21 +117,26 @@ except Collection.DoesNotExist: return RedirectView.as_view(url=reverse("404error")) try: + image = Image.objects.get(image_ref=kwargs.get("image_ref", "")) + except Image.DoesNotExist: + return RedirectView.as_view(url=reverse("404error")) + try: annotation = Annotation.objects.get(annotation_guid=kwargs.get("annotation_guid", "")) except Annotation.DoesNotExist: return RedirectView.as_view(url=reverse("404error")) - return collection, annotation + return collection, image, annotation def get(self, request, *args, **kwargs): - collection, annotation = self.check_kwargs(kwargs) + collection, image, annotation = self.check_kwargs(kwargs) annotation_form = AnnotationRevisionForm() context = self.get_context_data(**kwargs) + context["image"] = image context["annotation"] = annotation context["form"] = annotation_form return render(request, 'iconolab/change_annotation.html', context) def post(self, request, *args, **kwargs): - collection, annotation = self.check_kwargs(kwargs) + collection, image, annotation = self.check_kwargs(kwargs) collection_name = kwargs["collection_name"] image_ref = kwargs["image_ref"] annotation_guid = kwargs["annotation_guid"]