def document_upload(request): if request.method == 'GET': return render_to_response('documents/document_upload.html', RequestContext(request) ) elif request.method == 'POST': try: content_type = ContentType.objects.get(name=request.POST['type']) object_id = request.POST['q'] except: content_type = None object_id = None title = request.POST['title'] doc_file = request.FILES['file'] if len(request.POST['title'])==0: return HttpResponse(_('You need to provide a document title.')) if not os.path.splitext(doc_file.name)[1].lower()[1:] in ALLOWED_DOC_TYPES: return HttpResponse(_('This file type is not allowed.')) if not doc_file.size < settings.MAX_DOCUMENT_SIZE * 1024 * 1024: return HttpResponse(_('This file is too big.')) document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() permissionsStr = request.POST['permissions'] permissions = json.loads(permissionsStr) document_set_permissions(document, permissions) return HttpResponseRedirect(reverse('document_metadata', args=(document.id,)))
def document_upload(request): if request.method == 'GET': return render_to_response('documents/document_upload.html', RequestContext(request), context_instance=RequestContext(request) ) elif request.method == 'POST': try: content_type = ContentType.objects.get(name=request.POST['ctype']) object_id = request.POST['objid'] except: content_type = None object_id = None try: int(object_id) except: if object_id is not None: object_id = Layer.objects.get(uuid=object_id).id doc_file = request.FILES['file'] title = request.POST['title'] document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() document.set_default_permissions() permissionsStr = request.POST['permissions'] permissions = json.loads(permissionsStr) set_document_permissions(document, permissions) return HttpResponse(json.dumps({'success': True,'redirect_to': reverse('document_metadata', args=(document.id,))}))
def create_letter_document(request_letter, profile=None, profile_request=None): if not profile and not profile_request: raise PermissionDenied details = None letter_owner = None permissions = None if profile: pprint("profile is not empty") details = profile letter_owner = profile permissions = {"users":{profile.username:["view_resourcebase","download_resourcebase"]}} else: pprint("profile request is not empty") details = profile_request letter_owner, created = Profile.objects.get_or_create(username='******') permissions = {"users":{"dataRegistrationUploader":["view_resourcebase"]}} requester_name = unidecode(details.first_name+" " +details.last_name) letter = Document() letter.owner = letter_owner letter.doc_file = request_letter letter.title = requester_name + " Request Letter " +datetime.datetime.now().strftime("%Y-%m-%d") letter.is_published = False letter.save() letter.set_permissions(permissions) return letter
def document_upload(request): if request.method == "GET": return render_to_response( "documents/document_upload.html", RequestContext(request), context_instance=RequestContext(request) ) elif request.method == "POST": try: content_type = ContentType.objects.get(name=request.POST["type"]) object_id = request.POST["q"] except: content_type = None object_id = None if not os.path.splitext(request.FILES["file"].name)[1].lower()[1:] in ALLOWED_DOC_TYPES: return HttpResponse("This file type is not allowed.") if not request.FILES["file"].size < settings.MAX_DOCUMENT_SIZE * 1024 * 1024: return HttpResponse("This file is too big.") doc_file = request.FILES["file"] title = request.POST["title"] document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() permissionsStr = request.POST["permissions"] permissions = json.loads(permissionsStr) document_set_permissions(document, permissions) return HttpResponseRedirect(reverse("document_metadata", args=(document.id,)))
def import_document(self, filename, owner): """ Import document on the url list on the file filename should be always in the fixtures :param filename: name of file that will be imported :type filename: str :param owner: user as the owner :type owner: User """ print('--------------------------------------------------------------') print('Importing {}.'.format(filename)) print('--------------------------------------------------------------') django_root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) _file = os.path.join(django_root, 'fixtures', filename) f = open(_file, 'r') lines = f.readlines() for line in lines: line = line.replace('\n', '') print('-----------------------') print('check file : {}'.format(line)) downloaded_filename = os.path.basename(line) downloaded_filename, ext = os.path.splitext(downloaded_filename) downloaded_filename = downloaded_filename + ext.split('_')[0] # skip request if document exist if Document.objects.filter(title=downloaded_filename, owner=owner).count() == 0: # request the file temp_file = tempfile.NamedTemporaryFile() document = Document() document.owner = owner document.title = downloaded_filename document.doc_url = line # Read the streamed image in sections response = requests.get(line, stream=True) if response.status_code == requests.codes.ok: for block in response.iter_content(1024 * 8): if not block: break temp_file.write(block) # safe it to document document.doc_file.save(downloaded_filename, files.File(temp_file)) else: print('link is dead so just save the url') document.save() else: print('skip. found document with same name and owner')
def document_upload(request): if request.method == 'GET': return render_to_response('documents/document_upload.html', RequestContext(request), context_instance=RequestContext(request)) elif request.method == 'POST': try: content_type = ContentType.objects.get(name=request.POST['type']) object_id = request.POST['q'] except: content_type = None object_id = None if not os.path.splitext(request.FILES['file'].name)[1].lower( )[1:] in ALLOWED_DOC_TYPES: return HttpResponse( json.dumps({ 'success': False, 'errormsgs': ['This file type is not allowed.'] })) if not request.FILES[ 'file'].size < settings.MAX_DOCUMENT_SIZE * 1024 * 1024: return HttpResponse( json.dumps({ 'success': False, 'errormsgs': ['This file is too big.'] })) doc_file = request.FILES['file'] title = request.POST['title'] document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() permissionsStr = request.POST['permissions'] permissions = json.loads(permissionsStr) document_set_permissions(document, permissions) return HttpResponseRedirect( reverse('document_metadata', args=(document.id, )))
def document_upload(request): if request.method == 'GET': return render_to_response('documents/document_upload.html', RequestContext(request), context_instance=RequestContext(request)) elif request.method == 'POST': try: content_type = ContentType.objects.get(name=request.POST['ctype']) object_id = request.POST['objid'] except: content_type = None object_id = None try: int(object_id) except: if object_id is not None: object_id = Layer.objects.get(uuid=object_id).id doc_file = request.FILES['file'] title = request.POST['title'] document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() document.set_default_permissions() permissionsStr = request.POST['permissions'] permissions = json.loads(permissionsStr) set_document_permissions(document, permissions) return HttpResponse( json.dumps({ 'success': True, 'redirect_to': reverse('document_metadata', args=(document.id, )) }))
def update_datarequest_obj(datarequest=None, parameter_dict=None, interest_layer=None, request_letter = None): if datarequest is None or parameter_dict is None or request_letter is None: raise HttpResponseBadRequest ### Updating the other fields of the request datarequest.project_summary = parameter_dict['project_summary'] datarequest.data_type_requested = parameter_dict['data_type_requested'] if parameter_dict['purpose'] == 'other': datarequest.purpose = parameter_dict['purpose_other'] else: datarequest.purpose = parameter_dict['purpose'] datarequest.intended_use_of_dataset = parameter_dict['intended_use_of_dataset'] datarequest.organization_type = parameter_dict['organization_type'] datarequest.request_level = parameter_dict['request_level'] datarequest.funding_source = parameter_dict['funding_source'] datarequest.is_consultant = parameter_dict['is_consultant'] if interest_layer: datarequest.jurisdiction_shapefile = interest_layer requester_name = datarequest.first_name+" "+datarequest.middle_name+" "+datarequest.last_name letter = Document() letter_owner, created = Profile.objects.get_or_create(username='******') letter.owner = letter_owner letter.doc_file = request_letter letter.title = requester_name+ " Request Letter " +datetime.datetime.now().strftime("%Y-%m-%d") letter.is_published = False letter.save() letter.set_permissions( {"users":{"dataRegistrationUploader":["view_resourcebase"]}}) datarequest.request_letter =letter; datarequest.save() return (datarequest, letter)
def document_update(request, id=None, template_name='wfpdocs/document_form.html'): wfpdoc = None if id: wfpdoc = get_object_or_404(WFPDocument, pk=id) if request.method == 'POST': try: content_type = ContentType.objects.get(name=request.POST['type']) object_id = request.POST['q'] except: content_type = None object_id = None title = request.POST['title'] doc_file = None if 'file' in request.FILES: doc_file = request.FILES['file'] if len(request.POST['title'])==0: return HttpResponse(_('You need to provide a document title.')) if not os.path.splitext(doc_file.name)[1].lower()[1:] in ALLOWED_DOC_TYPES: return HttpResponse(_('This file type is not allowed.')) if not doc_file.size < settings.MAX_DOCUMENT_SIZE * 1024 * 1024: return HttpResponse(_('This file is too big.')) # map document form = WFPDocumentForm(request.POST) if form.is_valid(): source = form.cleaned_data.get('source') publication_date = form.cleaned_data.get('publication_date') orientation = form.cleaned_data.get('orientation') page_format = form.cleaned_data.get('page_format') categories = form.cleaned_data.get('categories') regions = form.cleaned_data.get('regions') last_version = form.cleaned_data.get('last_version') if wfpdoc is None: wfpdoc = WFPDocument() wfpdoc.source = source wfpdoc.orientation = orientation wfpdoc.page_format = page_format wfpdoc.last_version = last_version # if we are creating the static map, we need to create the document as well if not id: document = Document(content_type=content_type, object_id=object_id, title=title, doc_file=doc_file) document.owner = request.user document.save() wfpdoc.document = document else: document = wfpdoc.document # title=title, doc_file=doc_file, date=publication_date, regions=regions document.owner = request.user document.title = title if doc_file: document.doc_file = doc_file document.date = publication_date document.regions = regions document.save() document.update_thumbnail() #wfpdoc = WFPDocument(source = source, orientation=orientation, # page_format=page_format, document=document) wfpdoc.save() wfpdoc.categories = categories return HttpResponseRedirect(reverse('wfpdocs-browse')) else: if wfpdoc: form = WFPDocumentForm(instance=wfpdoc, initial={'regions': wfpdoc.document.regions.all()}) else: form = WFPDocumentForm() # some field in the form must be manually populated return render_to_response( 'wfpdocs/document_form.html', { 'form': form, }, RequestContext(request) )
from django.core.files import File from geonode.documents.models import Document from django.contrib.auth.models import User from datetime import datetime base_dir = os.getcwd() base_url = "https://s3-us-west-2.amazonaws.com/arcmaps/haiyan/" permissions_string='{"anonymous":"document_readonly","authenticated":"document_readwrite","users":[]}' csrftoken='' with open('centroids.json', 'r') as f: maps = json.load(f) for map in maps: url = base_url + map['filename'] + '.pdf' print url filename = wget.download(url) print filename file = open(filename) djangofile = File(file) doc = Document() doc.title = map['title'] doc.abstract = map['description'] doc.doc_file = djangofile doc.owner = User.objects.all()[0] doc.date = datetime.strptime(map['date'], '%m/%d/%Y') doc.bbox_x0 = map['longitude'] doc.bbox_y0 = map['latitude'] doc.bbox_x1 = map['longitude'] doc.bbox_y1 = map['latitude'] doc.save()