Skip to content

Instantly share code, notes, and snippets.

@sopac
Last active August 2, 2016 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sopac/2ef0a2cbdf50a21d25af to your computer and use it in GitHub Desktop.
Save sopac/2ef0a2cbdf50a21d25af to your computer and use it in GitHub Desktop.
Geonode Batch Document Load
##sudo geonode shell
##execfile("secure_reports.py")
from django.test import TestCase
from django.conf import settings
from django.test.client import Client
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from geonode.maps.models import Map
from geonode.documents.models import Document
from geonode.security.enumerations import ANONYMOUS_USERS, AUTHENTICATED_USERS
import geonode.documents.views
import geonode.security
from geonode.search.populate_search_test_data import create_models
import os
print "Starting..."
# Inherited
# - LEVEL_NONE = _none
# Layer
# - LEVEL_READ = document_read
# - LEVEL_WRITE = document_readwrite
# - LEVEL_ADMIN = document_admin
#secure existing documents
perm_spec = {"anonymous":"_none","authenticated": Document.LEVEL_READ,"users":[["sachin","document_readwrite"]]}
queryset = Document.objects.all()
for d in queryset:
geonode.documents.views.document_set_permissions(d, perm_spec)
print "Finished"
##sudo geonode shell
##execfile("upload_reports.py")
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth.models import User
from geonode.documents.models import Document
from geonode.base.models import ResourceBase
import os
print "Starting..."
#delete existing documents
queryset = Document.objects.all()
for d in queryset:
rb = ResourceBase.objects.get(id=d.resourcebase_ptr_id)
d.delete()
rb.delete()
for file in os.listdir("/home/solgeo/OUT/Documents"):
print file
name = file.replace(".pdf", "")
t = open('/home/solgeo/OUT/Documents' + file, 'r')
f = SimpleUploadedFile(file, t.read(), 'application/pdf')
superuser = User.objects.get(id=2)
if len(name) >= 100: name = name[:99]
c = Document.objects.create(doc_file=f,owner=superuser, title=name)
c.set_default_permissions()
print "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment