src/iconolab/forms/annotations.py
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 20 Feb 2017 17:29:55 +0100
changeset 323 55c024fc7c60
parent 187 456d13962c0d
permissions -rw-r--r--
Roughly implement annotation navigator.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
b319db67a5f3 work on comments (in annotation form, list in annotation detail, post in annotation detail isn't working yet)
durandn
parents: 35
diff changeset
     1
from django import forms
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
     2
from iconolab.models import AnnotationRevision
187
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
     3
import json, logging
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
     4
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
     5
logger = logging.getLogger(__name__)
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
     6
38
b319db67a5f3 work on comments (in annotation form, list in annotation detail, post in annotation detail isn't working yet)
durandn
parents: 35
diff changeset
     7
class MultipleTagsField(forms.TypedMultipleChoiceField):
33
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
     8
    pass
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
     9
38
b319db67a5f3 work on comments (in annotation form, list in annotation detail, post in annotation detail isn't working yet)
durandn
parents: 35
diff changeset
    10
class AnnotationRevisionForm(forms.ModelForm):
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    11
    description = forms.CharField(required=False)
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    12
    fragment = forms.CharField(required=False)
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    13
    tags = forms.CharField(widget=forms.Textarea, required=False)
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    14
    comment = forms.CharField(widget=forms.Textarea, required=False)
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    15
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
    16
    class Meta:
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
    17
        model = AnnotationRevision
35
31d4f0e86723 added backend json handling for tags (properly tested/corrected) + corrected templates to replace image_ref with image_guid
durandn
parents: 33
diff changeset
    18
        fields = ('title', 'description', 'fragment')
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents:
diff changeset
    19
        widgets = {
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    20
            'fragment': forms.Textarea(),
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    21
            'tags': forms.Textarea()
33
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    22
        }
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    23
187
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    24
    def __init__(self, *args, **kwargs):
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    25
        super(AnnotationRevisionForm, self).__init__(*args, **kwargs)
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    26
187
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    27
    def clean(self, *args, **kwargs):
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    28
        cleaned_data = super(AnnotationRevisionForm, self).clean(*args, **kwargs)
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    29
        if not (cleaned_data.get("title", "") or cleaned_data.get("description", "") or json.loads(cleaned_data.get("tags", "[]"))):
456d13962c0d Reworked mandatory fields to submit an annotation (only one of title, description or tags is required) #19
durandn
parents: 38
diff changeset
    30
            raise forms.ValidationError("Au moins un de ces champs doit être renseigné: description, titre ou mot-clé", code="missing_fields")
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    31
33
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    32
    def tags_json(self):
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    33
        if self.instance:
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    34
            tags_list = []
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    35
            for tag_info in self.instance.tagginginfo_set.all():
35
31d4f0e86723 added backend json handling for tags (properly tested/corrected) + corrected templates to replace image_ref with image_guid
durandn
parents: 33
diff changeset
    36
                tags_list.append({
33
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    37
                    'tag_input': tag_info.tag.link,
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    38
                    'relevancy': tag_info.relevancy,
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    39
                    'accuracy': tag_info.accuracy
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    40
                })
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    41
            return json.dumps(tags_list)
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 24
diff changeset
    42
        else:
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 187
diff changeset
    43
            return '[]'