Пример #1
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, )))
Пример #2
0
    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, #^^
                ))) #^^
Пример #3
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, )))
Пример #4
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,
                )))
Пример #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.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,
                    )))