src/iconolab/views.py
changeset 42 51257e2701d9
parent 38 b319db67a5f3
child 43 ccc449ef6f16
--- a/src/iconolab/views.py	Wed Jun 29 14:54:16 2016 +0200
+++ b/src/iconolab/views.py	Wed Jun 29 14:56:27 2016 +0200
@@ -1,35 +1,23 @@
+from django.apps import apps
 from django.shortcuts import HttpResponse, get_object_or_404, render
 from django.http import Http404
 from django.contrib.auth.decorators import login_required
 from django.views.generic import View, RedirectView
 from django.views.generic.base import ContextMixin
+from django.views.decorators.csrf import csrf_protect
+from django.views.decorators.http import require_POST
 from django.core.urlresolvers import reverse
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.sites.models import Site
 from django.conf import settings
-from iconolab.models import Annotation, Collection, Image, IconolabComment
+from iconolab.models import Annotation, AnnotationRevision, Collection, Image, IconolabComment
 from iconolab.forms.annotations import AnnotationRevisionForm
-import json, datetime
+import datetime
+import django_comments
+from django_comments import signals
+from django_comments.views.utils import next_redirect, confirmation_view
 
-def make_tags_json(annotation_revision):
-	final_list = []
-	for tagging_info in annotation_revision.tagginginfo_set.select_related("tag").all():
-		if tagging_info.tag.is_internal():
-			final_list.append({
-				"tag_label": tagging_info.tag.label,
-				"tag_link": tagging_info.tag.link,
-				"accuracy": tagging_info.accuracy,
-				"relevancy": tagging_info.relevancy
-			})
-		else:
-			#import label from dbpedia
-			final_list.append({
-				"tag_label": "",
-				"tag_link": tagging_info.tag.link,
-				"accuracy": tagging_info.accuracy,
-				"relevancy": tagging_info.relevancy
-			})
-	return json.dumps(final_list) 
 
 class GlobalHomepageView(View):
 	def get(self, request, *args, **kwargs):
@@ -65,7 +53,7 @@
 		context["collection"] = collection
 		context["image"] = image
 		return render(request, 'iconolab/detail_image.html', context);
-    
+	
 class CreateAnnotationView(View, ContextMixin):
 	
 	def get_context_data(self, **kwargs):
@@ -105,6 +93,17 @@
 			fragment = annotation_form.cleaned_data["fragment"]
 			tags_json = annotation_form.cleaned_data["tags"]
 			new_annotation = Annotation.objects.create_annotation(author, image, title=title, description=description, fragment=fragment, tags_json=tags_json)
+			revision_comment = annotation_form.cleaned_data["comment"]
+			IconolabComment.objects.create(
+				comment = revision_comment,
+				revision = new_annotation.current_revision,
+				content_type = ContentType.objects.get(app_label="iconolab", model="annotation"),
+				content_object = new_annotation,
+				site = Site.objects.get(id=settings.SITE_ID),
+				object_pk = new_annotation.id,
+				user = request.user,
+				user_name = request.user.username
+			)
 			return RedirectView.as_view(url=reverse("annotation_detail", kwargs={'collection_name': collection_name, 'image_guid': image_guid, 'annotation_guid': new_annotation.annotation_guid}))(request)
 
 
@@ -137,8 +136,9 @@
 		self.check_kwargs(kwargs)
 		context = self.get_context_data(**kwargs)
 		context["collection"] = collection
-		context["image"] = collection
+		context["image"] = image
 		context["annotation"] = annotation
+		context["tags_data"] = annotation.current_revision.get_tags_json()
 		return render(request, 'iconolab/detail_annotation.html', context)
 
 
@@ -173,7 +173,7 @@
 		context["image"] = image
 		context["annotation"] = annotation
 		context["form"] = annotation_form
-		context["tags_data"] = make_tags_json(annotation.current_revision)
+		context["tags_data"] = annotation.current_revision.get_tags_json()
 		return render(request, 'iconolab/change_annotation.html', context) 
 	
 	def post(self, request, *args, **kwargs):
@@ -190,7 +190,7 @@
 			revision_tags_json = annotation_form.cleaned_data["tags"]
 			new_revision = annotation.make_new_revision(revision_author, revision_title, revision_description, revision_fragment, revision_tags_json)
 			revision_comment = annotation_form.cleaned_data["comment"]
