def document_metadata(request, docid, template="documents/document_metadata.html"): document = Document.objects.get(id=docid) poc = document.poc metadata_author = document.metadata_author if request.method == "POST": document_form = DocumentForm(request.POST, instance=document, prefix="document") else: document_form = DocumentForm(instance=document, prefix="document") if request.method == "POST" and document_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"] if new_poc is None: poc_form = ProfileForm(request.POST, prefix="poc") if poc_form.has_changed and poc_form.is_valid(): new_poc = poc_form.save() if new_author is None: author_form = ProfileForm(request.POST, prefix="author") 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(commit=False) the_document.poc = new_poc the_document.metadata_author = new_author the_document.keywords.add(*new_keywords) the_document.save() return HttpResponseRedirect(reverse("document_detail", args=(document.id,))) if poc.user 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.user 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 return render_to_response( template, RequestContext( request, {"document": document, "document_form": document_form, "poc_form": poc_form, "author_form": author_form}, ), )
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), }))
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 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 and request.POST["category_choice_field"] else None) tkeywords_form = TKeywordForm(request.POST) else: document_form = DocumentForm(instance=document, prefix="resource") category_form = CategoryForm( prefix="category_choice_field", initial=topic_category.id if topic_category else None) # Keywords from THESAURUS management doc_tkeywords = document.tkeywords.all() tkeywords_list = '' lang = 'en' # TODO: use user's language if doc_tkeywords and len(doc_tkeywords) > 0: tkeywords_ids = doc_tkeywords.values_list('id', flat=True) if hasattr(settings, 'THESAURUS') and settings.THESAURUS: el = settings.THESAURUS thesaurus_name = el['name'] try: t = Thesaurus.objects.get(identifier=thesaurus_name) for tk in t.thesaurus.filter(pk__in=tkeywords_ids): tkl = tk.keyword.filter(lang=lang) if len(tkl) > 0: tkl_ids = ",".join( map(str, tkl.values_list('id', flat=True))) tkeywords_list += "," + \ tkl_ids if len( tkeywords_list) > 0 else tkl_ids except Exception: tb = traceback.format_exc() logger.error(tb) tkeywords_form = TKeywordForm(instance=document) if request.method == "GET": print("TESTE NO LAYERS!!!") project = request.GET.get("list_projects") management_actions = request.GET.get("list_management_actions") if project: print("CLIQUEI EM PROJETO!!") settings.PROJETO_API = True settings.ACAO_GERENCIAL_API = False elif management_actions: print("CLIQUEI EM AÇÃO GERENCIAL") settings.ACAO_GERENCIAL_API = True settings.PROJETO_API = False 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_embrapa_keywords = document_form.cleaned_data['embrapa_keywords'] new_embrapa_data_quality_statement = document_form.cleaned_data['embrapa_data_quality_statement'] new_embrapa_authors = document_form.cleaned_data['embrapa_autores'] new_category = None if category_form and 'category_choice_field' in category_form.cleaned_data and\ category_form.cleaned_data['category_choice_field']: new_category = TopicCategory.objects.get( id=int(category_form.cleaned_data['category_choice_field'])) print("VIEWS DO DOCUMENTS!!") 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() document = document_form.instance if new_poc is not None and new_author is not None: document.poc = new_poc document.metadata_author = new_author document.embrapa_autores.clear() document.embrapa_autores.add(*new_embrapa_authors) document.embrapa_data_quality_statement.clear() document.embrapa_data_quality_statement.add(*new_embrapa_data_quality_statement) document.embrapa_keywords.clear() document.embrapa_keywords.add(*new_embrapa_keywords) document.keywords.clear() document.keywords.add(*new_keywords) document.regions.clear() document.regions.add(*new_regions) document.category = new_category document.save() document_form.save_many2many() register_event(request, EventType.EVENT_CHANGE_METADATA, document) if not ajax: return HttpResponseRedirect( reverse( 'document_detail', args=( document.id, ))) message = document.id try: # Keywords from THESAURUS management # Rewritten to work with updated autocomplete if not tkeywords_form.is_valid(): return HttpResponse(json.dumps({'message': "Invalid thesaurus keywords"}, status_code=400)) tkeywords_data = tkeywords_form.cleaned_data['tkeywords'] thesaurus_setting = getattr(settings, 'THESAURUS', None) if thesaurus_setting: tkeywords_data = tkeywords_data.filter( thesaurus__identifier=thesaurus_setting['name'] ) document.tkeywords = tkeywords_data except Exception: tb = traceback.format_exc() logger.error(tb) return HttpResponse(json.dumps({'message': message})) # - POST Request Ends here - # Request.GET if poc is not None: # embrapa # 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 Exception: 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 Exception: is_manager = False if not is_manager or not can_change_metadata: document_form.fields['is_approved'].widget.attrs.update( {'disabled': 'true'}) register_event(request, EventType.EVENT_VIEW_METADATA, document) 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, "tkeywords_form": tkeywords_form, "metadata_author_groups": metadata_author_groups, "TOPICCATEGORY_MANDATORY": getattr(settings, 'TOPICCATEGORY_MANDATORY', False), "GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False), })
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, }))
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, }))
def document_metadata(request, docid, template='documents/document_metadata.html'): document = Document.objects.get(id=docid) 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.user is None: poc_form = ProfileForm(request.POST, prefix="poc", instance=poc) else: poc_form = ProfileForm(request.POST, prefix="poc") 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.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) the_document.category = new_category the_document.save() return HttpResponseRedirect(reverse('document_detail', args=(document.id,))) 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 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, }))
def document_metadata(request, docid, template='documents/document_metadata.html'): document = Document.objects.get(id=docid) poc = document.poc metadata_author = document.metadata_author if request.method == "POST": document_form = DocumentForm(request.POST, instance=document, prefix="document") else: document_form = DocumentForm(instance=document, prefix="document") if request.method == "POST" and document_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'] if new_poc is None: poc_form = ProfileForm(request.POST, prefix="poc") if poc_form.has_changed and poc_form.is_valid(): new_poc = poc_form.save() if new_author is None: author_form = ProfileForm(request.POST, prefix="author") 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(commit=False) the_document.poc = new_poc the_document.metadata_author = new_author the_document.keywords.add(*new_keywords) the_document.save() return HttpResponseRedirect( reverse('document_detail', args=(document.id, ))) if poc.user 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.user 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 return render_to_response( template, RequestContext( request, { "document": document, "document_form": document_form, "poc_form": poc_form, "author_form": author_form, }))
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 PermissionDenied: return HttpResponse(_("Not allowed"), status=403) except Exception: raise Http404(_("Not found")) if not document: raise Http404(_("Not found")) # Add metadata_author or poc if missing document.add_missing_metadata_author_or_poc() poc = document.poc metadata_author = document.metadata_author topic_category = document.category current_keywords = [keyword.name for keyword in document.keywords.all()] 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 and request.POST["category_choice_field"] else None) if hasattr(settings, 'THESAURUS'): tkeywords_form = TKeywordForm(request.POST) else: tkeywords_form = ThesaurusAvailableForm(request.POST, prefix='tkeywords') else: document_form = DocumentForm(instance=document, prefix="resource") document_form.disable_keywords_widget_for_non_superuser(request.user) category_form = CategoryForm( prefix="category_choice_field", initial=topic_category.id if topic_category else None) # Keywords from THESAURUS management doc_tkeywords = document.tkeywords.all() if hasattr(settings, 'THESAURUS') and settings.THESAURUS: warnings.warn( 'The settings for Thesaurus has been moved to Model, \ this feature will be removed in next releases', DeprecationWarning) tkeywords_list = '' lang = 'en' # TODO: use user's language if doc_tkeywords and len(doc_tkeywords) > 0: tkeywords_ids = doc_tkeywords.values_list('id', flat=True) if hasattr(settings, 'THESAURUS') and settings.THESAURUS: el = settings.THESAURUS thesaurus_name = el['name'] try: t = Thesaurus.objects.get(identifier=thesaurus_name) for tk in t.thesaurus.filter(pk__in=tkeywords_ids): tkl = tk.keyword.filter(lang=lang) if len(tkl) > 0: tkl_ids = ",".join( map(str, tkl.values_list('id', flat=True))) tkeywords_list += f",{tkl_ids}" if len( tkeywords_list) > 0 else tkl_ids except Exception: tb = traceback.format_exc() logger.error(tb) tkeywords_form = TKeywordForm(instance=document) else: tkeywords_form = ThesaurusAvailableForm(prefix='tkeywords') # set initial values for thesaurus form for tid in tkeywords_form.fields: values = [] values = [ keyword.id for keyword in doc_tkeywords if int(tid) == keyword.thesaurus.id ] tkeywords_form.fields[tid].initial = values if request.method == "POST" and document_form.is_valid( ) and category_form.is_valid() and tkeywords_form.is_valid(): new_poc = document_form.cleaned_data['poc'] new_author = document_form.cleaned_data['metadata_author'] new_keywords = current_keywords if request.keyword_readonly else document_form.cleaned_data[ 'keywords'] new_regions = document_form.cleaned_data['regions'] new_category = None if category_form and 'category_choice_field' in category_form.cleaned_data and \ category_form.cleaned_data['category_choice_field']: new_category = TopicCategory.objects.get( id=int(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')) 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')) if author_form.has_changed and author_form.is_valid(): new_author = author_form.save() document = document_form.instance if new_poc is not None and new_author is not None: document.poc = new_poc document.metadata_author = new_author document.keywords.clear() document.keywords.add(*new_keywords) document.regions.clear() document.regions.add(*new_regions) document.category = new_category document.save(notify=True) document_form.save_many2many() register_event(request, EventType.EVENT_CHANGE_METADATA, document) if not ajax: return HttpResponseRedirect( reverse('document_detail', args=(document.id, ))) message = document.id try: # Keywords from THESAURUS management # Rewritten to work with updated autocomplete if not tkeywords_form.is_valid(): return HttpResponse( json.dumps({'message': "Invalid thesaurus keywords"}, status_code=400)) thesaurus_setting = getattr(settings, 'THESAURUS', None) if thesaurus_setting: tkeywords_data = tkeywords_form.cleaned_data['tkeywords'] tkeywords_data = tkeywords_data.filter( thesaurus__identifier=thesaurus_setting['name']) document.tkeywords.set(tkeywords_data) elif Thesaurus.objects.all().exists(): fields = tkeywords_form.cleaned_data document.tkeywords.set(tkeywords_form.cleanx(fields)) except Exception: tb = traceback.format_exc() logger.error(tb) 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")) except Exception: all_metadata_author_groups = GroupProfile.objects.exclude( access="private") [ 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: 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 Exception: is_manager = False if not is_manager or not can_change_metadata: if settings.RESOURCE_PUBLISHING: document_form.fields['is_published'].widget.attrs.update( {'disabled': 'true'}) document_form.fields['is_approved'].widget.attrs.update( {'disabled': 'true'}) register_event(request, EventType.EVENT_VIEW_METADATA, document) 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, "tkeywords_form": tkeywords_form, "metadata_author_groups": metadata_author_groups, "TOPICCATEGORY_MANDATORY": getattr(settings, 'TOPICCATEGORY_MANDATORY', False), "GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False), "UI_MANDATORY_FIELDS": [ 'title', 'abstract', 'doi', 'attribution', 'data_quality_statement', 'restriction_code_type' ] })
def document_metadata( request, docid, template='v2/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.user is None: poc_form = ProfileForm( request.POST, prefix="poc", instance=poc) else: poc_form = ProfileForm(request.POST, prefix="poc") 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.has_changed and author_form.is_valid(): new_author = author_form.save() if new_poc is not None and new_author is not None: # rename document file if any title fields changed title_fields = ['category', 'regions', 'datasource', 'title', 'subtitle', 'papersize', 'date', 'edition'] 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 document_form.cleaned_data['regions']]), document_form.cleaned_data['datasource'], slugify(document_form.cleaned_data['title']), slugify(document_form.cleaned_data['subtitle']), document_form.cleaned_data['papersize'], document_form.cleaned_data['date'].strftime('%Y-%m-%d'), document_form.cleaned_data['edition']])), 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.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) return HttpResponseRedirect( reverse( 'document_detail', args=( document.id, ))) 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 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, }))
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.user is None: poc_form = ProfileForm(request.POST, prefix="poc", instance=poc) else: poc_form = ProfileForm(request.POST, prefix="poc") 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.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) the_document.category = new_category the_document.save() return HttpResponseRedirect( reverse('document_detail', args=(document.id, ))) 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 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, }))
def document_metadata( request, docid, template='documents/document_metadata.html'): document = None try: document = _resolve_document( request, docid, 'base.change_resourcebase', _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.user is None: poc_form = ProfileForm( request.POST, prefix="poc", instance=poc) else: poc_form = ProfileForm(request.POST, prefix="poc") 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.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) the_document.category = new_category the_document.save() return HttpResponseRedirect( reverse( 'document_detail', args=( document.id, ))) 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 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, }))
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), })
def document_metadata(request, docid, template='documents/document_metadata.html'): document = Document.objects.get(id=docid) 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.user is None: poc_form = ProfileForm(request.POST, prefix="poc", instance=poc) else: poc_form = ProfileForm(request.POST, prefix="poc") 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.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) the_document.category = new_category the_document.save() return HttpResponseRedirect( reverse('document_detail', args=(document.id, ))) 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 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, }))
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), })