def add_main(request, template='icraf_dr/add_main.html'): context = RequestContext(request) if request.method == 'POST': form = MainForm(request.POST, request.FILES) if form.is_valid(): # saving manually main = Main( category=Category.objects.get(pk=request.POST['category']), coverage=Coverage.objects.get(pk=request.POST['coverage']), source=Source.objects.get(pk=request.POST['source']), year=Year.objects.get(pk=request.POST['year']), basename=request.POST['basename'] ) main.save() #form.save(commit=True) return HttpResponseRedirect(reverse('icraf_dr_index')) else: print form.errors else: form = MainForm() return render_to_response(template, {'form': form}, context)
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, #^^ ))) #^^
def layer_upload(request, template='upload/layer_upload.html'): if request.method == 'GET': ##icraf_dr_categories = Category.objects.all() #^^ icraf_dr_categories = Category.objects.order_by('cat_num') #^^ ##icraf_dr_coverages = Coverage.objects.all() #^^ icraf_dr_coverages = Coverage.objects.order_by('cov_num') #^^ ##icraf_dr_sources = Source.objects.all() #^^ icraf_dr_sources = Source.objects.order_by('src_num') #^^ ##icraf_dr_years = Year.objects.all() #^^ icraf_dr_years = Year.objects.order_by('year_num') #^^ layer_form = LayerForm(prefix="resource") #^^ category_form = CategoryForm(prefix="category_choice_field") #^^ ctx = { 'icraf_dr_categories': icraf_dr_categories, #^^ 'icraf_dr_coverages': icraf_dr_coverages, #^^ 'icraf_dr_sources': icraf_dr_sources, #^^ 'icraf_dr_years': icraf_dr_years, #^^ "layer_form": layer_form, #^^ "category_form": category_form, #^^ 'charsets': CHARSETS, 'is_layer': True, } return render_to_response(template, RequestContext(request, ctx)) elif request.method == 'POST': form = NewLayerUploadForm(request.POST, request.FILES) tempdir = None errormsgs = [] out = {'success': False} if form.is_valid(): title = form.cleaned_data["layer_title"] # Replace dots in filename - GeoServer REST API upload bug # and avoid any other invalid characters. # Use the title if possible, otherwise default to the filename if title is not None and len(title) > 0: name_base = title else: name_base, __ = os.path.splitext( form.cleaned_data["base_file"].name) name = slugify(name_base.replace(".", "_")) try: # Moved this inside the try/except block because it can raise # exceptions when unicode characters are present. # This should be followed up in upstream Django. 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 #^^ main = Main( #^^ category=icraf_dr_category, #^^ coverage=icraf_dr_coverage, #^^ source=icraf_dr_source, #^^ year=icraf_dr_year, #^^ basename=name_base, #^^ topic_category = TopicCategory(id=request.POST['category_choice_field']), #^^ regions = request.POST['regions'], #^^ date_created=icraf_dr_date_created, #^^ date_published=icraf_dr_date_published, #^^ date_revised=icraf_dr_date_revised #^^ ) #^^ #^^ save icraf_dr_main and pass id to file_upload below main.save() #^^ main_id = main.id #^^ #^^ get metadata form values form_metadata = json.dumps({ #^^ 'owner': request.POST['owner'], #^^ 'title': request.POST['title'], #^^ #'date': request.POST['date'], #^^ replaced_by icraf_dr_date_created 'date': icraf_dr_date_created, #^^ 'date_type': request.POST['date_type'], #^^ #'edition': request.POST['edition'], #^^ replaced by icraf_dr_year 'edition': str(icraf_dr_year.year_num), #^^ 'abstract': request.POST['abstract'], #^^ 'purpose': request.POST['purpose'], #^^ 'maintenance_frequency': request.POST['maintenance_frequency'], #^^ 'regions': request.POST['regions'], #^^ 'restriction_code_type': request.POST['restriction_code_type'], #^^ 'constraints_other': request.POST['constraints_other'], #^^ 'license': request.POST['license'], #^^ 'language': request.POST['language'], #^^ 'spatial_representation_type': request.POST['spatial_representation_type'], #^^ 'temporal_extent_start': request.POST['temporal_extent_start'], #^^ 'temporal_extent_end': request.POST['temporal_extent_end'], #^^ 'supplemental_information': request.POST['supplemental_information'], #^^ 'distribution_url': request.POST['distribution_url'], #^^ 'distribution_description': request.POST['distribution_description'], #^^ 'data_quality_statement': request.POST['data_quality_statement'], #^^ 'featured': request.POST.get('featured', False), #^^ 'is_published': request.POST.get('is_published', False), #^^ 'thumbnail_url': request.POST['thumbnail_url'], #^^ 'keywords': request.POST['keywords'], #^^ 'poc': request.POST['poc'], #^^ 'metadata_author': request.POST['metadata_author'], #^^ 'category_choice_field': request.POST['category_choice_field'], #^^ }) #^^ tempdir, base_file = form.write_files() saved_layer = file_upload( base_file, name=name, user=request.user, overwrite=False, charset=form.cleaned_data["charset"], abstract=form.cleaned_data["abstract"], title=form.cleaned_data["layer_title"], main_id=main_id, #^^ form_metadata=form_metadata, #^^ ) except Exception as e: print 'debug layer creation failed, deleting main' #^^ main.delete() #^^ exception_type, error, tb = sys.exc_info() logger.exception(e) out['success'] = False out['errors'] = str(error) # Assign the error message to the latest UploadSession from that user. latest_uploads = UploadSession.objects.filter(user=request.user).order_by('-date') if latest_uploads.count() > 0: upload_session = latest_uploads[0] upload_session.error = str(error) upload_session.traceback = traceback.format_exc(tb) upload_session.context = log_snippet(CONTEXT_LOG_FILE) upload_session.save() out['traceback'] = upload_session.traceback out['context'] = upload_session.context out['upload_session'] = upload_session.id else: out['success'] = True if hasattr(saved_layer, 'info'): out['info'] = saved_layer.info out['url'] = reverse( 'layer_detail', args=[ saved_layer.service_typename]) upload_session = saved_layer.upload_session upload_session.processed = True upload_session.save() permissions = form.cleaned_data["permissions"] if permissions is not None and len(permissions.keys()) > 0: saved_layer.set_permissions(permissions) finally: if tempdir is not None: shutil.rmtree(tempdir) else: for e in form.errors.values(): errormsgs.extend([escape(v) for v in e]) out['errors'] = form.errors out['errormsgs'] = errormsgs if out['success']: status_code = 200 else: status_code = 400 return HttpResponse( json.dumps(out), mimetype='application/json', status=status_code)