Example #1
0
def create_single_doc(name):
    admin, created = get_user_model().objects.get_or_create(username='******')
    if created:
        admin.is_superuser = True
        admin.first_name = 'admin'
        admin.set_password('admin')
        admin.save()
    test_datetime = datetime.strptime('2020-01-01', '%Y-%m-%d')
    user = get_user_model().objects.get(username='******')
    dd = (name, 'lorem ipsum', name, f'{name}', [
        0, 22, 0, 22], test_datetime, ('populartag',))
    title, abstract, name, alternate, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), start, kws = dd
    logger.debug(f"[SetUp] Add document {title}")
    m = Document(
        title=title,
        abstract=abstract,
        owner=user,
        bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
        ll_bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
        srid='EPSG:4326',
        files=dfile,
        resource_type="document"
    )
    m.save()
    m.set_default_permissions()
    m.clear_dirty_state()
    m.set_processing_state(enumerations.STATE_PROCESSED)
    return m
Example #2
0
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,)))
Example #3
0
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,)))
Example #4
0
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
Example #5
0
def create_document(number):
    """ Creates a new document """
    file_list = glob.glob('%s*.jpg' % doc_path)
    random_index = randint(0, len(file_list) - 1)
    file_uri = file_list[random_index]
    title = 'Document N. %s' % number
    img_filename = '%s_img.jpg' % number
    doc = Document(title=title, owner=get_random_user())
    doc.save()
    with open(file_uri, 'r') as f:
        img_file = File(f)
        doc.doc_file.save(img_filename, img_file, True)
    assign_keywords(doc)
    # regions
    resource = doc.get_self_resource()
    set_resource(resource)
Example #6
0
def create_document(number):
    """ Creates a new document """
    file_list = glob.glob('%s*.jpg' % doc_path)
    random_index = randint(0, len(file_list) -1)
    file_uri = file_list[random_index]
    title = 'Document N. %s' % number
    img_filename = '%s_img.jpg' % number
    doc = Document(title=title, owner=get_random_user())
    doc.save()
    with open(file_uri, 'r') as f:
        img_file = File(f) 
        doc.doc_file.save(img_filename, img_file, True)
    assign_keywords(doc)
    # regions
    resource = doc.get_self_resource()
    set_resource(resource)
Example #7
0
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, )))
Example #8
0
    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')
Example #9
0
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,))}))
Example #10
0
def create_document(number):
    print 'Generating image %s' % number
    admin = Profile.objects.filter(username='******')[0]

    file_list = (
                    '1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg',
                    '8.jpg', '9.png', '10.jpg', '11.jpg',)
    random_index = randint(0, 10)
    file_uri = '/home/capooti/Desktop/maps/%s' % file_list[random_index]
    title = 'Document N. %s' % number
    img_filename = '%s_img.jpg' % number

    doc = Document(title=title, owner=admin)
    doc.save()
    with open(file_uri, 'r') as f:
        img_file = File(f)
        doc.doc_file.save(img_filename, img_file, True)

    base = doc.get_self_resource()
    random_index = randint(0, 18)
    tc = TopicCategory.objects.all()[random_index]
    base.category = tc
    base.save()
Example #11
0
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)
Example #12
0
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, ))
            }))
Example #13
0
                        newdata = Document.objects.get(
                            doc_file__icontains=row[18])
                        # alternative to newdata.update(**kwargs)
                        for (key, value) in kwargs.items():
                            setattr(newdata, key, value)
                    except Document.DoesNotExist:
                        raise Exception(
                            'previous_file_name \'%s\' not found in database.'
                            % (row[18]))
                    except Document.MultipleObjectsReturned:
                        raise Exception(
                            'previous_file_name \'%s\' returns multiple row.' %
                            (row[18]))
                else:
                    # no prev_file_name
                    newdata = Document(**kwargs)

                fullpath_dest = os.path.normpath(
                    os.path.join(path_dest, row[10], row[0]))
                # print 'fullpath_dest', fullpath_dest

                if not os.path.exists(os.path.dirname(fullpath_dest)):
                    os.makedirs(os.path.dirname(fullpath_dest))

                os.rename(fullpath_source, fullpath_dest)
                newdata.save()
                valid_keywords = filter(None, row[7].split("-"))
                newdata.keywords.add(*valid_keywords)
                row[16] = newdata.id
                loc = Region.objects.get(pk=row[4])
                newdata.regions.add(loc)
					'abstract':row[14]
				}
				if (len(row) > 18) and (row[18]):
					# prev_file_name exist
					try:
						newdata = Document.objects.get(doc_file__icontains=row[18])
						# alternative to newdata.update(**kwargs)
						for (key, value) in kwargs.items():
							setattr(newdata, key, value)
					except Document.DoesNotExist:
						raise Exception('previous_file_name \'%s\' not found in database.'%(row[18]))
					except Document.MultipleObjectsReturned:
						raise Exception('previous_file_name \'%s\' returns multiple row.'%(row[18]))
				else:
					# no prev_file_name
					newdata = Document(**kwargs)

				os.rename((fullpath_source), (os.path.normpath(path_dest+row[10]+'/'+row[0])))
				newdata.save()
				valid_keywords = filter(None, row[7].split("-"))
				newdata.keywords.add(*valid_keywords)
				row[16]=newdata.id
				loc = Region.objects.get(pk=row[4])
				newdata.regions.add(loc)

				# delete row based on row[0]
				for delidx, delrow in enumerate(remaining):
					if delrow and (delrow[0] == row[0]):
						del remaining[delidx]
						break
