Exemple #1
0
def document_remove(request, docid, template='documents/document_remove.html'):
    try:
        document = _resolve_document(
            request,
            docid,
            'base.delete_resourcebase',
            _PERMISSION_MSG_DELETE)

        if request.method == 'GET':
            return render_to_response(template, RequestContext(request, {
                "document": document
            }))

        if request.method == 'POST':

            if getattr(settings, 'SLACK_ENABLED', False):
                slack_message = None
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document
                    slack_message = build_slack_message_document("document_delete", document)
                except:
                    print "Could not build slack message for delete document."

                document.delete()
                # notify document owner that someone have deleted the document
                if request.user != document.owner:
                    recipient = document.owner
                    notify.send(request.user, recipient=recipient, actor=request.user,
                    target=document, verb='deleted your document')

                try:
                    from geonode.contrib.slack.utils import send_slack_messages
                    send_slack_messages(slack_message)
                except:
                    print "Could not send slack message for delete document."
            else:
                document.delete()
                # notify document owner that someone have deleted the document
                if request.user != document.owner:
                    recipient = document.owner
                    notify.send(request.user, recipient=recipient, actor=request.user,
                    target=document, verb='deleted your document')

            return HttpResponseRedirect(reverse("document_browse"))
        else:
            return HttpResponse("Not allowed", status=403)

    except PermissionDenied:
        return HttpResponse(
            loader.render_to_string(
                '401.html', RequestContext(
                    request, {
                        'error_message': _("You are not allowed to delete this document.")})), status=401)
def document_remove(request, docid, template='documents/document_remove.html'):
    try:
        document = _resolve_document(
            request,
            docid,
            'base.delete_resourcebase',
            _PERMISSION_MSG_DELETE)

        if request.method == 'GET':
            return render_to_response(template, RequestContext(request, {
                "document": document
            }))

        if request.method == 'POST':

            try: #^^
                main = Main.objects.get(document=document) #^^
                main.delete() #^^
            except: #^^
                pass #^^
            
            if getattr(settings, 'SLACK_ENABLED', False):
                slack_message = None
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document
                    slack_message = build_slack_message_document("document_delete", document)
                except:
                    print "Could not build slack message for delete document."

                document.delete()

                try:
                    from geonode.contrib.slack.utils import send_slack_messages
                    send_slack_messages(slack_message)
                except:
                    print "Could not send slack message for delete document."
            else:
                document.delete()

            return HttpResponseRedirect(reverse("document_browse"))
        else:
            return HttpResponse("Not allowed", status=403)

    except PermissionDenied:
        return HttpResponse(
            'You are not allowed to delete this document',
            mimetype="text/plain",
            status=401
        )
Exemple #3
0
def document_remove(request,
                    docid,
                    template='documents/document_remove.html',
                    basemodel=Document):
    try:
        document = _resolve_document(request,
                                     docid,
                                     'base.delete_resourcebase',
                                     _PERMISSION_MSG_DELETE,
                                     basemodel=basemodel)

        if request.method == 'GET':
            return render(request, template, context={"document": document})

        if request.method == 'POST':

            if getattr(settings, 'SLACK_ENABLED', False):
                slack_message = None
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document
                    slack_message = build_slack_message_document(
                        "document_delete", document)
                except BaseException:
                    print "Could not build slack message for delete document."

                document.delete()

                try:
                    from geonode.contrib.slack.utils import send_slack_messages
                    send_slack_messages(slack_message)
                except BaseException:
                    print "Could not send slack message for delete document."
            else:
                document.delete()

            return HttpResponseRedirect(reverse(basemodel.namelc() +
                                                "_browse"))
        else:
            return HttpResponse("Not allowed", status=403)

    except PermissionDenied:
        return HttpResponse('You are not allowed to delete this document',
                            content_type="text/plain",
                            status=401)