-			IconolabComment.objects.create(
+			comment = IconolabComment.objects.create(
 				comment = revision_comment,
 				revision = new_revision,
 				content_type = ContentType.objects.get(app_label="iconolab", model="annotation"),
@@ -200,9 +200,55 @@
 				user = request.user,
 				user_name = request.user.username
 			)
+			print(comment.revision)
+			print(new_revision.creation_comment.first())
 			return RedirectView.as_view(url=reverse("annotation_detail", kwargs={'collection_name': collection_name, 'image_guid': image_guid, 'annotation_guid': annotation_guid}))(request)
 		print(annotation_form.errors)
 		return HttpResponse("wow errors")
+
+
+class ShowRevisionView(View, ContextMixin):
+	
+	def get_context_data(self, **kwargs):
+		context = super(ShowRevisionView, self).get_context_data(**kwargs)
+		context["collection_name"] = self.kwargs.get("collection_name", "")
+		context["image_guid"] = self.kwargs.get("image_guid", "")
+		context["annotation_guid"] = self.kwargs.get("annotation_guid", "")
+		context["revision_guid"] = self.kwargs.get("revision_guid", "")
+		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_guid=kwargs.get("image_guid", ""))
+		except Image.DoesNotExist:
+			return RedirectView.as_view(url=reverse("404error"))
+		try:
+			annotation = Annotation.objects.select_related("current_revision").get(annotation_guid=kwargs.get("annotation_guid", ""))
+		except Annotation.DoesNotExist:
+			return RedirectView.as_view(url=reverse("404error"))
+		try:
+			revision = AnnotationRevision.objects.select_related("parent_revision").get(revision_guid=kwargs.get("revision_guid", ""))
+		except AnnotationRevision.DoesNotExist:
+			return RedirectView.as_view(url=reverse("404error"))
+		return collection, image, annotation, revision
+	
+	def get(self, request, *args, **kwargs):
+		collection, image, annotation, revision = self.check_kwargs(kwargs)
+		self.check_kwargs(kwargs)
+		context = self.get_context_data(**kwargs)
+		context["collection"] = collection
+		context["image"] = image
+		context["annotation"] = annotation
+		context["revision"] = revision
+		context["tags_data"] = revision.get_tags_json()
+		print(revision.creation_comment)
+		context["comment"] = revision.creation_comment.first()
+		return render(request, 'iconolab/detail_revision.html', context)
+
 		
 class MergeProposalView(View):
 	
@@ -218,4 +264,96 @@
 class NotFoundErrorView(View):
 	def get(self, request, *args, **kwargs):
 		# Handle image display here
-		pass
\ No newline at end of file
+		pass
+	
+@csrf_protect
+@require_POST
+def post_comment_iconolab(request, next=None, using=None):
+	"""
+	Post a comment.
+	HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
+	errors a preview template, ``comments/preview.html``, will be rendered.
+	"""
+	# Fill out some initial data fields from an authenticated user, if present
+	data = request.POST.copy()
+	if request.user.is_authenticated():
+		if not data.get('name', ''):
+			data["name"] = request.user.get_full_name() or request.user.get_username()
+		if not data.get('email', ''):
+			data["email"] = request.user.email
+
+	# Look up the object we're trying to comment about
+	ctype = data.get("content_type")
+	object_pk = data.get("object_pk")
+	if ctype is None or object_pk is None:
+		return CommentPostBadRequest("Missing content_type or object_pk field.")
+	try:
+		model = apps.get_model(*ctype.split(".", 1))
+		target = model._default_manager.using(using).get(pk=object_pk)
+	except TypeError:
+		return CommentPostBadRequest(
+			"Invalid content_type value: %r" % escape(ctype))
+	except AttributeError:
+		return CommentPostBadRequest(
+			"The given content-type %r does not resolve to a valid model." % escape(ctype))
+	except ObjectDoesNotExist:
+		return CommentPostBadRequest(
+			"No object matching content-type %r and object PK %r exists." % (
+				escape(ctype), escape(object_pk)))
+	except (ValueError, ValidationError) as e:
+		return CommentPostBadRequest(
+			"Attempting go get content-type %r and object PK %r exists raised %s" % (
+				escape(ctype), escape(object_pk), e.__class__.__name__))
+
+	# Do we want to preview the comment?
+	preview = "preview" in data
+
+	# Construct the comment form
+	form = django_comments.get_form()(target, data=data)
+
+	# Check security information
+	if form.security_errors():
+		return CommentPostBadRequest(
+			"The comment form failed security verification: %s" % escape(str(form.security_errors())))
+
+	# If there are errors or if we requested a preview show the comment
+	if form.errors:
+		return render(request, "iconolab/detail_annotation.html", {
+				"comment_form": form,
+				"next": data.get("next", next),
+				"annotation": target,
+				"annotation_guid": target.annotation_guid,
+				"image_guid": target.image.image_guid,
+				"collection_name": target.image.item.collection.name,
+				"tags_data": target.current_revision.get_tags_json()
+			},
+		)
+
+	# Otherwise create the comment
+	comment = form.get_comment_object()
+	comment.ip_address = request.META.get("REMOTE_ADDR", None)
+	if request.user.is_authenticated():
+		comment.user = request.user
+
+	# Signal that the comment is about to be saved
+	responses = signals.comment_will_be_posted.send(
+		sender=comment.__class__,
+		comment=comment,
+		request=request
+	)
+
+	for (receiver, response) in responses:
+		if response is False:
+			return CommentPostBadRequest(
+				"comment_will_be_posted receiver %r killed the comment" % receiver.__name__)
+
+	# Save the comment and signal that it was saved
+	comment.save()
+	signals.comment_was_posted.send(
+		sender=comment.__class__,
+		comment=comment,
+		request=request
+	)
+
+	return next_redirect(request, fallback=next or 'comments-comment-done',
+						 c=comment._get_pk_val())
\ No newline at end of file