Example #15
0
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)
        )
Example #16
0
def create_models(type=None):
    from django.contrib.auth.models import Group
    map_data, user_data, people_data, layer_data, document_data = create_fixtures()
    anonymous_group, created = Group.objects.get_or_create(name='anonymous')
    u, _ = get_user_model().objects.get_or_create(username='******', is_superuser=True, first_name='admin')
    u.set_password('admin')
    u.save()
    users = []

    for ud, pd in zip(user_data, cycle(people_data)):
        user_name, password, first_name, last_name = ud
        u, created = get_user_model().objects.get_or_create(username=user_name)
        if created:
            u.first_name = first_name
            u.last_name = last_name
            u.save()
        u.groups.add(anonymous_group)
        users.append(u)

    get_user_model().objects.get(username='******').groups.add(anonymous_group)

    if not type or type == 'map':
        for md, user in zip(map_data, cycle(users)):
            title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = md
            m = Map(title=title,
                    abstract=abstract,
                    zoom=4,
                    projection='EPSG:4326',
                    center_x=42,
                    center_y=-73,
                    owner=user,
                    bbox_x0=bbox_x0,
                    bbox_x1=bbox_x1,
                    bbox_y0=bbox_y0,
                    bbox_y1=bbox_y1,
                    category=category,
                    )
            m.save()
            for kw in kws:
                m.keywords.add(kw)
                m.save()

    if not type or type == 'document':
        for dd, user in zip(document_data, cycle(users)):
            title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = dd
            m = Document(title=title,
                         abstract=abstract,
                         owner=user,
                         bbox_x0=bbox_x0,
                         bbox_x1=bbox_x1,
                         bbox_y0=bbox_y0,
                         bbox_y1=bbox_y1,
                         category=category,
                         doc_file=f)
            m.save()
            for kw in kws:
                m.keywords.add(kw)
                m.save()

    if not type or type == 'layer':
        for ld, owner, storeType in zip(layer_data, cycle(users), cycle(('coverageStore', 'dataStore'))):
            title, abstract, name, alternate, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), start, kws, category = ld
            end = start + timedelta(days=365)
            l = Layer(title=title,
                      abstract=abstract,
                      name=name,
                      alternate=alternate,
                      bbox_x0=bbox_x0,
                      bbox_x1=bbox_x1,
                      bbox_y0=bbox_y0,
                      bbox_y1=bbox_y1,
                      uuid=str(uuid4()),
                      owner=owner,
                      temporal_extent_start=start,
                      temporal_extent_end=end,
                      date=start,
                      storeType=storeType,
                      category=category,
                      )
            l.save()
            for kw in kws:
                l.keywords.add(kw)
                l.save()