Exemple #4
0
def document_metadata(request,
                      docid,
                      template='documents/document_metadata.html'):

    document = None
    try:
        document = _resolve_document(request, docid,
                                     'base.change_resourcebase_metadata',
                                     _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(loader.render_to_string(
            '404.html', RequestContext(request, {})),
                            status=404)

    except PermissionDenied:
        return HttpResponse(loader.render_to_string(
            '401.html',
            RequestContext(request, {
                'error_message':
                _("You are not allowed to edit this document.")
            })),
                            status=403)

    if document is None:
        return HttpResponse('An unknown error has occured.',
                            content_type="text/plain",
                            status=401)

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            document_form = DocumentForm(request.POST,
                                         instance=document,
                                         prefix="resource")
            category_form = CategoryForm(
                request.POST,
                prefix="category_choice_field",
                initial=int(request.POST["category_choice_field"])
                if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_regions = document_form.cleaned_data['regions']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc is None:
                    poc_form = ProfileForm(request.POST,
                                           prefix="poc",
                                           instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set a point of contact for this resource'
                              ))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST,
                                              prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            the_document = document_form.instance
            if new_poc is not None and new_author is not None:
                the_document.poc = new_poc
                the_document.metadata_author = new_author
            if new_keywords:
                the_document.keywords.clear()
                the_document.keywords.add(*new_keywords)
            if new_regions:
                the_document.regions.clear()
                the_document.regions.add(*new_regions)
            the_document.save()
            document_form.save_many2many()
            Document.objects.filter(id=the_document.id).update(
                category=new_category)

            if getattr(settings, 'SLACK_ENABLED', False):
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                    send_slack_messages(
                        build_slack_message_document("document_edit",
                                                     the_document))
                except BaseException:
                    print "Could not send slack message for modified document."

            return HttpResponseRedirect(
                reverse('document_detail', args=(document.id, )))
        # - POST Request Ends here -

        # Request.GET
        if poc is not None:
            document_form.fields['poc'].initial = poc.id
            poc_form = ProfileForm(prefix="poc")
            poc_form.hidden = True

        if metadata_author is not None:
            document_form.fields[
                'metadata_author'].initial = metadata_author.id
            author_form = ProfileForm(prefix="author")
            author_form.hidden = True

        metadata_author_groups = []
        if request.user.is_superuser or request.user.is_staff:
            metadata_author_groups = GroupProfile.objects.all()
        else:
            try:
                all_metadata_author_groups = chain(
                    request.user.group_list_all(),
                    GroupProfile.objects.exclude(access="private").exclude(
                        access="public-invite"))
            except:
                all_metadata_author_groups = GroupProfile.objects.exclude(
                    access="private").exclude(access="public-invite")
            [
                metadata_author_groups.append(item)
                for item in all_metadata_author_groups
                if item not in metadata_author_groups
            ]

        if settings.ADMIN_MODERATE_UPLOADS:
            if not request.user.is_superuser:
                document_form.fields['is_published'].widget.attrs.update(
                    {'disabled': 'true'})

                can_change_metadata = request.user.has_perm(
                    'change_resourcebase_metadata',
                    document.get_self_resource())
                try:
                    is_manager = request.user.groupmember_set.all().filter(
                        role='manager').exists()
                except:
                    is_manager = False
                if not is_manager or not can_change_metadata:
                    document_form.fields['is_approved'].widget.attrs.update(
                        {'disabled': 'true'})

        return render_to_response(
            template,
            RequestContext(
                request, {
                    "resource":
                    document,
                    "document":
                    document,
                    "document_form":
                    document_form,
                    "poc_form":
                    poc_form,
                    "author_form":
                    author_form,
                    "category_form":
                    category_form,
                    "metadata_author_groups":
                    metadata_author_groups,
                    "GROUP_MANDATORY_RESOURCES":
                    getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
                }))
Exemple #5
0
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        # by default, if RESOURCE_PUBLISHING=True then document.is_published
        # must be set to False
        # RESOURCE_PUBLISHING works in similar way as ADMIN_MODERATE_UPLOADS,
        # but is applied to documents only. ADMIN_MODERATE_UPLOADS has wider
        # usage
        is_published = not (settings.RESOURCE_PUBLISHING
                            or settings.ADMIN_MODERATE_UPLOADS)
        self.object.is_published = is_published
        self.object.save()
        form.save_many2many()
        self.object.set_permissions(form.cleaned_data['permissions'])

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None

        out = {'success': False}

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.contrib.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except BaseException:
                print "Exif extraction failed."

        if getattr(settings, 'NLP_ENABLED', False):
            try:
                from geonode.contrib.nlp.utils import nlp_extract_metadata_doc
                nlp_metadata = nlp_extract_metadata_doc(self.object)
                if nlp_metadata:
                    regions.extend(nlp_metadata.get('regions', []))
                    keywords.extend(nlp_metadata.get('keywords', []))
            except BaseException:
                print "NLP extraction failed."

        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(bbox_x0=bbox_x0,
                                                              bbox_x1=bbox_x1,
                                                              bbox_y0=bbox_y0,
                                                              bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(
                    build_slack_message_document("document_new", self.object))
            except BaseException:
                print "Could not send slack message for new document."

        if settings.MONITORING_ENABLED and self.object:
            if hasattr(self.object, 'alternate'):
                self.request.add_resource('document', self.object.alternate)

        if self.request.REQUEST.get('no__redirect', False):
            out['success'] = True
            out['url'] = reverse('document_detail', args=(self.object.id, ))
            if out['success']:
                status_code = 200
            else:
                status_code = 400
            return HttpResponse(json.dumps(out),
                                content_type='application/json',
                                status=status_code)
        else:
            return HttpResponseRedirect(
                reverse('document_metadata', args=(self.object.id, )))
def document_metadata(
        request,
        docid,
        template='documents/document_metadata.html'):

    document = None
    try:
        document = _resolve_document(
            request,
            docid,
            'base.change_resourcebase_metadata',
            _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(
            loader.render_to_string(
                '404.html', RequestContext(
                    request, {
                        })), status=404)

    except PermissionDenied:
        return HttpResponse(
            loader.render_to_string(
                '401.html', RequestContext(
                    request, {
                        'error_message': _("You are not allowed to edit this document.")})), status=403)

    if document is None:
        return HttpResponse(
            'An unknown error has occured.',
            mimetype="text/plain",
            status=401
        )

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            icraf_dr_category =Category.objects.get(pk=request.POST['icraf_dr_category']) #^^
            icraf_dr_coverage =Coverage.objects.get(pk=request.POST['icraf_dr_coverage']) #^^
            icraf_dr_source =Source.objects.get(pk=request.POST['icraf_dr_source']) #^^
            icraf_dr_year =Year.objects.get(pk=request.POST['icraf_dr_year']) #^^
            icraf_dr_date_created = request.POST['icraf_dr_date_created'] #^^
            icraf_dr_date_published = request.POST['icraf_dr_date_published'] #^^
            icraf_dr_date_revised = request.POST['icraf_dr_date_revised'] #^^
            
            #^^ validate date format
            if (len(icraf_dr_date_created)): #^^
                try: #^^
                    parse(icraf_dr_date_created) #^^
                except ValueError: #^^
                    icraf_dr_date_created = None #^^
            else: #^^
                icraf_dr_date_created = None #^^
            
            if (len(icraf_dr_date_published)): #^^
                try: #^^
                    parse(icraf_dr_date_published) #^^
                except ValueError: #^^
                    icraf_dr_date_published = None #^^
            else: #^^
                icraf_dr_date_published = None #^^
            
            if (len(icraf_dr_date_revised)): #^^
                try: #^^
                    parse(icraf_dr_date_revised) #^^
                except ValueError: #^^
                    icraf_dr_date_revised = None #^^
            else: #^^
                icraf_dr_date_revised = None #^^
            
            try: #^^
                main_topic_category = TopicCategory(id=request.POST['category_choice_field']) #^^
            except: #^^
                main_topic_category = None #^^
            
            main_regions = ','.join(request.POST.getlist('resource-regions')) #^^ save as comma separated ids
            
            main_defaults = { #^^
                'category': icraf_dr_category, #^^
                'coverage': icraf_dr_coverage, #^^
                'source': icraf_dr_source, #^^
                'year': icraf_dr_year, #^^
                'topic_category': main_topic_category, #^^
                'regions': main_regions, #^^
                #^^ 'date_created': icraf_dr_date_created, #^^ 20151019 label swapped!
                #^^ 'date_published': icraf_dr_date_published, #^^ 20151019 label swapped!
                'date_created': icraf_dr_date_published, #^^
                'date_published': icraf_dr_date_created, #^^
                'date_revised': icraf_dr_date_revised #^^
            } #^^
            
            main, main_created = Main.objects.get_or_create(document=document, defaults=main_defaults) #^^
            
            if not main_created: #^^
                main.category = icraf_dr_category #^^
                main.coverage = icraf_dr_coverage #^^
                main.source = icraf_dr_source #^^
                main.year = icraf_dr_year #^^
                main.topic_category = main_topic_category #^^
                main.regions = main_regions #^^
                #^^ main.date_created = icraf_dr_date_created #^^ 20151019 label swapped!
                #^^ main.date_published = icraf_dr_date_published #^^ 20151019 label swapped!
                main.date_created = icraf_dr_date_published #^^
                main.date_published = icraf_dr_date_created #^^
                main.date_revised = icraf_dr_date_revised #^^
                main.save() #^^
            
            #^^ override resource-date with icraf_dr_date_created
            #^^ override resource-edition with icraf_dr_year
            request_post = request.POST.copy() #^^
            request_post['resource-date'] = icraf_dr_date_created #^^
            request_post['resource-edition'] = icraf_dr_year.year_num #^^
            
            document_form = DocumentForm(
                request_post, #^^ replace request.POST
                instance=document,
                prefix="resource")
            category_form = CategoryForm(
                request.POST,
                prefix="category_choice_field",
                initial=int(
                    request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)
            
            icraf_dr_categories = Category.objects.order_by('cat_num') #^^
            icraf_dr_coverages = Coverage.objects.order_by('cov_num') #^^
            icraf_dr_sources = Source.objects.order_by('src_num') #^^
            icraf_dr_years = Year.objects.order_by('year_num') #^^
            try: #^^
                icraf_dr_main = Main.objects.get(document=document) #^^
            except: #^^
                icraf_dr_main = None #^^

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc is None:
                    poc_form = ProfileForm(
                        request.POST,
                        prefix="poc",
                        instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault('profile', ErrorList())
                        errors.append(_('You must set a point of contact for this resource'))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST, prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault('profile', ErrorList())
                        errors.append(_('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            if new_poc is not None and new_author is not None:
                the_document = document_form.save()
                the_document.poc = new_poc
                the_document.metadata_author = new_author
                the_document.keywords.add(*new_keywords)
                Document.objects.filter(id=the_document.id).update(category=new_category)
                
                #^^ start update doc_type
                doc_type = request.POST.get('doc_type', None) #^^
                
                if doc_type:
                    try:
                        Document.objects.filter(id=the_document.id).update(doc_type=doc_type)
                    except:
                        pass
                #^^ end

                if getattr(settings, 'SLACK_ENABLED', False):
                    try:
                        from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                        send_slack_messages(build_slack_message_document("document_edit", the_document))
                    except:
                        print "Could not send slack message for modified document."

                return HttpResponseRedirect(
                    reverse(
                        'document_detail',
                        args=(
                            document.id,
                        )))

        if poc is not None:
            document_form.fields['poc'].initial = poc.id
            poc_form = ProfileForm(prefix="poc")
            poc_form.hidden = True

        if metadata_author is not None:
            document_form.fields['metadata_author'].initial = metadata_author.id
            author_form = ProfileForm(prefix="author")
            author_form.hidden = True

        return render_to_response(template, RequestContext(request, {
            "document": document,
            "document_form": document_form,
            "poc_form": poc_form,
            "author_form": author_form,
            "category_form": category_form,
            'icraf_dr_categories': icraf_dr_categories, #^^
            'icraf_dr_coverages': icraf_dr_coverages, #^^
            'icraf_dr_sources': icraf_dr_sources, #^^
            'icraf_dr_years': icraf_dr_years, #^^
            'icraf_dr_main': icraf_dr_main, #^^
        }))
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        print 'debug document form valid' #^^
        
        icraf_dr_category =Category.objects.get(pk=self.request.POST['icraf_dr_category']) #^^
        icraf_dr_coverage =Coverage.objects.get(pk=self.request.POST['icraf_dr_coverage']) #^^
        icraf_dr_source =Source.objects.get(pk=self.request.POST['icraf_dr_source']) #^^
        icraf_dr_year =Year.objects.get(pk=self.request.POST['icraf_dr_year']) #^^
        icraf_dr_date_created = self.request.POST.get('icraf_dr_date_created', None) #^^
        icraf_dr_date_published = self.request.POST.get('icraf_dr_date_published', None) #^^
        icraf_dr_date_revised = self.request.POST.get('icraf_dr_date_revised', None) #^^
        
        #^^ validate date format
        if icraf_dr_date_created: #^^
            try: #^^
                parse(icraf_dr_date_created) #^^
            except ValueError: #^^
                icraf_dr_date_created = None #^^
        else: #^^
            icraf_dr_date_created = None #^^
        
        if icraf_dr_date_published: #^^
            try: #^^
                parse(icraf_dr_date_published) #^^
            except ValueError: #^^
                icraf_dr_date_published = None #^^
        else: #^^
            icraf_dr_date_published = None #^^
        
        if icraf_dr_date_revised: #^^
            try: #^^
                parse(icraf_dr_date_revised) #^^
            except ValueError: #^^
                icraf_dr_date_revised = None #^^
        else: #^^
            icraf_dr_date_revised = None #^^
        
        try: #^^
            main_topic_category = TopicCategory(id=self.request.POST['category_choice_field']) #^^
        except: #^^
            main_topic_category = None #^^
        
        main_regions = ','.join(self.request.POST.getlist('resource-regions')) #^^ save as comma separated ids
        
        main = Main( #^^
            category=icraf_dr_category, #^^
            coverage=icraf_dr_coverage, #^^
            source=icraf_dr_source, #^^
            year=icraf_dr_year, #^^
            basename=form.cleaned_data['doc_file'].name, #^^
            topic_category = main_topic_category, #^^
            regions = main_regions, #^^
            #^^ date_created=icraf_dr_date_created, #^^ 20151019 label swapped!
            #^^ date_published=icraf_dr_date_published, #^^ 20151019 label swapped!
            date_created=icraf_dr_date_published, #^^
            date_published=icraf_dr_date_created, #^^
            date_revised=icraf_dr_date_revised #^^
        ) #^^
        
        #^^ save icraf_dr_main and pass id to document model object
        main.save() #^^
        main_id = main.id #^^
        
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        resource_id = self.request.POST.get('resource', None)
        if resource_id:
            self.object.content_type = ResourceBase.objects.get(id=resource_id).polymorphic_ctype
            self.object.object_id = resource_id
        # by default, if RESOURCE_PUBLISHING=True then document.is_published
        # must be set to False
        is_published = True
        if settings.RESOURCE_PUBLISHING:
            is_published = False
        self.object.is_published = is_published
        
        self.object.main_id = main_id #^^
        
        try: #^^
            self.object.save() #^^
            main.document = self.object #^^
            main.save() #^^
        except: #^^
            main.delete() #^^
        
        #^^ self.object.save()
        #^^self.object.set_permissions(form.cleaned_data['permissions'])
        
        document = None #^^
        try: #^^
            #^^ alternate permission saving method
            print 'debug' #^^
            print self.object.id #^^
            document = Document.objects.get(id=self.object.id) #^^
            document.set_permissions(form.cleaned_data['permissions']) #^^
        except: #^^
            pass #^^

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None
        
        #^^ start processing metadata fields
        owner = self.request.POST.get('resource-owner', None) #^^
        #title = self.request.POST['resource-title'] #^^ replaced by title
        #date = self.request.POST.get('resource-date'], None) #^^ replaced by icraf_dr_date_created
        date = icraf_dr_date_created #^^
        date_type = self.request.POST.get('resource-date_type', None) #^^
        #edition = self.request.POST['resource-edition'] #^^ replaced by icraf_dr_year
        edition = str(icraf_dr_year.year_num) #^^
        abstract = self.request.POST.get('resource-abstract', None) #^^
        purpose = self.request.POST.get('resource-purpose', None) #^^
        maintenance_frequency = self.request.POST.get('resource-maintenance_frequency', None) #^^
        regions = self.request.POST.getlist('resource-regions', None) #^^
        restriction_code_type = self.request.POST.get('resource-restriction_code_type', None) #^^
        constraints_other = self.request.POST.get('resource-constraints_other', None) #^^
        license = self.request.POST.get('resource-license', None) #^^
        language = self.request.POST.get('resource-language', None) #^^
        spatial_representation_type = self.request.POST.get('resource-spatial_representation_type', None) #^^
        temporal_extent_start = self.request.POST.get('resource-temporal_extent_start', None) #^^
        temporal_extent_end = self.request.POST.get('resource-temporal_extent_end', None) #^^
        supplemental_information = self.request.POST.get('resource-supplemental_information', None) #^^
        distribution_url = self.request.POST.get('resource-distribution_url', None) #^^
        distribution_description = self.request.POST.get('resource-distribution_description', None) #^^
        data_quality_statement = self.request.POST.get('resource-data_quality_statement', None) #^^
        featured = self.request.POST.get('resource-featured', False) #^^
        is_published = self.request.POST.get('resource-is_published', False) #^^
        thumbnail_url = self.request.POST.get('resource-thumbnail_url', None) #^^
        keywords = self.request.POST.get('resource-keywords', None) #^^
        poc = self.request.POST.get('resource-poc', None) #^^
        metadata_author = self.request.POST.get('resource-metadata_author', None) #^^
        category_choice_field = self.request.POST.get('category_choice_field', None) #^^
        doc_type = self.request.POST.get('doc_type', None) #^^
        
        if owner and owner.isdigit():
            try:
                owner = get_user_model().objects.get(id=owner)
                self.object.owner = owner
            except get_user_model().DoesNotExist:
                pass
        
        if date:
            self.object.date = date
        
        if date_type:
            self.object.date_type = date_type
        
        if edition:
            self.object.edition = edition
        
        if abstract:
            self.object.abstract = abstract
        
        if purpose:
            self.object.purpose = purpose
        
        if maintenance_frequency:
            self.object.maintenance_frequency = maintenance_frequency
        
        if restriction_code_type:
            try:
                self.object.restriction_code_type = RestrictionCodeType(id=restriction_code_type)
            except:
                pass
        
        if constraints_other:
            self.object.constraints_other = constraints_other
        
        if license:
            try:
                self.object.license = License(id=license)
            except:
                pass
        
        if language:
            self.object.language = language
        
        if spatial_representation_type:
            try:
                self.object.spatial_representation_type = SpatialRepresentationType(id=spatial_representation_type)
            except:
                pass
        
        if temporal_extent_start:
            try:
                parse(temporal_extent_start)
                self.object.temporal_extent_start = temporal_extent_start
            except ValueError:
                pass
        
        if temporal_extent_end:
            try:
                parse(temporal_extent_end)
                self.object.temporal_extent_end = temporal_extent_end
            except ValueError:
                pass
        
        if supplemental_information:
            self.object.supplemental_information = supplemental_information
        
        if distribution_url:
            self.object.distribution_url = distribution_url
        
        if distribution_description:
            self.object.distribution_description = distribution_description
        
        if data_quality_statement:
            self.object.data_quality_statement = data_quality_statement
        
        if featured != False:
            self.object.featured = True
        
        if is_published != False:
            self.object.is_published = True
        
        if thumbnail_url:
            val = URLValidator()
            try:
                val(thumbnail_url)
                if (thumbnail_url.lower().startswith(('http://', 'https://')) and thumbnail_url.lower().endswith(('.jpg', '.jpeg', '.png'))):
                    self.object.thumbnail_url = thumbnail_url
            except ValidationError:
                pass
        
        if len(keywords) > 0 and document != None:
            keywords = [keyword.strip() for keyword in keywords.split(',')]
            document.keywords.add(*keywords)
        
        if len(regions) > 0 and document != None:
            document.regions.add(*regions)
        
        if poc and poc.isdigit():
            try:
                contact = get_user_model().objects.get(id=poc)
                self.object.poc = contact
            except get_user_model().DoesNotExist:
                pass
        
        if metadata_author and metadata_author.isdigit():
            try:
                author = get_user_model().objects.get(id=metadata_author)
                self.object.metadata_author = author
            except get_user_model().DoesNotExist:
                pass
        
        if category_choice_field:
            try:
                self.object.category = TopicCategory(id=category_choice_field)
            except:
                pass
        
        self.object.save()
        
        if doc_type:
            try:
                Document.objects.filter(id=self.object.pk).update(doc_type=doc_type)
            except:
                pass
        #^^ end

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.contrib.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except:
                print "Exif extraction failed."

        if getattr(settings, 'NLP_ENABLED', False):
            try:
                from geonode.contrib.nlp.utils import nlp_extract_metadata_doc
                nlp_metadata = nlp_extract_metadata_doc(self.object)
                if nlp_metadata:
                    regions.extend(nlp_metadata.get('regions', []))
                    keywords.extend(nlp_metadata.get('keywords', []))
            except:
                print "NLP extraction failed."

        """ #^^ overriden above in metadata saving
        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)
        """

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(
                bbox_x0=bbox_x0,
                bbox_x1=bbox_x1,
                bbox_y0=bbox_y0,
                bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(build_slack_message_document("document_new", self.object))
            except:
                print "Could not send slack message for new document."

        #^^return HttpResponseRedirect(
        #^^    reverse(
        #^^        'document_metadata',
        #^^        args=(
        #^^            self.object.id,
        #^^        )))
        
        #^^ start send email notification to 'Pengelola Basis Data' group managers
        if 'notification' in settings.INSTALLED_APPS and document != None:
            try:
                group_approver = GroupProfile.objects.get(title='Pengelola Basis Data')
                if group_approver:
                    group_managers = group_approver.get_managers()
                    if group_managers:
                        notif_ctx = {
                            'resource': document,
                        }
                        print 'sending notification'
                        notification.send(group_managers, 'document_created', notif_ctx)
            except:
                pass
        #^^ end

        #^^ redirect to document detail instead of document metadata page
        return HttpResponseRedirect( #^^
            reverse( #^^
                'document_detail', #^^
                args=( #^^
                    self.object.id, #^^
                ))) #^^
Exemple #8
0
def document_metadata(
        request,
        docid,
        template='documents/document_metadata.html'):

    document = None
    try:
        document = _resolve_document(
            request,
            docid,
            'base.change_resourcebase_metadata',
            _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(
            loader.render_to_string(
                '404.html', RequestContext(
                    request, {
                        })), status=404)

    except PermissionDenied:
        return HttpResponse(
            loader.render_to_string(
                '401.html', RequestContext(
                    request, {
                        'error_message': _("You are not allowed to edit this document.")})), status=403)

    if document is None:
        return HttpResponse(
            'An unknown error has occured.',
            content_type="text/plain",
            status=401
        )

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            document_form = DocumentForm(
                request.POST,
                instance=document,
                prefix="resource")
            category_form = CategoryForm(
                request.POST,
                prefix="category_choice_field",
                initial=int(
                    request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc is None:
                    poc_form = ProfileForm(
                        request.POST,
                        prefix="poc",
                        instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault('profile', ErrorList())
                        errors.append(_('You must set a point of contact for this resource'))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST, prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault('profile', ErrorList())
                        errors.append(_('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            if new_poc is not None and new_author is not None:
                the_document = document_form.save()
                the_document.poc = new_poc
                the_document.metadata_author = new_author
                the_document.keywords.add(*new_keywords)
                Document.objects.filter(id=the_document.id).update(category=new_category)

                if getattr(settings, 'SLACK_ENABLED', False):
                    try:
                        from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                        send_slack_messages(build_slack_message_document("document_edit", the_document))
                    except:
                        print "Could not send slack message for modified document."

                return HttpResponseRedirect(
                    reverse(
                        'document_detail',
                        args=(
                            document.id,
                        )))

        if poc is not None:
            document_form.fields['poc'].initial = poc.id
            poc_form = ProfileForm(prefix="poc")
            poc_form.hidden = True

        if metadata_author is not None:
            document_form.fields['metadata_author'].initial = metadata_author.id
            author_form = ProfileForm(prefix="author")
            author_form.hidden = True

        return render_to_response(template, RequestContext(request, {
            "document": document,
            "document_form": document_form,
            "poc_form": poc_form,
            "author_form": author_form,
            "category_form": category_form,
        }))
Exemple #9
0
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        resource_id = self.request.POST.get('resource', None)
        if resource_id:
            self.object.content_type = ResourceBase.objects.get(id=resource_id).polymorphic_ctype
            self.object.object_id = resource_id
        # by default, if RESOURCE_PUBLISHING=True then document.is_published
        # must be set to False
        is_published = True
        if settings.RESOURCE_PUBLISHING:
            is_published = False
        self.object.is_published = is_published

        self.object.save()
        self.object.set_permissions(form.cleaned_data['permissions'])

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.contrib.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except:
                print "Exif extraction failed."

        if getattr(settings, 'NLP_ENABLED', False):
            try:
                from geonode.contrib.nlp.utils import nlp_extract_metadata_doc
                nlp_metadata = nlp_extract_metadata_doc(self.object)
                if nlp_metadata:
                    regions.extend(nlp_metadata.get('regions', []))
                    keywords.extend(nlp_metadata.get('keywords', []))
            except:
                print "NLP extraction failed."

        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(
                bbox_x0=bbox_x0,
                bbox_x1=bbox_x1,
                bbox_y0=bbox_y0,
                bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(build_slack_message_document("document_new", self.object))
            except:
                print "Could not send slack message for new document."

        return HttpResponseRedirect(
            reverse(
                'document_metadata',
                args=(
                    self.object.id,
                )))
Exemple #10
0
def document_metadata(request,
                      docid,
                      template='documents/document_metadata.html'):

    document = None
    try:
        document = _resolve_document(request, docid,
                                     'base.change_resourcebase_metadata',
                                     _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(loader.render_to_string(
            '404.html', RequestContext(request, {})),
                            status=404)

    except PermissionDenied:
        return HttpResponse(loader.render_to_string(
            '401.html',
            RequestContext(request, {
                'error_message':
                _("You are not allowed to edit this document.")
            })),
                            status=403)

    if document is None:
        return HttpResponse('An unknown error has occured.',
                            mimetype="text/plain",
                            status=401)

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            document_form = DocumentForm(request.POST,
                                         instance=document,
                                         prefix="resource")
            category_form = CategoryForm(
                request.POST,
                prefix="category_choice_field",
                initial=int(request.POST["category_choice_field"])
                if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc is None:
                    poc_form = ProfileForm(request.POST,
                                           prefix="poc",
                                           instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set a point of contact for this resource'
                              ))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST,
                                              prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            if new_poc is not None and new_author is not None:
                the_document = document_form.save()
                the_document.poc = new_poc
                the_document.metadata_author = new_author
                the_document.keywords.add(*new_keywords)
                Document.objects.filter(id=the_document.id).update(
                    category=new_category)

                if getattr(settings, 'SLACK_ENABLED', False):
                    try:
                        from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                        send_slack_messages(
                            build_slack_message_document(
                                "document_edit", the_document))
                    except:
                        print "Could not send slack message for modified document."

                return HttpResponseRedirect(
                    reverse('document_detail', args=(document.id, )))

        if poc is not None:
            document_form.fields['poc'].initial = poc.id
            poc_form = ProfileForm(prefix="poc")
            poc_form.hidden = True

        if metadata_author is not None:
            document_form.fields[
                'metadata_author'].initial = metadata_author.id
            author_form = ProfileForm(prefix="author")
            author_form.hidden = True

        return render_to_response(
            template,
            RequestContext(
                request, {
                    "document": document,
                    "document_form": document_form,
                    "poc_form": poc_form,
                    "author_form": author_form,
                    "category_form": category_form,
                }))
Exemple #11
0
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        resource_id = self.request.POST.get('resource', None)
        if resource_id:
            self.object.content_type = ResourceBase.objects.get(
                id=resource_id).polymorphic_ctype
            self.object.object_id = resource_id
        # by default, if RESOURCE_PUBLISHING=True then document.is_published
        # must be set to False
        is_published = True
        if settings.RESOURCE_PUBLISHING:
            is_published = False
        self.object.is_published = is_published

        self.object.save()
        self.object.set_permissions(form.cleaned_data['permissions'])

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.contrib.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except:
                print "Exif extraction failed."

        if getattr(settings, 'NLP_ENABLED', False):
            try:
                from geonode.contrib.nlp.utils import nlp_extract_metadata_doc
                nlp_metadata = nlp_extract_metadata_doc(self.object)
                if nlp_metadata:
                    regions.extend(nlp_metadata.get('regions', []))
                    keywords.extend(nlp_metadata.get('keywords', []))
            except:
                print "NLP extraction failed."

        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(bbox_x0=bbox_x0,
                                                              bbox_x1=bbox_x1,
                                                              bbox_y0=bbox_y0,
                                                              bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(
                    build_slack_message_document("document_new", self.object))
            except:
                print "Could not send slack message for new document."

        return HttpResponseRedirect(
            reverse('document_metadata', args=(self.object.id, )))
Exemple #12
0
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user

        if settings.ADMIN_MODERATE_UPLOADS:
            self.object.is_approved = False
        if settings.RESOURCE_PUBLISHING:
            self.object.is_published = False
        self.object.save()
        form.save_many2many()
        self.object.set_permissions(form.cleaned_data['permissions'])

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None

        out = {'success': False}

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.documents.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except Exception:
                logger.error("Exif extraction failed.")

        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(bbox_x0=bbox_x0,
                                                              bbox_x1=bbox_x1,
                                                              bbox_y0=bbox_y0,
                                                              bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(
                    build_slack_message_document("document_new", self.object))
            except Exception:
                logger.error("Could not send slack message for new document.")

        register_event(self.request, EventType.EVENT_UPLOAD, self.object)

        if self.request.GET.get('no__redirect', False):
            out['success'] = True
            out['url'] = reverse('document_detail', args=(self.object.id, ))
            if out['success']:
                status_code = 200
            else:
                status_code = 400
            return HttpResponse(json.dumps(out),
                                content_type='application/json',
                                status=status_code)
        else:
            return HttpResponseRedirect(
                reverse('document_metadata', args=(self.object.id, )))
Exemple #13
0
def document_metadata(
        request,
        docid,
        template='documents/document_metadata.html',
        ajax=True):

    document = None
    try:
        document = _resolve_document(
            request,
            docid,
            'base.change_resourcebase_metadata',
            _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(
            loader.render_to_string(
                '404.html', context={
                }, request=request), status=404)

    except PermissionDenied:
        return HttpResponse(
            loader.render_to_string(
                '401.html', context={
                    'error_message': _("You are not allowed to edit this document.")}, request=request), status=403)

    if document is None:
        return HttpResponse(
            'An unknown error has occured.',
            content_type="text/plain",
            status=401
        )

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            document_form = DocumentForm(
                request.POST,
                instance=document,
                prefix="resource")
            category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
                request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_regions = document_form.cleaned_data['regions']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc is None:
                    poc_form = ProfileForm(
                        request.POST,
                        prefix="poc",
                        instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set a point of contact for this resource'))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST, prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            the_document = document_form.instance
            if new_poc is not None and new_author is not None:
                the_document.poc = new_poc
                the_document.metadata_author = new_author
            if new_keywords:
                the_document.keywords.clear()
                the_document.keywords.add(*new_keywords)
            if new_regions:
                the_document.regions.clear()
                the_document.regions.add(*new_regions)
            the_document.save()
            document_form.save_many2many()
            Document.objects.filter(
                id=the_document.id).update(
                category=new_category)

            if getattr(settings, 'SLACK_ENABLED', False):
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                    send_slack_messages(
                        build_slack_message_document(
                            "document_edit", the_document))
                except BaseException:
                    print "Could not send slack message for modified document."

            if not ajax:
                return HttpResponseRedirect(
                    reverse(
                        'document_detail',
                        args=(
                            document.id,
                        )))

            message = document.id

            return HttpResponse(json.dumps({'message': message}))

        # - POST Request Ends here -

        # Request.GET
        if poc is not None:
            document_form.fields['poc'].initial = poc.id
            poc_form = ProfileForm(prefix="poc")
            poc_form.hidden = True

        if metadata_author is not None:
            document_form.fields['metadata_author'].initial = metadata_author.id
            author_form = ProfileForm(prefix="author")
            author_form.hidden = True

        metadata_author_groups = []
        if request.user.is_superuser or request.user.is_staff:
            metadata_author_groups = GroupProfile.objects.all()
        else:
            try:
                all_metadata_author_groups = chain(
                    request.user.group_list_all(),
                    GroupProfile.objects.exclude(
                        access="private").exclude(access="public-invite"))
            except BaseException:
                all_metadata_author_groups = GroupProfile.objects.exclude(
                    access="private").exclude(access="public-invite")
            [metadata_author_groups.append(item) for item in all_metadata_author_groups
                if item not in metadata_author_groups]

        if settings.ADMIN_MODERATE_UPLOADS:
            if not request.user.is_superuser:
                document_form.fields['is_published'].widget.attrs.update(
                    {'disabled': 'true'})

                can_change_metadata = request.user.has_perm(
                    'change_resourcebase_metadata',
                    document.get_self_resource())
                try:
                    is_manager = request.user.groupmember_set.all().filter(role='manager').exists()
                except BaseException:
                    is_manager = False
                if not is_manager or not can_change_metadata:
                    document_form.fields['is_approved'].widget.attrs.update(
                        {'disabled': 'true'})

        return render(request, template, context={
            "resource": document,
            "document": document,
            "document_form": document_form,
            "poc_form": poc_form,
            "author_form": author_form,
            "category_form": category_form,
            "metadata_author_groups": metadata_author_groups,
            "GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
        })
Exemple #14
0
    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        # by default, if RESOURCE_PUBLISHING=True then document.is_published
        # must be set to False
        # RESOURCE_PUBLISHING works in similar way as ADMIN_MODERATE_UPLOADS,
        # but is applied to documents only. ADMIN_MODERATE_UPLOADS has wider
        # usage
        is_published = not (
            settings.RESOURCE_PUBLISHING or settings.ADMIN_MODERATE_UPLOADS)
        self.object.is_published = is_published
        self.object.save()
        form.save_many2many()
        self.object.set_permissions(form.cleaned_data['permissions'])

        abstract = None
        date = None
        regions = []
        keywords = []
        bbox = None

        out = {'success': False}

        if getattr(settings, 'EXIF_ENABLED', False):
            try:
                from geonode.contrib.exif.utils import exif_extract_metadata_doc
                exif_metadata = exif_extract_metadata_doc(self.object)
                if exif_metadata:
                    date = exif_metadata.get('date', None)
                    keywords.extend(exif_metadata.get('keywords', []))
                    bbox = exif_metadata.get('bbox', None)
                    abstract = exif_metadata.get('abstract', None)
            except BaseException:
                print "Exif extraction failed."

        if getattr(settings, 'NLP_ENABLED', False):
            try:
                from geonode.contrib.nlp.utils import nlp_extract_metadata_doc
                nlp_metadata = nlp_extract_metadata_doc(self.object)
                if nlp_metadata:
                    regions.extend(nlp_metadata.get('regions', []))
                    keywords.extend(nlp_metadata.get('keywords', []))
            except BaseException:
                print "NLP extraction failed."

        if abstract:
            self.object.abstract = abstract
            self.object.save()

        if date:
            self.object.date = date
            self.object.date_type = "Creation"
            self.object.save()

        if len(regions) > 0:
            self.object.regions.add(*regions)

        if len(keywords) > 0:
            self.object.keywords.add(*keywords)

        if bbox:
            bbox_x0, bbox_x1, bbox_y0, bbox_y1 = bbox
            Document.objects.filter(id=self.object.pk).update(
                bbox_x0=bbox_x0,
                bbox_x1=bbox_x1,
                bbox_y0=bbox_y0,
                bbox_y1=bbox_y1)

        if getattr(settings, 'SLACK_ENABLED', False):
            try:
                from geonode.contrib.slack.utils import build_slack_message_document, send_slack_message
                send_slack_message(
                    build_slack_message_document(
                        "document_new", self.object))
            except BaseException:
                print "Could not send slack message for new document."

        if settings.MONITORING_ENABLED and self.object:
            if hasattr(self.object, 'alternate'):
                self.request.add_resource('document', self.object.alternate)

        if self.request.GET.get('no__redirect', False):
            out['success'] = True
            out['url'] = reverse(
                'document_detail',
                args=(
                    self.object.id,
                ))
            if out['success']:
                status_code = 200
            else:
                status_code = 400
            return HttpResponse(
                json.dumps(out),
                content_type='application/json',
                status=status_code)
        else:
            return HttpResponseRedirect(
                reverse(
                    'document_metadata',
                    args=(
                        self.object.id,
                    )))
Exemple #15
0
def document_metadata(request,
                      docid,
                      template='documents/document_metadata.html',
                      ajax=True):

    document = None
    try:
        document = _resolve_document(request, docid,
                                     'base.change_resourcebase_metadata',
                                     _PERMISSION_MSG_METADATA)

    except Http404:
        return HttpResponse(loader.render_to_string('404.html',
                                                    context={},
                                                    request=request),
                            status=404)

    except PermissionDenied:
        return HttpResponse(loader.render_to_string(
            '401.html',
            context={
                'error_message':
                _("You are not allowed to edit this document.")
            },
            request=request),
                            status=403)

    if document is None:
        return HttpResponse('An unknown error has occured.',
                            content_type="text/plain",
                            status=401)

    else:
        poc = document.poc
        metadata_author = document.metadata_author
        topic_category = document.category

        if request.method == "POST":
            document_form = DocumentForm(request.POST,
                                         instance=document,
                                         prefix="resource")
            category_form = CategoryForm(
                request.POST,
                prefix="category_choice_field",
                initial=int(request.POST["category_choice_field"])
                if "category_choice_field" in request.POST else None)
        else:
            document_form = DocumentForm(instance=document, prefix="resource")
            category_form = CategoryForm(
                prefix="category_choice_field",
                initial=topic_category.id if topic_category else None)

        if request.method == "POST" and document_form.is_valid(
        ) and category_form.is_valid():
            new_poc = document_form.cleaned_data['poc']
            new_author = document_form.cleaned_data['metadata_author']
            new_keywords = document_form.cleaned_data['keywords']
            new_regions = document_form.cleaned_data['regions']
            new_category = TopicCategory.objects.get(
                id=category_form.cleaned_data['category_choice_field'])

            if new_poc is None:
                if poc.user is None:
                    poc_form = ProfileForm(request.POST,
                                           prefix="poc",
                                           instance=poc)
                else:
                    poc_form = ProfileForm(request.POST, prefix="poc")
                if poc_form.is_valid():
                    if len(poc_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = poc_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set a point of contact for this resource'
                              ))
                        poc = None
                if poc_form.has_changed and poc_form.is_valid():
                    new_poc = poc_form.save()

            if new_author is None:
                if metadata_author is None:
                    author_form = ProfileForm(request.POST,
                                              prefix="author",
                                              instance=metadata_author)
                else:
                    author_form = ProfileForm(request.POST, prefix="author")
                if author_form.is_valid():
                    if len(author_form.cleaned_data['profile']) == 0:
                        # FIXME use form.add_error in django > 1.7
                        errors = author_form._errors.setdefault(
                            'profile', ErrorList())
                        errors.append(
                            _('You must set an author for this resource'))
                        metadata_author = None
                if author_form.has_changed and author_form.is_valid():
                    new_author = author_form.save()

            # rename document file if any title fields changed
            # TODO: in Geonode 2.8 there is separate 'version' field which in DRR used 'edition' field
            title_fields = [
                'category', 'regions', 'datasource', 'title', 'subtitle',
                'papersize', 'date', 'version'
            ]
            title_fields_changed = [
                i for e in title_fields for i in document_form.changed_data
                if e == i
            ]
            if title_fields_changed:
                doc_file_path = os.path.dirname(document.doc_file.name)
                new_filename = '%s.%s' % (get_valid_filename('_'.join([
                    'afg', new_category.identifier, '-'.join([
                        r.code for r in Region.objects.all().filter(
                            pk__in=document_form.cleaned_data['regions'])
                    ]), document_form.cleaned_data['datasource'],
                    slugify(document_form.cleaned_data['title'],
                            allow_unicode=True),
                    slugify(document_form.cleaned_data['subtitle'],
                            allow_unicode=True),
                    document_form.cleaned_data['papersize'],
                    document_form.cleaned_data['date'].strftime('%Y-%m-%d'),
                    document_form.cleaned_data['version']
                ])), document.extension)
                new_doc_file = os.path.join(doc_file_path, new_filename)
                old_path = os.path.join(settings.MEDIA_ROOT,
                                        document.doc_file.name)
                new_path = os.path.join(settings.MEDIA_ROOT, new_doc_file)
                os.rename(old_path, new_path)
                document.doc_file.name = new_doc_file
                document.save()

            the_document = document_form.instance
            if new_poc is not None and new_author is not None:
                the_document = document_form.save()
                the_document.poc = new_poc
                the_document.metadata_author = new_author
            if new_keywords:
                the_document.keywords.clear()
                the_document.keywords.add(*new_keywords)
            if new_regions:
                the_document.regions.clear()
                the_document.regions.add(*new_regions)
            the_document.save()
            document_form.save_many2many()
            Document.objects.filter(id=the_document.id).update(
                category=new_category)

            if getattr(settings, 'SLACK_ENABLED', False):
                try:
                    from geonode.contrib.slack.utils import build_slack_message_document, send_slack_messages
                    send_slack_messages(
                        build_slack_message_document("document_edit",
                                                     the_document))
                except BaseException:
                    print "Could not send slack message for modified document."

            if not ajax:
                return HttpResponseRedirect(
                    reverse('document_detail', args=(document.id, )))

            message = document.id

            return HttpResponse(json.dumps({'message': message}))

        # - POST Request Ends here -

        # Request.GET
        if poc is None:
            poc_form = ProfileForm(request.POST, prefix="poc")
        else:
            if poc is None:
                poc_form = ProfileForm(instance=poc, prefix="poc")
            else:
                document_form.fields['poc'].initial = poc.id
                poc_form = ProfileForm(prefix="poc")
                poc_form.hidden = True

        if metadata_author is None:
            author_form = ProfileForm(request.POST, prefix="author")
        else:
            if metadata_author is None:
                author_form = ProfileForm(instance=metadata_author,
                                          prefix="author")
            else:
                document_form.fields[
                    'metadata_author'].initial = metadata_author.id
                author_form = ProfileForm(prefix="author")
                author_form.hidden = True

        metadata_author_groups = []
        if request.user.is_superuser or request.user.is_staff:
            metadata_author_groups = GroupProfile.objects.all()
        else:
            try:
                all_metadata_author_groups = chain(
                    request.user.group_list_all(),
                    GroupProfile.objects.exclude(access="private").exclude(
                        access="public-invite"))
            except BaseException:
                all_metadata_author_groups = GroupProfile.objects.exclude(
                    access="private").exclude(access="public-invite")
            [
                metadata_author_groups.append(item)
                for item in all_metadata_author_groups
                if item not in metadata_author_groups
            ]

        if settings.ADMIN_MODERATE_UPLOADS:
            if not request.user.is_superuser:
                document_form.fields['is_published'].widget.attrs.update(
                    {'disabled': 'true'})

                can_change_metadata = request.user.has_perm(
                    'change_resourcebase_metadata',
                    document.get_self_resource())
                try:
                    is_manager = request.user.groupmember_set.all().filter(
                        role='manager').exists()
                except BaseException:
                    is_manager = False
                if not is_manager or not can_change_metadata:
                    document_form.fields['is_approved'].widget.attrs.update(
                        {'disabled': 'true'})

        return render(request,
                      template,
                      context={
                          "resource":
                          document,
                          "document":
                          document,
                          "document_form":
                          document_form,
                          "poc_form":
                          poc_form,
                          "author_form":
                          author_form,
                          "category_form":
                          category_form,
                          "metadata_author_groups":
                          metadata_author_groups,
                          "GROUP_MANDATORY_RESOURCES":
                          getattr(settings, 'GROUP_MANDATORY_RESOURCES',
                                  False),
                      })