web/ldt/text/views.py
changeset 24 9e19b7ae3780
parent 22 83b28fc0d731
parent 21 1a061f244254
child 25 c8dfd7ea87e5
equal deleted inserted replaced
23:65baf3ff7315 24:9e19b7ae3780
    42     
    42     
    43     if request.GET.get('uri'):
    43     if request.GET.get('uri'):
    44         query &= Q(uri=request.GET.get('uri'))
    44         query &= Q(uri=request.GET.get('uri'))
    45     if request.GET.get('creator'):
    45     if request.GET.get('creator'):
    46         query &= Q(creator=request.GET.get('creator'))
    46         query &= Q(creator=request.GET.get('creator'))
       
    47 
       
    48     annotlist = Annotation.objects.filter(query)
       
    49     
    47     if request.GET.get('filter') and len(request.GET.get('filter')) > 0:
    50     if request.GET.get('filter') and len(request.GET.get('filter')) > 0:
    48         query &= Q(text__icontains=request.GET.get('filter'))
    51         search = LdtSearch()
    49 
    52         res = search.query("all",request.GET.get('filter'))        
    50     annotlist = Annotation.objects.filter(query)
    53         for r in res:
       
    54             annotlist.append(r)
    51     
    55     
    52     if request.GET.get('limit'):
    56     if request.GET.get('limit'):
    53         nb = request.GET.get('limit')
    57         nb = request.GET.get('limit')
    54         #offset = request.GET.get('limit')[1]
    58         #offset = request.GET.get('limit')[1]
    55         annotlist = annotlist[:nb]
    59         annotlist = annotlist[:nb]
    64     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
    68     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
    65 
    69 
    66 
    70 
    67 ## Creates an annotation from a urlencoded xml content
    71 ## Creates an annotation from a urlencoded xml content
    68 ## Returns an xml-structured annotation
    72 ## Returns an xml-structured annotation
    69 @oauth_required
       
    70 @csrf_exempt
    73 @csrf_exempt
    71 def create_annotation(request):
    74 def create_annotation(request):
    72     cont = request.POST["content"]
    75     cont = request.POST["content"]
    73     doc = lxml.etree.fromstring(cont)
    76     doc = lxml.etree.fromstring(cont)
    74     
    77     
   143     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
   146     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
   144 
   147 
   145 
   148 
   146 ## Deletes an annotation (from its id)
   149 ## Deletes an annotation (from its id)
   147 ## Returns an empty xml-structured annotation
   150 ## Returns an empty xml-structured annotation
   148 @oauth_required
       
   149 @csrf_exempt
   151 @csrf_exempt
   150 def delete_annotation(request):
   152 def delete_annotation(request):
   151     try:
   153     try:
   152         annot = Annotation.objects.get(external_id=request.POST["id"])
   154         annot = Annotation.objects.get(external_id=request.POST["id"])
   153         annot.delete()
   155         annot.delete()
   157     return HttpResponse("")
   159     return HttpResponse("")
   158 
   160 
   159 
   161 
   160 ## Updates the content of an annotation
   162 ## Updates the content of an annotation
   161 ## Returns the xml-structured updated annotation
   163 ## Returns the xml-structured updated annotation
   162 @oauth_required
       
   163 @csrf_exempt
   164 @csrf_exempt
   164 def update_annotation(request):
   165 def update_annotation(request):
   165     try:
   166     try:
   166         annot = Annotation.objects.get(external_id=request.POST["id"])
   167         annot = Annotation.objects.get(external_id=request.POST["id"])
   167     except Annotation.DoesNotExist:
   168     except Annotation.DoesNotExist:
   181         tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
   182         tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
   182         tags_str = ",".join(tags)
   183         tags_str = ",".join(tags)
   183         if len(tags) == 1:
   184         if len(tags) == 1:
   184             tags_str += ","
   185             tags_str += ","
   185         annot.tags = tags_str
   186         annot.tags = tags_str
   186         
   187                     
   187             
       
   188         
       
   189             
       
   190     title = doc.xpath("/iri/text-annotation/content/title/text()")
   188     title = doc.xpath("/iri/text-annotation/content/title/text()")
   191     if title and annot.title != title[0]:
   189     if title and annot.title != title[0]:
   192         annot.title = unicode(title[0])
   190         annot.title = unicode(title[0])
   193     desc = doc.xpath("/iri/text-annotation/content/description/text()")
   191     desc = doc.xpath("/iri/text-annotation/content/description/text()")
   194     if desc and annot.description != desc[0]:
   192     if desc and annot.description != desc[0]:
   206     update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
   204     update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
   207     if update_date and annot.update_date != update_date[0]:
   205     if update_date and annot.update_date != update_date[0]:
   208         annot.update_date = unicode(update_date[0])
   206         annot.update_date = unicode(update_date[0])
   209 
   207 
   210     annot.save()
   208     annot.save()
       
   209     annot.update_index()
   211 
   210 
   212     return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")
   211     return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")
   213 
   212 
   214     
   213