Example #17
0
def create_models(type=None, integration=False):
    map_data, user_data, people_data, layer_data, document_data = create_fixtures(
    )
    anonymous_group, created = Group.objects.get_or_create(name='anonymous')
    with transaction.atomic():
        logger.info("[SetUp] Get or create user admin")
        u = get_user_model().objects.filter(username='******').first()
        if not u:
            try:
                u = get_user_model().objects.create(username='******',
                                                    is_superuser=True,
                                                    first_name='admin')
            except Exception:
                raise
        if u:
            u.set_password('admin')
            u.save()

    users = []
    for ud, pd in zip(user_data, cycle(people_data)):
        user_name, password, first_name, last_name = ud
        with transaction.atomic():
            try:
                logger.info(f"[SetUp] Get or create user {user_name}")
                u, created = get_user_model().objects.get_or_create(
                    username=user_name)
                if created:
                    u.set_password(password)
                    u.first_name = first_name
                    u.last_name = last_name
                    u.save()
                u.groups.add(anonymous_group)
                users.append(u)
            except Exception:
                raise

    logger.info(f"[SetUp] Add group {anonymous_group}")
    get_user_model().objects.get(
        username='******').groups.add(anonymous_group)

    obj_ids = []
    from geonode.utils import DisableDjangoSignals
    with DisableDjangoSignals(skip=integration):
        if not type or ensure_string(type) == 'map':
            for md, user in zip(map_data, cycle(users)):
                title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0,
                                       bbox_y1), category = md
                logger.info(f"[SetUp] Add map {title}")
                m = Map(title=title,
                        abstract=abstract,
                        zoom=4,
                        projection='EPSG:4326',
                        center_x=42,
                        center_y=-73,
                        owner=user,
                        bbox_polygon=Polygon.from_bbox(
                            (bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        srid='EPSG:4326',
                        category=category)
                m.save()
                m.set_default_permissions()
                obj_ids.append(m.id)
                for kw in kws:
                    m.keywords.add(kw)
                    m.save()

        if not type or ensure_string(type) == 'document':
            for dd, user in zip(document_data, cycle(users)):
                title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0,
                                       bbox_y1), category = dd
                logger.info(f"[SetUp] Add document {title}")
                m = Document(title=title,
                             abstract=abstract,
                             owner=user,
                             bbox_polygon=Polygon.from_bbox(
                                 (bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                             srid='EPSG:4326',
                             category=category,
                             doc_file=f)
                m.save()
                m.set_default_permissions()
                obj_ids.append(m.id)
                for kw in kws:
                    m.keywords.add(kw)
                    m.save()

        if not type or ensure_string(type) == 'layer':
            for ld, owner, storeType in zip(
                    layer_data, cycle(users),
                    cycle(('coverageStore', 'dataStore'))):
                title, abstract, name, alternate, (
                    bbox_x0, bbox_x1, bbox_y0,
                    bbox_y1), start, kws, category = ld
                end = start + timedelta(days=365)
                logger.info(f"[SetUp] Add layer {title}")
                layer = Layer(title=title,
                              abstract=abstract,
                              name=name,
                              alternate=alternate,
                              bbox_polygon=Polygon.from_bbox(
                                  (bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                              srid='EPSG:4326',
                              uuid=str(uuid4()),
                              owner=owner,
                              temporal_extent_start=start,
                              temporal_extent_end=end,
                              date=start,
                              storeType=storeType,
                              category=category)
                layer.save()
                layer.set_default_permissions()
                obj_ids.append(layer.id)
                for kw in kws:
                    layer.keywords.add(kw)
                    layer.save()
    return obj_ids
Example #18
0
def create_models(type=None, integration=False):
    users = []
    obj_ids = []
    with transaction.atomic():
        map_data, user_data, people_data, dataset_data, document_data = create_fixtures()
        registeredmembers_group, created = Group.objects.get_or_create(name='registered-members')
        anonymous_group, created = Group.objects.get_or_create(name='anonymous')
        cont_group, created = Group.objects.get_or_create(name='contributors')
        perm = Permission.objects.get(codename='add_resourcebase')
        cont_group.permissions.add(perm)
        logger.debug("[SetUp] Get or create user admin")
        u, created = get_user_model().objects.get_or_create(username='******')
        u.set_password('admin')
        u.is_superuser = True
        u.first_name = 'admin'
        u.save()
        u.groups.add(anonymous_group)
        users.append(u)

        for ud, pd in zip(user_data, cycle(people_data)):
            user_name, password, first_name, last_name = ud
            logger.debug(f"[SetUp] Get or create user {user_name}")
            u, created = get_user_model().objects.get_or_create(username=user_name)
            u.set_password(password)
            u.first_name = first_name
            u.last_name = last_name
            u.save()
            u.groups.add(anonymous_group)

            if not (u.is_superuser or u.is_staff or u.is_anonymous):
                u.groups.add(cont_group)
            users.append(u)

        logger.debug(f"[SetUp] Add group {anonymous_group}")
        get_user_model().objects.get(username='******').groups.add(anonymous_group)

        from geonode.utils import DisableDjangoSignals
        with DisableDjangoSignals(skip=integration):
            if not type or ensure_string(type) == 'map':
                for md, user in zip(map_data, cycle(users)):
                    title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = md
                    logger.debug(f"[SetUp] Add map {title}")
                    m = Map(
                        title=title,
                        abstract=abstract,
                        zoom=4,
                        projection='EPSG:4326',
                        center_x=42,
                        center_y=-73,
                        owner=user,
                        bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        ll_bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        srid='EPSG:4326',
                        category=category,
                        metadata_only=title == 'map metadata true'
                    )
                    m.save()
                    m.set_default_permissions()
                    m.clear_dirty_state()
                    m.set_processing_state(enumerations.STATE_PROCESSED)
                    obj_ids.append(m.id)
                    for kw in kws:
                        m.keywords.add(kw)
                        m.save()

            if not type or ensure_string(type) == 'document':
                for dd, user in zip(document_data, cycle(users)):
                    title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = dd
                    logger.debug(f"[SetUp] Add document {title}")
                    m = Document(
                        title=title,
                        abstract=abstract,
                        owner=user,
                        bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        ll_bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        srid='EPSG:4326',
                        files=dfile,
                        extension="gif",
                        metadata_only=title == 'doc metadata true'
                    )
                    m.save()
                    m.set_default_permissions()
                    m.clear_dirty_state()
                    m.set_processing_state(enumerations.STATE_PROCESSED)
                    obj_ids.append(m.id)
                    for kw in kws:
                        m.keywords.add(kw)
                        m.save()

            if not type or ensure_string(type) == 'dataset':
                for ld, owner, subtype in zip(dataset_data, cycle(users), cycle(('raster', 'vector'))):
                    title, abstract, name, alternate, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), start, kws, category = ld
                    end = start + timedelta(days=365)
                    logger.debug(f"[SetUp] Add dataset {title}")
                    dataset = Dataset(
                        title=title,
                        abstract=abstract,
                        name=name,
                        alternate=alternate,
                        bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        ll_bbox_polygon=Polygon.from_bbox((bbox_x0, bbox_y0, bbox_x1, bbox_y1)),
                        srid='EPSG:4326',
                        uuid=str(uuid4()),
                        owner=owner,
                        temporal_extent_start=start,
                        temporal_extent_end=end,
                        date=start,
                        subtype=subtype,
                        category=category,
                        metadata_only=title == 'dataset metadata true'
                    )
                    dataset.save()
                    dataset.set_default_permissions()
                    dataset.clear_dirty_state()
                    dataset.set_processing_state(enumerations.STATE_PROCESSED)
                    obj_ids.append(dataset.id)
                    for kw in kws:
                        dataset.keywords.add(kw)
                        dataset.save()
    return obj_ids
def create_models(type=None):
    map_data, user_data, people_data, layer_data, document_data = create_fixtures(
    )

    u, _ = User.objects.get_or_create(username='******', is_superuser=True)
    u.set_password('admin')
    u.save()
    users = []

    for ud, pd in zip(user_data, cycle(people_data)):
        user_name, password, first_name, last_name = ud
        profile = pd[0]
        u, created = User.objects.get_or_create(username=user_name)
        if created:
            u.first_name = first_name
            u.last_name = last_name
            u.save()
            contact = Profile.objects.get(user=u)
            contact.profile = profile
            contact.save()
        users.append(u)

    if not type or type == 'map':
        for md, user in zip(map_data, cycle(users)):
            title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0,
                                   bbox_y1), category = md
            m = Map(
                title=title,
                abstract=abstract,
                zoom=4,
                projection='EPSG:4326',
                center_x=42,
                center_y=-73,
                owner=user,
                bbox_x0=bbox_x0,
                bbox_x1=bbox_x1,
                bbox_y0=bbox_y0,
                bbox_y1=bbox_y1,
                category=category,
            )
            m.save()
            for kw in kws:
                m.keywords.add(kw)
                m.save()

    if not type or type == 'document':
        for dd, user in zip(document_data, cycle(users)):
            title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0,
                                   bbox_y1), category = dd
            m = Document(title=title,
                         abstract=abstract,
                         owner=user,
                         bbox_x0=bbox_x0,
                         bbox_x1=bbox_x1,
                         bbox_y0=bbox_y0,
                         bbox_y1=bbox_y1,
                         category=category,
                         doc_file=f)
            m.save()
            for kw in kws:
                m.keywords.add(kw)
                m.save()

    if not type or type == 'layer':
        for ld, owner, storeType in zip(layer_data, cycle(users),
                                        cycle(('coverageStore', 'dataStore'))):
            title, abstract, name, typename, (bbox_x0, bbox_x1, bbox_y0,
                                              bbox_y1), dt, kws, category = ld
            year, month, day = map(int, (dt[:4], dt[4:6], dt[6:]))
            start = datetime(year, month, day)
            end = start + timedelta(days=365)
            l = Layer(
                title=title,
                abstract=abstract,
                name=name,
                typename=typename,
                bbox_x0=bbox_x0,
                bbox_x1=bbox_x1,
                bbox_y0=bbox_y0,
                bbox_y1=bbox_y1,
                uuid=str(uuid4()),
                owner=owner,
                temporal_extent_start=start,
                temporal_extent_end=end,
                date=start,
                storeType=storeType,
                category=category,
            )
            l.save()
            for kw in kws:
                l.keywords.add(kw)
                l.save()
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()