functionality: creation of new annotation from scratch
authordurandn
Wed, 22 Jun 2016 17:53:40 +0200
changeset 30 7ff344b4bf6d
parent 29 b3768547ad3d
child 31 e34b70a00488
child 33 f9d4c9a63e4e
functionality: creation of new annotation from scratch
src/iconolab/models.py
src/iconolab/templates/iconolab/change_annotation.html
src/iconolab/templates/iconolab/detail_image.html
src/iconolab/views.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)
--- 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 @@
 			
 			<div class="col-md-8">
 				<div v-el:image id="iconolab-image-wrapper">
-					{% thumbnail annotation.image.media "x800" crop="center" as im %}
+					{% thumbnail image.media "x800" crop="center" as im %}
 	    				<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
-	    				<path class="image-path" d="{{ annotation.current_revision.fragment }}"></path>
+	    				<path class="image-path" d="{% if annotation %}{{ annotation.current_revision.fragment }}{% endif %}"></path>
 					{% endthumbnail %}
 				</div>
 			</div>
@@ -40,7 +40,7 @@
 			
 			<div class="col-xs-6">
 				<div class="small-image-wrapper" style="position: relative">
-					{% thumbnail annotation.image.media "x300" crop="center" as im %}
+					{% thumbnail image.media "x300" crop="center" as im %}
 						<img v-el:small-image @click="showEditor" src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
 
 						<svg width="{{ im.width }}" height="{{ im.height }}" version="1.1" style="position:absolute; top:0px; left: 0px">
@@ -71,9 +71,20 @@
 			</div>
 
 			<div class='col-xs-6' style="">
-				 <form method="POST" class="post-form">
+				 <form class="form" action="{% if annotation %}{% url 'annotation_edit' collection_name image_ref annotation_guid %}{% else %}{% url 'annotation_create' collection_name image_ref %}{% endif %}" method="POST">
 				 	{% csrf_token %}
-        			{{ form.as_p }}
+                    <fieldset class="form-group">
+                      <label class="control-label" for="id_{{ form.title.name }}">{{ form.title.label }}</label>
+                      <input type="text" class="form-control"
+                        name="{{ form.title.name }}"
+                        id="id_{{ form.title.name }}" value="{% if annotation %}{{ annotation.current_revision.title }}{% endif %}">
+                    </fieldset>
+                    <fieldset class="form-group">
+                      <label class="control-label" for="id_{{ form.description.name }}">{{ form.description.label }}</label>
+                      <textarea class="form-control"
+                        name="{{ form.description.name }}"
+                        id="id_{{ form.description.name }}" >{% if annotation %}{{ annotation.current_revision.description }}{% endif %}</textarea>
+                    </fieldset>
         			<input v-model="normalizePath" type="hidden" name="fragment"></input>
         			<button type="submit" class="save btn btn-default">Enregister</button>
     			</form>
--- 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 %}
+<p>detail_image for {{image_ref}} in the collection {{collection_name}}</p>
+<a href="{% url 'annotation_create' collection_name image_ref %}">Create Annotation</a>
+
+{% endblock %}
\ No newline at end of file
--- 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"]