work on comments (in annotation form, list in annotation detail, post in annotation detail isn't working yet)
authordurandn
Tue, 28 Jun 2016 15:54:05 +0200
changeset 38 b319db67a5f3
parent 37 aed809b3a075
child 39 cd1fab6934a9
work on comments (in annotation form, list in annotation detail, post in annotation detail isn't working yet)
requirements.txt
src/iconolab/forms/annotations.py
src/iconolab/forms/comments.py
src/iconolab/settings/__init__.py
src/iconolab/settings/dev.py.tmpl
src/iconolab/templates/iconolab/change_annotation.html
src/iconolab/templates/iconolab/detail_annotation.html
src/iconolab/urls.py
src/iconolab/views.py
--- a/requirements.txt	Tue Jun 28 15:50:57 2016 +0200
+++ b/requirements.txt	Tue Jun 28 15:54:05 2016 +0200
@@ -1,16 +1,16 @@
-diff-match-patch==20121119
-Django==1.9.7
-django-model-utils==2.5
-django-notifications-hq==1.1
-django-reversion==2.0.1
-django-reversion-compare==0.6.2
-django-revision==0.1.6
-djangorestframework==3.3.3
-gitdb==0.6.4
-GitPython==2.0.5
-jsonfield==1.0.3
-Pillow==3.2.0
-psycopg2==2.6.1
-pytz==2016.4
-smmap==0.9.0
-sorl-thumbnail==12.4a1
+diff-match-patch==20121119
+Django==1.9.7
+django-comments-xtd==1.5.3
+django-contrib-comments==1.7.1
+django-model-utils==2.5
+django-notifications-hq==1.1
+djangorestframework==3.3.3
+gitdb==0.6.4
+GitPython==2.0.5
+jsonfield==1.0.3
+Pillow==3.2.0
+psycopg2==2.6.1
+pytz==2016.4
+six==1.10.0
+smmap==0.9.0
+sorl-thumbnail==12.4a1
--- a/src/iconolab/forms/annotations.py	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/forms/annotations.py	Tue Jun 28 15:54:05 2016 +0200
@@ -1,19 +1,20 @@
-from django.forms import ModelForm, TypedMultipleChoiceField, HiddenInput, CharField, MultipleHiddenInput
+from django import forms
 from iconolab.models import AnnotationRevision
 import json
 
-class MultipleTagsField(TypedMultipleChoiceField):
+class MultipleTagsField(forms.TypedMultipleChoiceField):
     pass
 
-class AnnotationRevisionForm(ModelForm):
-    tags = CharField()
+class AnnotationRevisionForm(forms.ModelForm):
+    tags = forms.CharField()
+    comment = forms.CharField(widget=forms.Textarea)
     
     class Meta:
         model = AnnotationRevision
         fields = ('title', 'description', 'fragment')
         widgets = {
-            'fragment': HiddenInput(),
-            'tags': HiddenInput()
+            'fragment': forms.HiddenInput(),
+            'tags': forms.HiddenInput()
         }
     
     def tags_json(self):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/iconolab/forms/comments.py	Tue Jun 28 15:54:05 2016 +0200
@@ -0,0 +1,16 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+from django_comments_xtd.forms import XtdCommentForm
+from iconolab.models import MetaCategory
+
+class IconolabCommentForm(XtdCommentForm):
+    metacategories = forms.ModelMultipleChoiceField(queryset=MetaCategory.objects.all())
+    
+    def __init__(self, *args, **kwargs):
+        super(IconolabCommentForm, self).__init__(*args, **kwargs)
+        self.fields.pop('email')
+    
+    def get_comment_create_data(self):
+        data = super(IconolabCommentForm, self).get_comment_create_data()
+        data.update({'metacategories': self.cleaned_data['metacategories']})
+        return data
\ No newline at end of file
--- a/src/iconolab/settings/__init__.py	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/settings/__init__.py	Tue Jun 28 15:54:05 2016 +0200
@@ -53,15 +53,19 @@
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
-    'notifications',
-    'reversion',
-    #'reversion_compare',
+    'django.contrib.sites',
+    'django_comments',
+    'django_comments_xtd',
     'iconolab.apps.IconolabApp',
     'sorl.thumbnail',
 ]
 
-ADD_REVERSION_ADMIN=True
+COMMENTS_APP = "django_comments_xtd"
+COMMENTS_XTD_MODEL = "iconolab.models.IconolabComment"
+COMMENTS_XTD_FORM_CLASS = 'iconolab.forms.comments.IconolabCommentForm'
+COMMENTS_XTD_MAX_THREAD_LEVEL = 0
 
+SITE_ID = 1
 
 MIDDLEWARE_CLASSES = [
     'django.middleware.security.SecurityMiddleware',
--- a/src/iconolab/settings/dev.py.tmpl	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/settings/dev.py.tmpl	Tue Jun 28 15:54:05 2016 +0200
@@ -49,6 +49,9 @@
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'django.contrib.sites',
+    'django_comments',
+    'django_comments_xtd',
     'notifications',
     'reversion',
     'reversion_compare',
@@ -56,8 +59,11 @@
     'sorl.thumbnail',
 ]
 
-ADD_REVERSION_ADMIN=True
+COMMENTS_APP = "django_comments_xtd"
+COMMENTS_XTD_MODEL = "iconolab.models.IconolabComment"
+COMMENTS_XTD_MAX_THREAD_LEVEL = 0
 
+SITE_ID = 1
 
 MIDDLEWARE_CLASSES = [
     'django.middleware.security.SecurityMiddleware',
--- a/src/iconolab/templates/iconolab/change_annotation.html	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/templates/iconolab/change_annotation.html	Tue Jun 28 15:54:05 2016 +0200
@@ -92,6 +92,12 @@
                     <fieldset class="form-group">
                       <label class="control-label" for="id_{{ form.tags.name }}">{{ form.tags.label }}</label>
                     </fieldset>
+                    <fieldset class="form-group">
+                      <label class="control-label" for="id_{{ form.comment.name }}">{{ form.comment.label }}</label>
+                      <textarea class="form-control"
+                        name="{{ form.comment.name }}"
+                        id="id_{{ form.comment.name }}" ></textarea>
+                    </fieldset>
         			<input v-model="normalizePath" type="hidden" name="fragment"></input>
                     <input id="tags_input" type="hidden" name="tags" value="{{ form.tags_json }}"></input>
         			<button type="submit" class="save btn btn-default">Enregister</button>
--- a/src/iconolab/templates/iconolab/detail_annotation.html	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/templates/iconolab/detail_annotation.html	Tue Jun 28 15:54:05 2016 +0200
@@ -1,13 +1,13 @@
 {% extends 'iconolab_base.html' %}
 
+{% load i18n %}
 {% load staticfiles %}
-
+{% load comments %}
+{% load comments_xtd %}
 {% load thumbnail %}
-
 {% load iconolab_tags %}
 
 {% block content %}
-	
 	<a href="{% url 'image_detail' collection_name image_guid %}"><i class="fa fa-list"></i> Revoir l'image </a>
 	<div id="annotation-wrapper" class="row" style="border: 1px solid gray">
 		
@@ -33,11 +33,48 @@
 				<p> <strong>Title:</strong> {{ annotation.current_revision.title }}</p>
 				<p> <strong>Description:</strong> {{ annotation.current_revision.description }}</p>
 				<p> <strong>Tags:</strong> {{ annotation.current_revision.tags }}</p>
-				<p> <strong>Fragment:</strong> {{ annotation.current_revision.fragment }}</p>
 
 				<a href="{% url 'annotation_edit' collection_name image_guid annotation_guid  %}">Editer</a> | 
 				<a href="{% url 'annotation_edit' collection_name image_guid annotation_guid %}">Proposer une révision</a>
 			</div>
 		</div>
+        <div class='col-md-12'>
+          {% get_comment_list for annotation as comment_list %}
+          <dl class="dl-horizontal" id="comments">
+            {% for comment in comment_list %}
+              <dt id="c{{ comment.id }}">
+                {{ comment.name }}
+              </dt>
+              <dd>
+                <p>{{ comment.comment }} </p>
+                <div style="font-size:0.9em">{{ comment.submit_date }}&nbsp;-&nbsp;
+                {% if comment.allow_thread %}&nbsp;-&nbsp;<a href="{{ comment.get_reply_url }}">{% trans "Reply" %}</a>{% endif %}</div>
+              </dd>
+            {% endfor %}
+          </dl>
+        </div>
+        <div class='col-md-12'>
+        {% if user.is_authenticated %}
+        {% get_comment_form for annotation as comment_form %}
+        <form class="form" action="{% comment_form_target %}" method="POST">
+          {% csrf_token %}
+          {{ comment_form.content_type }}
+          {{ comment_form.object_pk }}
+          {{ comment_form.timestamp }}
+          {{ comment_form.security_hash }}
+          <input type="hidden" name="next" value="{% url 'annotation_create' collection_name image_guid %}">
+          <fieldset class="form-group">
+            <label class="control-label" for="id_{{ comment_form.name.name }}">Commenting as {{ user.username }}</label>
+          </fieldset>
+          <fieldset class="form-group">
+            <label class="control-label" for="id_{{ comment_form.comment.name }}">{{ comment_form.comment.label }}</label>
+            <textarea class="form-control"
+              name="{{ comment_form.comment.name }}"
+              id="id_{{ comment_form.comment.name }}" ></textarea>
+          </fieldset>
+          <button type="submit" class="save btn btn-default">Poster</button>
+        </form>
+        {% endif %}
+        <div></div>
 	</div>
 {% endblock %}
--- a/src/iconolab/urls.py	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/urls.py	Tue Jun 28 15:54:05 2016 +0200
@@ -31,8 +31,10 @@
     url(r'^collections/(?P<collection_name>[a-z]+)/images/(?P<image_guid>[^/]+)/annotations/(?P<annotation_guid>[^/]+)/detail$', views.ShowAnnotationView.as_view(), name='annotation_detail'),
     url(r'^collections/(?P<collection_name>[a-z]+)/images/(?P<image_guid>[^/]+)/annotations/(?P<annotation_guid>[^/]+)/edit$', login_required(views.EditAnnotationView.as_view()), name='annotation_edit'),
     url(r'^collections/(?P<collection_name>[a-z]+)/images/(?P<image_guid>[^/]+)/annotations/(?P<annotation_guid>[^/]+)/revisions/(?P<revision_guid>[0-9]+)/merge$', login_required(views.MergeProposalView.as_view()), name='annotation_merge'),
+    url(r'errors/404', views.NotFoundErrorView.as_view(), name="404error"),
     url(r'^rest', include('restapi.urls')),
     url(r'^account/', include('iconolab.auth.urls', namespace='account')),
+    url(r'^comments/', include('django_comments_xtd.urls'))
 ]
 
 urlpatterns += staticfiles_urlpatterns()
--- a/src/iconolab/views.py	Tue Jun 28 15:50:57 2016 +0200
+++ b/src/iconolab/views.py	Tue Jun 28 15:54:05 2016 +0200
@@ -4,19 +4,31 @@
 from django.views.generic import View, RedirectView
 from django.views.generic.base import ContextMixin
 from django.core.urlresolvers import reverse
-from iconolab.models import Annotation, Collection, Image
+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.forms.annotations import AnnotationRevisionForm
-import json
+import json, datetime
 
 def make_tags_json(annotation_revision):
 	final_list = []
-	for tagging_info in annotation_revision.tagginginfo_set.all():
-		final_list.append({
-			"tag_label": tagging_info.tag.label,
-			"tag_link": tagging_info.tag.link,
-			"accuracy": tagging_info.accuracy,
-			"relevancy": tagging_info.relevancy
-		})
+	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):
@@ -124,6 +136,8 @@
 		collection, image, annotation = self.check_kwargs(kwargs)
 		self.check_kwargs(kwargs)
 		context = self.get_context_data(**kwargs)
+		context["collection"] = collection
+		context["image"] = collection
 		context["annotation"] = annotation
 		return render(request, 'iconolab/detail_annotation.html', context)
 
@@ -174,10 +188,21 @@
 			revision_description = annotation_form.cleaned_data["description"]
 			revision_fragment = annotation_form.cleaned_data["fragment"]
 			revision_tags_json = annotation_form.cleaned_data["tags"]
-			annotation.make_new_revision(revision_author, revision_title, revision_description, revision_fragment, revision_tags_json)
+			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 = revision_comment,
+				revision = new_revision,
+				content_type = ContentType.objects.get(app_label="iconolab", model="annotation"),
+				content_object = annotation,
+				site = Site.objects.get(id=settings.SITE_ID),
+				object_pk = 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': annotation_guid}))(request)
 		print(annotation_form.errors)
-		return HttpResponse("wow")
+		return HttpResponse("wow errors")
 		
 class MergeProposalView(View):