Exemplo n.º 1
0
def index_resources():
    """
    Deletes any existing indicies from elasticsearch related to resources 
    and then indexes all resources from the database

    """

    result_summary = {}
    se = SearchEngineFactory().create()

    # clear existing indexes
    for index_type in ['resource_relations', 'entity', 'resource', 'maplayers']:
        se.delete_index(index=index_type)
    se.delete(index='term', body='{"query":{"bool":{"must":[{"constant_score":{"filter":{"missing":{"field":"value.options.conceptid"}}}}],"must_not":[],"should":[]}}}')

    Resource().prepare_term_index(create=True)

    cursor = connection.cursor()
    cursor.execute("""select entitytypeid from data.entity_types where isresource = TRUE""")
    resource_types = cursor.fetchall()
    Resource().prepare_resource_relations_index(create=True)

    for resource_type in resource_types:
        Resource().prepare_search_index(resource_type[0], create=True)
    
    index_resources_by_type(resource_types, result_summary)

    se.es.indices.refresh(index='entity')
    for resource_type in resource_types:
        result_summary[resource_type[0]]['indexed'] = se.es.count(index="entity", doc_type=resource_type[0])['count']

    print '\nResource Index Results:'
    for k, v in result_summary.iteritems():
        status = 'Passed' if v['database'] == v['indexed'] else 'failed'
        print "Status: {0}, Resource Type: {1}, In Database: {2}, Indexed: {3}".format(status, k, v['database'], v['indexed'])
Exemplo n.º 2
0
 def delete(self):
     from arches.app.search.search_engine_factory import SearchEngineFactory
     se = SearchEngineFactory().create()
     se.delete(index='resource_relations',
               doc_type='all',
               id=self.resourcexid)
     super(ResourceXResource, self).delete()
Exemplo n.º 3
0
    def delete(self, user={}, note=''):
        """
        Deletes a single resource and any related indexed data

        """

        se = SearchEngineFactory().create()
        related_resources = self.get_related_resources(lang="en-US",
                                                       start=0,
                                                       limit=1000)
        for rr in related_resources['resource_relationships']:
            models.ResourceXResource.objects.get(pk=rr['resourcexid']).delete()
        query = Query(se)
        bool_query = Bool()
        bool_query.filter(
            Terms(field='resourceinstanceid', terms=[self.resourceinstanceid]))
        query.add_query(bool_query)
        results = query.search(index='strings',
                               doc_type='term')['hits']['hits']
        for result in results:
            se.delete(index='strings', doc_type='term', id=result['_id'])
        se.delete(index='resource',
                  doc_type=str(self.graph_id),
                  id=self.resourceinstanceid)
        self.save_edit(edit_type='delete')
        super(Resource, self).delete()
Exemplo n.º 4
0
    def delete(self, *args, **kwargs):
        se = SearchEngineFactory().create()
        request = kwargs.pop("request", None)
        provisional_edit_log_details = kwargs.pop("provisional_edit_log_details", None)
        for tile in self.tiles:
            tile.delete(*args, request=request, **kwargs)
        try:
            user = request.user
            user_is_reviewer = user_is_resource_reviewer(user)
        except AttributeError:  # no user
            user = None
            user_is_reviewer = True

        if user_is_reviewer is True or self.user_owns_provisional(user):
            query = Query(se)
            bool_query = Bool()
            bool_query.filter(Terms(field="tileid", terms=[self.tileid]))
            query.add_query(bool_query)
            results = query.search(index="terms")["hits"]["hits"]

            for result in results:
                se.delete(index="terms", id=result["_id"])

            self.__preDelete(request)
            self.save_edit(
                user=request.user, edit_type="tile delete", old_value=self.data, provisional_edit_log_details=provisional_edit_log_details
            )
            super(Tile, self).delete(*args, **kwargs)
            resource = Resource.objects.get(resourceinstanceid=self.resourceinstance.resourceinstanceid)
            resource.index()

        else:
            self.apply_provisional_edit(user, data={}, action="delete")
            super(Tile, self).save(*args, **kwargs)
Exemplo n.º 5
0
def related_resources(request, resourceid):

    ## get allowed resource types based on permissions
    allowedtypes = get_allowed_types(request)
    is_anon = False
    if request.user.username == "anonymous":
        is_anon = True
    
    if request.method == 'GET':
        lang = request.GET.get('lang', settings.LANGUAGE_CODE)
        start = request.GET.get('start', 0)
        resources = get_related_resources(resourceid, lang, start=start, limit=15, allowedtypes=allowedtypes, is_anon=is_anon)
        return JSONResponse(resources, indent=4)
    
    if 'edit' in request.user.user_groups and request.method == 'DELETE':
        se = SearchEngineFactory().create()
        data = JSONDeserializer().deserialize(request.body) 
        entityid1 = data.get('entityid1')
        entityid2 = data.get('entityid2')
        resourcexid = data.get('resourcexid')
        realtionshiptype = data.get('realtionshiptype')
        resource = Resource(entityid1)
        resource.delete_resource_relationship(entityid2, realtionshiptype)
        se.delete(index='resource_relations', doc_type='all', id=resourcexid)
        return JSONResponse({ 'success': True })
Exemplo n.º 6
0
def index_resources():
    """
    Deletes any existing indicies from elasticsearch related to resources 
    and then indexes all resources from the database

    """

    result_summary = {}
    se = SearchEngineFactory().create()

    # clear existing indexes
    for index_type in ['resource_relations', 'entity', 'resource', 'maplayers']:
        se.delete_index(index=index_type)
    se.delete(index='term', body='{"query":{"bool":{"must":[{"constant_score":{"filter":{"missing":{"field":"value.options.conceptid"}}}}],"must_not":[],"should":[]}}}')

    Resource().prepare_term_index(create=True)

    cursor = connection.cursor()
    cursor.execute("""select entitytypeid from data.entity_types where isresource = TRUE""")
    resource_types = cursor.fetchall()
    Resource().prepare_resource_relations_index(create=True)

    for resource_type in resource_types:
        Resource().prepare_search_index(resource_type[0], create=True)
    
    index_resources_by_type(resource_types, result_summary)

    se.es.indices.refresh(index='entity')
    for resource_type in resource_types:
        result_summary[resource_type[0]]['indexed'] = se.es.count(index="entity", doc_type=resource_type[0])['count']

    print '\nResource Index Results:'
    for k, v in result_summary.iteritems():
        status = 'Passed' if v['database'] == v['indexed'] else 'failed'
        print "Status: {0}, Resource Type: {1}, In Database: {2}, Indexed: {3}".format(status, k, v['database'], v['indexed'])
Exemplo n.º 7
0
    def delete(self, *args, **kwargs):
        se = SearchEngineFactory().create()
        request = kwargs.pop('request', None)
        provisional_edit_log_details = kwargs.pop('provisional_edit_log_details', None)
        for tile in self.tiles:
            tile.delete(*args, request=request, **kwargs)
        try:
            user = request.user
            user_is_reviewer = request.user.groups.filter(name='Resource Reviewer').exists()
        except AttributeError: #no user
            user = None

        if user_is_reviewer is True or self.user_owns_provisional(user):
            query = Query(se)
            bool_query = Bool()
            bool_query.filter(Terms(field='tileid', terms=[self.tileid]))
            query.add_query(bool_query)
            results = query.search(index='terms')['hits']['hits']

            for result in results:
                se.delete(index='terms', id=result['_id'])

            self.__preDelete(request)
            self.save_edit(
                user=request.user,
                edit_type='tile delete',
                old_value=self.data,
                provisional_edit_log_details=provisional_edit_log_details)
            super(Tile, self).delete(*args, **kwargs)
            resource = Resource.objects.get(resourceinstanceid=self.resourceinstance.resourceinstanceid)
            resource.index()

        else:
            self.apply_provisional_edit(user, data={}, action='delete')
            super(Tile, self).save(*args, **kwargs)
Exemplo n.º 8
0
def resource_manager(request, resourcetypeid='', form_id='default', resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    if form_id == 'default':
        form_id = resource.form_groups[0]['forms'][0]['id']

    form = resource.get_form(form_id)

    if request.method == 'DELETE':
        resource.delete_index()
        se = SearchEngineFactory().create()
        realtionships = resource.get_related_resources(return_entities=False)
        for realtionship in realtionships:
            se.delete(index='resource_relations', doc_type='all', id=realtionship.resourcexid)
            realtionship.delete()
        resource.delete()
        return JSONResponse({ 'success': True })

    if request.method == 'POST':
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        form.update(data, request.FILES)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid

            return redirect('resource_manager', resourcetypeid=resourcetypeid, form_id=form_id, resourceid=resourceid)

    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))
    
    if request.method == 'GET':
        if form != None:
            lang = request.GET.get('lang', settings.LANGUAGE_CODE)
            form.load(lang)
            return render(request, 'resource-manager.htm', {
                'form': form,
                'formdata': JSONSerializer().serialize(form.data),
                'form_template': 'views/forms/' + form_id + '.htm',
                'form_id': form_id,
                'resourcetypeid': resourcetypeid,
                'resourceid': resourceid,
                'main_script': 'resource-manager',
                'active_page': 'ResourceManger',
                'resource': resource,
                'resource_name': resource.get_primary_name(),
                'resource_type_name': resource.get_type_name(),
                'form_groups': resource.form_groups,
                'min_date': min_max_dates['val__min'].year if min_max_dates['val__min'] != None else 0,
                'max_date': min_max_dates['val__max'].year if min_max_dates['val__min'] != None else 1,
                'timefilterdata': JSONSerializer().serialize(Concept.get_time_filter_data()),
            })
        else:
            return HttpResponseNotFound('<h1>Arches form not found.</h1>')
def UnloadRelations(source):
    """
    Simple utility to unload relations
    
    AZ 17/1/17
    """
    with open(source, 'rb') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=',')
        se = SearchEngineFactory().create()
        for row in reader:
            entity = Resource()
            entity.entityid = row['RESOURCEID_FROM']
            related_oldindex = get_related_resources(row['RESOURCEID_FROM'])
            if related_oldindex:
                for releted_res in related_oldindex['resource_relationships']:
                    if str(releted_res['entityid2']) == str(
                            row['RESOURCEID_TO']):
                        se.delete(index='resource_relations',
                                  doc_type='all',
                                  id=releted_res['resourcexid'])
            try:
                relationship = RelatedResource.objects.get(
                    entityid1=entity.entityid,
                    entityid2=row['RESOURCEID_TO'],
                    relationshiptype=row['RELATION_TYPE'])
                entity.delete_resource_relationship(row['RESOURCEID_TO'],
                                                    row['RELATION_TYPE'])
            except:
                print "Issues deleting DB instance of relation with entity1 %s and entity2 %s . Most likely, the instance has already been deleted" % (
                    row['RESOURCEID_FROM'], row['RESOURCEID_TO'])
                pass
Exemplo n.º 10
0
def index_concepts():
    """
    Collects all concepts and indexes both concepts and concept_labels

    """

    se = SearchEngineFactory().create()
    se.delete_index(index='concept_labels')
    se.delete(
        index='term',
        body=
        '{"query":{"bool":{"must_not":[{"constant_score":{"filter":{"missing":{"field":"value.options.conceptid"}}}}],"must":[],"should":[]}}}'
    )

    Resource().prepare_term_index(create=True)

    print 'indexing concepts'
    start = datetime.now()

    cursor = connection.cursor()
    cursor.execute("""select conceptid from concepts.concepts""")
    conceptids = cursor.fetchall()
    for c in conceptids:
        if c[0] not in CORE_CONCEPTS:
            concept = Concept().get(id=c[0],
                                    include_subconcepts=True,
                                    include_parentconcepts=False,
                                    include=['label'])
            concept.index()

    end = datetime.now()
    duration = end - start
    print 'indexing concepts required', duration.seconds, 'seconds'

    cursor = connection.cursor()
    sql = """
        select conceptid, conceptlabel from concepts.vw_concepts where conceptid not in ('%s')
    """ % ("','".join(CORE_CONCEPTS))
    cursor.execute(sql)
    concepts = cursor.fetchall()
    concept_index_results = {'count': len(concepts), 'passed': 0, 'failed': 0}

    for conceptid, conceptvalue in concepts:
        result = get_indexed_concepts(se, conceptid, conceptvalue)
        if result != 'passed':
            concept_index_results['failed'] += 1
        else:
            concept_index_results['passed'] += 1

    status = 'Passed' if concept_index_results['failed'] == 0 else 'Failed'
    print '\nConcept Index Results:'
    print "Status: {0}, In Database: {1}, Indexed: {2}".format(
        status, concept_index_results['count'],
        concept_index_results['passed'])
Exemplo n.º 11
0
    def delete(self, user={}, note=''):
        """
        Deletes a single resource and any related indexed data

        """

        permit_deletion = False
        graph = models.GraphModel.objects.get(graphid=self.graph_id)
        if graph.isactive is False:
            message = _('This model is not yet active; unable to delete.')
            raise ModelInactiveError(message)
        if user != {}:
            user_is_reviewer = user.groups.filter(
                name='Resource Reviewer').exists()
            if user_is_reviewer is False:
                tiles = list(
                    models.TileModel.objects.filter(resourceinstance=self))
                resource_is_provisional = True if sum(
                    [len(t.data) for t in tiles]) == 0 else False
                if resource_is_provisional is True:
                    permit_deletion = True
            else:
                permit_deletion = True
        else:
            permit_deletion = True

        if permit_deletion is True:
            se = SearchEngineFactory().create()
            related_resources = self.get_related_resources(lang="en-US",
                                                           start=0,
                                                           limit=1000,
                                                           page=0)
            for rr in related_resources['resource_relationships']:
                models.ResourceXResource.objects.get(
                    pk=rr['resourcexid']).delete()
            query = Query(se)
            bool_query = Bool()
            bool_query.filter(
                Terms(field='resourceinstanceid',
                      terms=[self.resourceinstanceid]))
            query.add_query(bool_query)
            results = query.search(index='terms')['hits']['hits']
            for result in results:
                se.delete(index='terms', id=result['_id'])
            se.delete(index='resources', id=self.resourceinstanceid)

            self.save_edit(edit_type='delete',
                           user=user,
                           note=self.displayname)
            super(Resource, self).delete()

        return permit_deletion
Exemplo n.º 12
0
 def delete(self, request, resourceid=None):
     es = Elasticsearch()
     se = SearchEngineFactory().create()
     req = dict(request.GET)
     ids_to_delete = req['resourcexids[]']
     root_resourceinstanceid = req['root_resourceinstanceid']
     for resourcexid in ids_to_delete:
         try:
             ret = models.ResourceXResource.objects.get(pk=resourcexid).delete()
         except:
             print 'no such model'
         se.delete(index='resource_relations', doc_type='all', id=resourcexid)
     start = request.GET.get('start', 0)
     es.indices.refresh(index="resource_relations")
     return JSONResponse(self.get_related_resources(root_resourceinstanceid[0], lang="en-us", start=start, limit=15), indent=4)
Exemplo n.º 13
0
def related_resources(request, resourceid):
    if request.method == 'GET':
        lang = request.GET.get('lang', settings.LANGUAGE_CODE)
        start = request.GET.get('start', 0)
        return JSONResponse(get_related_resources(resourceid, lang, start=start, limit=15), indent=4)
    
    if 'edit' in request.user.user_groups and request.method == 'DELETE':
        se = SearchEngineFactory().create()
        data = JSONDeserializer().deserialize(request.body) 
        entityid1 = data.get('entityid1')
        entityid2 = data.get('entityid2')
        resourcexid = data.get('resourcexid')
        realtionshiptype = data.get('realtionshiptype')
        resource = Resource(entityid1)
        resource.delete_resource_relationship(entityid2, realtionshiptype)
        se.delete(index='resource_relations', doc_type='all', id=resourcexid)
        return JSONResponse({ 'success': True })
Exemplo n.º 14
0
def index_concepts():
    """
    Collects all concepts and indexes both concepts and concept_labels

    """

    se = SearchEngineFactory().create()
    se.delete_index(index='concept_labels')
    se.delete(index='term', body='{"query":{"bool":{"must_not":[{"constant_score":{"filter":{"missing":{"field":"value.options.conceptid"}}}}],"must":[],"should":[]}}}')

    Resource().prepare_term_index(create=True)

    print 'indexing concepts'
    start = datetime.now()

    cursor = connection.cursor()
    cursor.execute("""select conceptid from concepts.concepts""")
    conceptids = cursor.fetchall()
    for c in conceptids:
        if c[0] not in CORE_CONCEPTS:
            concept = Concept().get(id=c[0], include_subconcepts=True, include_parentconcepts=False, include=['label'])
            concept.index()

    end = datetime.now()
    duration = end - start
    print 'indexing concepts required', duration.seconds, 'seconds'

    cursor = connection.cursor()
    sql = """
        select conceptid, conceptlabel from concepts.vw_concepts where conceptid not in ('%s')
    """ % ("','".join(CORE_CONCEPTS))
    cursor.execute(sql)
    concepts = cursor.fetchall()
    concept_index_results = {'count':len(concepts), 'passed':0, 'failed':0}

    for conceptid, conceptvalue in concepts:
        result = get_indexed_concepts(se, conceptid, conceptvalue)
        if result != 'passed':
            concept_index_results['failed'] += 1
        else:
            concept_index_results['passed'] += 1

    status = 'Passed' if concept_index_results['failed'] == 0 else 'Failed'
    print '\nConcept Index Results:'
    print "Status: {0}, In Database: {1}, Indexed: {2}".format(status, concept_index_results['count'], concept_index_results['passed'])
Exemplo n.º 15
0
    def delete(self):
        """
        Deletes a single resource and any related indexed data

        """

        se = SearchEngineFactory().create()
        related_resources = self.get_related_resources(lang="en-US", start=0, limit=15)
        for rr in related_resources['resource_relationships']:
            models.ResourceXResource.objects.get(pk=rr['resourcexid']).delete()
        query = Query(se)
        bool_query = Bool()
        bool_query.filter(Terms(field='resourceinstanceid', terms=[self.resourceinstanceid]))
        query.add_query(bool_query)
        results = query.search(index='strings', doc_type='term')['hits']['hits']
        for result in results:
            se.delete(index='strings', doc_type='term', id=result['_id'])
        se.delete(index='resource', doc_type=str(self.graph_id), id=self.resourceinstanceid)
        super(Resource, self).delete()
Exemplo n.º 16
0
    def delete(self, *args, **kwargs):
        se = SearchEngineFactory().create()
        request = kwargs.pop('request', None)
        for tiles in self.tiles.itervalues():
            for tile in tiles:
                tile.delete(*args, request=request, **kwargs)

        query = Query(se)
        bool_query = Bool()
        bool_query.filter(Terms(field='tileid', terms=[self.tileid]))
        query.add_query(bool_query)
        results = query.search(index='strings', doc_type='term')['hits']['hits']
        for result in results:
            se.delete(index='strings', doc_type='term', id=result['_id'])

        self.__preDelete(request)
        super(Tile, self).delete(*args, **kwargs)
        resource = Resource.objects.get(resourceinstanceid=self.resourceinstance.resourceinstanceid)
        resource.index()
Exemplo n.º 17
0
    def delete(self, *args, **kwargs):
        se = SearchEngineFactory().create()
        request = kwargs.pop('request', None)
        for tiles in self.tiles.itervalues():
            for tile in tiles:
                tile.delete(*args, request=request, **kwargs)

        query = Query(se)
        bool_query = Bool()
        bool_query.filter(Terms(field='tileid', terms=[self.tileid]))
        query.add_query(bool_query)
        results = query.search(index='strings', doc_type='term')['hits']['hits']
        for result in results:
            se.delete(index='strings', doc_type='term', id=result['_id'])

        self.__preDelete(request)
        self.save_edit(user=request.user, edit_type='tile delete', old_value=self.data)
        super(Tile, self).delete(*args, **kwargs)
        resource = Resource.objects.get(resourceinstanceid=self.resourceinstance.resourceinstanceid)
        resource.index()
Exemplo n.º 18
0
    def update(self, data, files):
        se = SearchEngineFactory().create()
        related_resources_data = data.get('related-resources', [])
        original_relations = self.resource.get_related_resources()
        if self.resource.entityid == '':
            self.resource.save()
        relationship_ids = []

        for related_resource in related_resources_data:
            relationship_id = related_resource['relationship']['resourcexid']
            relationship_ids.append(relationship_id)
            resource_id = related_resource['relatedresourceid']
            relationship_type_id = related_resource['relationship']['relationshiptype']
            if isinstance(relationship_type_id, dict):
                relationship_type_id = relationship_type_id['value']
            notes = related_resource['relationship']['notes']
            date_started = related_resource['relationship']['datestarted']
            date_ended = related_resource['relationship']['dateended']
            if not relationship_id:
                relationship = self.resource.create_resource_relationship(resource_id,
                    relationship_type_id=relationship_type_id,
                    notes=notes,
                    date_started=date_started,
                    date_ended=date_ended
                    )
                    
            else:
                relationship = RelatedResource.objects.get(pk=relationship_id)
                relationship.relationshiptype = relationship_type_id
                relationship.notes = notes
                relationship.datestarted = date_started
                relationship.dateended = date_ended
                relationship.save()
                se.delete(index='resource_relations', doc_type='all', id=relationship_id)
            se.index_data(index='resource_relations', doc_type='all', body=model_to_dict(relationship), idfield='resourcexid')

        for relatedentity in original_relations:
            if relatedentity['relationship'].resourcexid not in relationship_ids:
                se.delete(index='resource_relations', doc_type='all', id=relatedentity['relationship'].resourcexid)
                relatedentity['relationship'].delete()
Exemplo n.º 19
0
def UnloadRelations(source):
    """
    Simple utility to unload relations
    
    AZ 17/1/17
    """
    with open(source, 'rb') as csvfile:
        reader = csv.DictReader(csvfile, delimiter= ',')
        se = SearchEngineFactory().create()
        for row in reader:
            entity = Resource()
            entity.entityid = row['RESOURCEID_FROM']          
            related_oldindex = get_related_resources(row['RESOURCEID_FROM'])
            if related_oldindex:
                for releted_res in related_oldindex['resource_relationships']:
                    if str(releted_res['entityid2']) == str(row['RESOURCEID_TO']):
                        se.delete(index='resource_relations', doc_type='all', id=releted_res['resourcexid'])
            try:            
                relationship = RelatedResource.objects.get(entityid1=entity.entityid, entityid2=row['RESOURCEID_TO'],relationshiptype=row['RELATION_TYPE'])
                entity.delete_resource_relationship(row['RESOURCEID_TO'], row['RELATION_TYPE'])
            except:
                print "Issues deleting DB instance of relation with entity1 %s and entity2 %s . Most likely, the instance has already been deleted" % (row['RESOURCEID_FROM'], row['RESOURCEID_TO'])
                pass
Exemplo n.º 20
0
    def delete_index(self):
        """
        removes an entity from the search index

        """

        se = SearchEngineFactory().create()
        se.delete(index='entity', doc_type=self.entitytypeid, id=self.entityid)
        se.delete(index='resource', doc_type=self.entitytypeid, id=self.entityid)        
        se.delete(index='maplayers', doc_type=self.entitytypeid, id=self.entityid)

        def delete_indexes(entity):
            if entity.businesstablename == 'strings' or entity.businesstablename == 'domains':
                se.delete_terms(entity.entityid)

        entity = Entity().get(self.entityid)
        entity.traverse(delete_indexes)
Exemplo n.º 21
0
    def delete_index(self):
        """
        removes an entity from the search index

        """

        se = SearchEngineFactory().create()
        se.delete(index='entity', doc_type=self.entitytypeid, id=self.entityid)
        se.delete(index='resource',
                  doc_type=self.entitytypeid,
                  id=self.entityid)
        se.delete(index='maplayers',
                  doc_type=self.entitytypeid,
                  id=self.entityid)

        def delete_indexes(entity):
            if entity.businesstablename == 'strings' or entity.businesstablename == 'domains':
                se.delete_terms(entity.entityid)

        entity = Entity().get(self.entityid)
        entity.traverse(delete_indexes)
Exemplo n.º 22
0
 def delete(self):
     se = SearchEngineFactory().create()
     se.delete(index='resource_relations', doc_type='all', id=self.resourcexid)
     super(ResourceXResource, self).delete()
Exemplo n.º 23
0
def resource_manager(request, resourcetypeid='', form_id='default', resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    if form_id == 'default':
        form_id = resource.form_groups[0]['forms'][0]['id']
    form = resource.get_form(form_id)

    # Pravice preverjamo zaenkrat le preko grup
    # Uporabnik mora imeti dodeljeno grupo z nazivom tipa resourca
    if (request.user.username != 'anonymous'):
        user = User.objects.get(username=request.user.username)
        user_groups = user.groups.values_list('name', flat=True)
    else:
        user_groups = []

    if (not 'EDIT_' + resourcetypeid in user_groups and not 'PUBLISH_' + resourcetypeid in user_groups and not request.user.is_staff and not request.user.is_superuser):
		raise UserNotAuthorized('User does have permission for this resource!')

    group_ownerships = 0
    group_ownership = ''
    for group in user_groups:
        if group.startswith('OWNERSHIP_'):
            group_ownership = group[10:]
            group_ownerships = group_ownerships + 1
    
    if (group_ownerships == 0 and (resourceid == '' or (resourceid != '' and not request.user.is_staff and not request.user.is_superuser))):
		raise UserNotAuthorized('User does have a ownership group! Please contact Early Watercraft administrator to resolve this issue.')
    
    if (group_ownerships > 1 and (resourceid == '' or (resourceid != '' and not request.user.is_staff and not request.user.is_superuser))):
		raise UserNotAuthorized('User have more than one ownership group! Please contact Early Watercraft administrator to resolve this issue.')

    if request.method == 'DELETE':
        resource.delete_index()
        se = SearchEngineFactory().create()
        realtionships = resource.get_related_resources(return_entities=False)
        for realtionship in realtionships:
            se.delete(index='resource_relations', doc_type='all', id=realtionship.resourcexid)
            realtionship.delete()
        resource.delete()
        return JSONResponse({ 'success': True })

    if request.method == 'POST':
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        current_status = resource.get_current_status()
        if (resourceid != ''):
            current_group = resource.get_current_group()
        else:
            current_group = group_ownership
        user_can_edit_document = get_user_can_edit_document(current_status, current_group, user, resourcetypeid, user_groups, group_ownership)
        if (not user_can_edit_document):
            return HttpResponseNotFound('<h1>User can not edit this document!</h1>')
        if 'action' in request.POST:
            action = request.POST.get('action')
            
            if action == 'ready-for-approval':
                current_status = 'Pending approval'
                resource.set_resource_status(current_status, user)
                empty_errors_cache()
                errors = []
                actions = get_possible_actions(current_status, False, user)
                if settings.EMAIL_ENABLED:
                    resource_url = request.build_absolute_uri(resolve_url('resource_manager', resourcetypeid=resourcetypeid, form_id='summary', resourceid=resourceid))
                    # Dobimo seznam vseh publisherjev v trenutni skupini uporabnika
                    if group_ownership <> '':
                        search_group = 'OWNERSHIP_' + group_ownership
                        current_group = Group.objects.get(name=search_group)
                        current_users = current_group.user_set.all()
                        search_group = 'PUBLISH_' + resourcetypeid
                        publisher_group = Group.objects.get(name=search_group)
                        publisher_users = publisher_group.user_set.all()
                        recipients = []
                        for user1 in current_users:
                            if user1 in publisher_users:
                                if user1.username <> user.username: 
                                    recipients.append(user1.first_name + ' ' + user1.last_name + '<' + user1.email + '>')
                        # Pripravmo seznam mailov
                        if len(recipients)>0:
                            resource_type_name= settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]['name']
                            status = 'Pending approval'
                            resource_name = resource.get_primary_name()
                            subject = resource_name + ' (' + resource_type_name + ') - ' + status
                            from_email = settings.EMAIL_FROM
                            text_content = 'User ' + user.first_name + ' ' + user.last_name + ' (' + user.username + ') has submitted a document ' + resource_name + ' (' + resource_type_name + ') for approval.'
                            html_content = 'User <strong>' + user.first_name + ' ' + user.last_name + ' (' + user.username + ')</strong> has submitted a document <a href="' + resource_url + '">' + resource_name + ' (' + resource_type_name + ')</a> for approval.<br>'
                            #print html_content
                            
                            msg = EmailMultiAlternatives(subject, text_content, from_email, recipients)
                            msg.attach_alternative(html_content, "text/html")            
                            msg.content_subtype = "html"  # Main content is now text/html
                            
                            # Posljemo mail
                            connection = mail.get_connection()
                            
                            # Manually open the connection
                            connection.open()

                            # Construct an email message that uses the connection
                            msg.send()
                            
                            connection.close()   
            else:
                if action == 'reject-approval':
                    current_status = 'Approval rejected'
                    resource.set_resource_status(current_status, user)
                    empty_errors_cache()
                    errors = []
                    actions = get_possible_actions(current_status, False, user)
                    if settings.EMAIL_ENABLED:
                        # Dobimo razlog zavrnitve
                        rejectedDescription = request.POST.get('description')
                        resource_url = request.build_absolute_uri(resolve_url('resource_manager', resourcetypeid=resourcetypeid, form_id='summary', resourceid=resourceid))
                        
                        # Dobimo uporabnika, ki je dokument dal v pregled
                        ret = []
                        current = None
                        index = -1
                        start = 0
                        limit = 3
                        recipients = []
                        if resourceid != '':
                            dates = models.EditLog.objects.filter(resourceid = resourceid).values_list('timestamp', flat=True).order_by('-timestamp').distinct('timestamp')[start:limit]
                            for log in models.EditLog.objects.filter(resourceid = resourceid, timestamp__in = dates).values().order_by('-timestamp', 'attributeentitytypeid'):
                                if log['attributeentitytypeid'] == 'EW_STATUS.E55' and log['oldvalue'] == 'Draft' and log['newvalue'] == 'Pending approval':
                                    if int(log['userid'])<>user.id:
                                        print 'Sending mail...'
                                        print log['userid']<>user.id
                                        print log['userid']
                                        print user.id
                                        recipients.append(log['user_firstname'] + ' ' + log['user_lastname'] + '<' + log['user_email'] + '>')
                            if len(recipients)>0:
                                resource_type_name= settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]['name']
                                status = 'Approval rejected'
                                resource_name = resource.get_primary_name()
                                subject = resource_name + ' (' + resource_type_name + ') - ' + status
                                from_email = settings.EMAIL_FROM
                                text_content = 'User ' + user.first_name + ' ' + user.last_name + ' (' + user.username + ') has rejected a document ' + resource_name + ' (' + resource_type_name + '). For explanation go open document in Early Watercraft (section Validate Watercraft)'
                                html_content = 'User <strong>' + user.first_name + ' ' + user.last_name + ' (' + user.username + ')</strong> has rejected a document <a href="' + resource_url + '">' + resource_name + ' (' + resource_type_name + ')</a> with following explanation:<br>' + rejectedDescription
                                print html_content
                                msg = EmailMultiAlternatives(subject, text_content, from_email, recipients)
                                msg.attach_alternative(html_content, "text/html")            
                                msg.content_subtype = "html"  # Main content is now text/html
                                
                                # Posljemo mail
                                connection = mail.get_connection()
                                
                                # Manually open the connection
                                connection.open()

                                # Construct an email message that uses the connection
                                msg.send()
                                
                                connection.close()  
                else:
                    if action == 'return-to-draft':
                        current_status = 'Draft'
                        resource.set_resource_status(current_status, user)
                        empty_errors_cache()
                        errors = []
                        actions = get_possible_actions(current_status, False, user)
                    else:
                        if action == 'publish':
                            current_status = 'Published'
                            resource.set_resource_status(current_status, user)
                            empty_errors_cache()
                            errors = []
                            actions = get_possible_actions(current_status, False, user)
        form.update(data, request.FILES)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid
            print "Redirect_resource_manager"
            return redirect('resource_manager', resourcetypeid=resourcetypeid, form_id=form_id, resourceid=resourceid)

    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))
    
    if request.method == 'GET':
        if form != None:
            lang = request.GET.get('lang', settings.LANGUAGE_CODE)
            current_status = resource.get_current_status()
            if (resourceid != ''):
                current_group = resource.get_current_group()
            else:
                current_group = group_ownership
            print "Current status: "
            print current_status
            print "Current group: "
            print current_group
            actions = []
            user_can_edit_document = get_user_can_edit_document(current_status, current_group, user, resourcetypeid, user_groups, group_ownership)
            form.load(lang, current_group)
                     
            # If user can not edit resource, there will be no validate and delete resource options
            if (not user_can_edit_document):
                for form_group in resource.form_groups:
                    for form1 in form_group["forms"]:
                        if (form1["id"] == 'delete-resource' or form1["id"] == 'validate-resource'):
                            form_group["forms"].remove(form1)
            # If status is not Draft and user is not superuser, delete is disabled
            if (current_status<> 'Draft' and not user.is_superuser):
                for form_group in resource.form_groups:
                    for form1 in form_group["forms"]:
                        if (form1["id"] == 'delete-resource'):
                            form_group["forms"].remove(form1)
            if form_id == 'validate-resource':
                errors = resource.validate_resource()
                cache.set('errors', errors, 1000)
                cache.set('resourceid', resourceid, 1000)
                errorsExists = False
                for error in errors:
                    print error
                    if error['type'] == 'error':
                        errorsExists = True
                        break
                actions = get_possible_actions(current_status, errorsExists, user)
            else:
                saved_resourceid = cache.get('resourceid')
                if (resourceid == saved_resourceid):
                    errors = cache.get('errors')
                else:
                    empty_errors_cache()
                    errors = []
                    
            return render_to_response('resource-manager.htm', {
                    'form': form,
                    'formdata': JSONSerializer().serialize(form.data),
                    'form_template': 'views/forms/' + form_id + '.htm',
                    'form_id': form_id,
                    'resourcetypeid': resourcetypeid,
                    'resourceid': resourceid,
                    'main_script': 'resource-manager',
                    'active_page': 'ResourceManger',
                    'resource': resource,
                    'resource_name': resource.get_primary_name(),
                    'resource_type_name': resource.get_type_name(),
                    'resource_icon': settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]["icon_class"],
                    'form_groups': resource.form_groups,
                    'min_date': min_max_dates['val__min'].year if min_max_dates['val__min'] != None else 0,
                    'max_date': min_max_dates['val__max'].year if min_max_dates['val__min'] != None else 1,
                    'timefilterdata': JSONSerializer().serialize(Concept.get_time_filter_data()),
                    'current_status': current_status,
                    'user_groups': user_groups,
					'errors': errors,
					'actions': actions,
					'user_can_edit_document': user_can_edit_document,
					'region_coordinates': JSONSerializer().serialize(settings.REGION_COORDINATES),
                    'help' :  settings.HELP['resource_manager']
                },
                context_instance=RequestContext(request))
        else:
            return HttpResponseNotFound('<h1>Arches form not found.</h1>')
Exemplo n.º 24
0
 def delete(self):
     se = SearchEngineFactory().create()
     se.delete(index='resource_relations', doc_type='all', id=self.resourcexid)
     super(ResourceXResource, self).delete()
Exemplo n.º 25
0
 def delete(self):
     from arches.app.search.search_engine_factory import SearchEngineFactory
     se = SearchEngineFactory().create()
     se.delete(index='resource_relations', doc_type='all', id=self.resourcexid)
     super(ResourceXResource, self).delete()
Exemplo n.º 26
0
    def delete(self, user={}, note=""):
        """
        Deletes a single resource and any related indexed data

        """

        permit_deletion = False
        graph = models.GraphModel.objects.get(graphid=self.graph_id)
        if graph.isactive is False:
            message = _("This model is not yet active; unable to delete.")
            raise ModelInactiveError(message)
        if user != {}:
            user_is_reviewer = user_is_resource_reviewer(user)
            if user_is_reviewer is False:
                tiles = list(
                    models.TileModel.objects.filter(resourceinstance=self))
                resource_is_provisional = True if sum(
                    [len(t.data) for t in tiles]) == 0 else False
                if resource_is_provisional is True:
                    permit_deletion = True
            else:
                permit_deletion = True
        else:
            permit_deletion = True

        if permit_deletion is True:
            se = SearchEngineFactory().create()
            related_resources = self.get_related_resources(lang="en-US",
                                                           start=0,
                                                           limit=1000,
                                                           page=0)
            for rr in related_resources["resource_relationships"]:
                # delete any related resource entries, also reindex the resrouce that references this resrouce that's being deleted
                resourceXresource = models.ResourceXResource.objects.get(
                    pk=rr["resourcexid"])
                resource_to_reindex = (
                    resourceXresource.resourceinstanceidfrom_id
                    if resourceXresource.resourceinstanceidto_id
                    == self.resourceinstanceid else
                    resourceXresource.resourceinstanceidto_id)
                resourceXresource.delete(
                    deletedResourceId=self.resourceinstanceid)
                res = Resource.objects.get(pk=resource_to_reindex)
                res.load_tiles()
                res.index()

            query = Query(se)
            bool_query = Bool()
            bool_query.filter(
                Terms(field="resourceinstanceid",
                      terms=[self.resourceinstanceid]))
            query.add_query(bool_query)
            results = query.search(index="terms")["hits"]["hits"]
            for result in results:
                se.delete(index="terms", id=result["_id"])
            se.delete(index="resources", id=self.resourceinstanceid)

            self.save_edit(edit_type="delete",
                           user=user,
                           note=self.displayname)
            super(Resource, self).delete()

        return permit_deletion
Exemplo n.º 27
0
def resource_manager(request, resourcetypeid='', form_id='default', resourceid=''):

    ## get and check all permissions here
    permissions = request.user.get_all_permissions()
    res_perms = {k:[] for k in settings.RESOURCE_TYPE_CONFIGS().keys()}

    for k,v in res_perms.iteritems():
        for p in permissions:
            t,res = p.split(".")[:2]
            if k.startswith(res):
                v.append(t)

    if resourceid == '' and not 'CREATE' in res_perms[resourcetypeid]:
        return redirect(settings.LOGIN_URL)
    if not 'EDIT' in res_perms[resourcetypeid]:
        return redirect(settings.LOGIN_URL)
    ## finish permission testing

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    if form_id == 'default':
        form_id = resource.form_groups[0]['forms'][0]['id']

    form = resource.get_form(form_id)

    if request.method == 'DELETE':
        resource.delete_index()
        se = SearchEngineFactory().create()
        relationships = resource.get_related_resources(return_entities=False)
        for relationship in relationships:
            se.delete(index='resource_relations', doc_type='all', id=relationship.resourcexid)
            relationship.delete()
        resource.delete()
        return JSONResponse({ 'success': True })

    if request.method == 'POST':
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        form.update(data, request.FILES)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid

            return redirect('resource_manager', resourcetypeid=resourcetypeid, form_id=form_id, resourceid=resourceid)

    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))
    
    if request.method == 'GET':
        if form != None:
            lang = request.GET.get('lang', settings.LANGUAGE_CODE)
            form.load(lang)
            return render_to_response('resource-manager.htm', {
                    'form': form,
                    'formdata': JSONSerializer().serialize(form.data),
                    'form_template': 'views/forms/' + form_id + '.htm',
                    'form_id': form_id,
                    'resourcetypeid': resourcetypeid,
                    'resourceid': resourceid,
                    'main_script': 'resource-manager',
                    'active_page': 'ResourceManger',
                    'resource': resource,
                    'resource_name': resource.get_primary_name(),
                    'resource_type_name': resource.get_type_name(),
                    'form_groups': resource.form_groups,
                    'min_date': min_max_dates['val__min'].year if min_max_dates['val__min'] != None else 0,
                    'max_date': min_max_dates['val__max'].year if min_max_dates['val__min'] != None else 1,
                    'timefilterdata': JSONSerializer().serialize(Concept.get_time_filter_data()),
                },
                context_instance=RequestContext(request))
        else:
            return HttpResponseNotFound('<h1>Arches form not found.</h1>')
Exemplo n.º 28
0
def resource_manager(request,
                     resourcetypeid='',
                     form_id='default',
                     resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    if form_id == 'default':
        form_id = resource.form_groups[0]['forms'][0]['id']
    form = resource.get_form(form_id)

    # Pravice preverjamo zaenkrat le preko grup
    # Uporabnik mora imeti dodeljeno grupo z nazivom tipa resourca
    if (request.user.username != 'anonymous'):
        user = User.objects.get(username=request.user.username)
        user_groups = user.groups.values_list('name', flat=True)
    else:
        user_groups = []

    if (not 'EDIT_' + resourcetypeid in user_groups
            and not 'PUBLISH_' + resourcetypeid in user_groups
            and not request.user.is_staff and not request.user.is_superuser):
        raise UserNotAuthorized('User does have permission for this resource!')

    group_ownerships = 0
    group_ownership = ''
    for group in user_groups:
        if group.startswith('OWNERSHIP_'):
            group_ownership = group[10:]
            group_ownerships = group_ownerships + 1

    if (group_ownerships == 0 and
        (resourceid == '' or (resourceid != '' and not request.user.is_staff
                              and not request.user.is_superuser))):
        raise UserNotAuthorized(
            'User does have a ownership group! Please contact Early Watercraft administrator to resolve this issue.'
        )

    if (group_ownerships > 1 and
        (resourceid == '' or (resourceid != '' and not request.user.is_staff
                              and not request.user.is_superuser))):
        raise UserNotAuthorized(
            'User have more than one ownership group! Please contact Early Watercraft administrator to resolve this issue.'
        )

    if request.method == 'DELETE':
        resource.delete_index()
        se = SearchEngineFactory().create()
        realtionships = resource.get_related_resources(return_entities=False)
        for realtionship in realtionships:
            se.delete(index='resource_relations',
                      doc_type='all',
                      id=realtionship.resourcexid)
            realtionship.delete()
        resource.delete()
        return JSONResponse({'success': True})

    if request.method == 'POST':
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        current_status = resource.get_current_status()
        if (resourceid != ''):
            current_group = resource.get_current_group()
        else:
            current_group = group_ownership
        user_can_edit_document = get_user_can_edit_document(
            current_status, current_group, user, resourcetypeid, user_groups,
            group_ownership)
        if (not user_can_edit_document):
            return HttpResponseNotFound(
                '<h1>User can not edit this document!</h1>')
        if 'action' in request.POST:
            action = request.POST.get('action')

            if action == 'ready-for-approval':
                current_status = 'Pending approval'
                resource.set_resource_status(current_status, user)
                empty_errors_cache()
                errors = []
                actions = get_possible_actions(current_status, False, user)
                if settings.EMAIL_ENABLED:
                    resource_url = request.build_absolute_uri(
                        resolve_url('resource_manager',
                                    resourcetypeid=resourcetypeid,
                                    form_id='summary',
                                    resourceid=resourceid))
                    # Dobimo seznam vseh publisherjev v trenutni skupini uporabnika
                    if group_ownership <> '':
                        search_group = 'OWNERSHIP_' + group_ownership
                        current_group = Group.objects.get(name=search_group)
                        current_users = current_group.user_set.all()
                        search_group = 'PUBLISH_' + resourcetypeid
                        publisher_group = Group.objects.get(name=search_group)
                        publisher_users = publisher_group.user_set.all()
                        recipients = []
                        for user1 in current_users:
                            if user1 in publisher_users:
                                if user1.username <> user.username:
                                    recipients.append(user1.first_name + ' ' +
                                                      user1.last_name + '<' +
                                                      user1.email + '>')
                        # Pripravmo seznam mailov
                        if len(recipients) > 0:
                            resource_type_name = settings.RESOURCE_TYPE_CONFIGS(
                            )[resourcetypeid]['name']
                            status = 'Pending approval'
                            resource_name = resource.get_primary_name()
                            subject = resource_name + ' (' + resource_type_name + ') - ' + status
                            from_email = settings.EMAIL_FROM
                            text_content = 'User ' + user.first_name + ' ' + user.last_name + ' (' + user.username + ') has submitted a document ' + resource_name + ' (' + resource_type_name + ') for approval.'
                            html_content = 'User <strong>' + user.first_name + ' ' + user.last_name + ' (' + user.username + ')</strong> has submitted a document <a href="' + resource_url + '">' + resource_name + ' (' + resource_type_name + ')</a> for approval.<br>'
                            #print html_content

                            msg = EmailMultiAlternatives(
                                subject, text_content, from_email, recipients)
                            msg.attach_alternative(html_content, "text/html")
                            msg.content_subtype = "html"  # Main content is now text/html

                            # Posljemo mail
                            connection = mail.get_connection()

                            # Manually open the connection
                            connection.open()

                            # Construct an email message that uses the connection
                            msg.send()

                            connection.close()
            else:
                if action == 'reject-approval':
                    current_status = 'Approval rejected'
                    resource.set_resource_status(current_status, user)
                    empty_errors_cache()
                    errors = []
                    actions = get_possible_actions(current_status, False, user)
                    if settings.EMAIL_ENABLED:
                        # Dobimo razlog zavrnitve
                        rejectedDescription = request.POST.get('description')
                        resource_url = request.build_absolute_uri(
                            resolve_url('resource_manager',
                                        resourcetypeid=resourcetypeid,
                                        form_id='summary',
                                        resourceid=resourceid))

                        # Dobimo uporabnika, ki je dokument dal v pregled
                        ret = []
                        current = None
                        index = -1
                        start = 0
                        limit = 3
                        recipients = []
                        if resourceid != '':
                            dates = models.EditLog.objects.filter(
                                resourceid=resourceid).values_list(
                                    'timestamp',
                                    flat=True).order_by('-timestamp').distinct(
                                        'timestamp')[start:limit]
                            for log in models.EditLog.objects.filter(
                                    resourceid=resourceid,
                                    timestamp__in=dates).values().order_by(
                                        '-timestamp', 'attributeentitytypeid'):
                                if log['attributeentitytypeid'] == 'EW_STATUS.E55' and log[
                                        'oldvalue'] == 'Draft' and log[
                                            'newvalue'] == 'Pending approval':
                                    if int(log['userid']) <> user.id:
                                        print 'Sending mail...'
                                        print log['userid'] <> user.id
                                        print log['userid']
                                        print user.id
                                        recipients.append(
                                            log['user_firstname'] + ' ' +
                                            log['user_lastname'] + '<' +
                                            log['user_email'] + '>')
                            if len(recipients) > 0:
                                resource_type_name = settings.RESOURCE_TYPE_CONFIGS(
                                )[resourcetypeid]['name']
                                status = 'Approval rejected'
                                resource_name = resource.get_primary_name()
                                subject = resource_name + ' (' + resource_type_name + ') - ' + status
                                from_email = settings.EMAIL_FROM
                                text_content = 'User ' + user.first_name + ' ' + user.last_name + ' (' + user.username + ') has rejected a document ' + resource_name + ' (' + resource_type_name + '). For explanation go open document in Early Watercraft (section Validate Watercraft)'
                                html_content = 'User <strong>' + user.first_name + ' ' + user.last_name + ' (' + user.username + ')</strong> has rejected a document <a href="' + resource_url + '">' + resource_name + ' (' + resource_type_name + ')</a> with following explanation:<br>' + rejectedDescription
                                print html_content
                                msg = EmailMultiAlternatives(
                                    subject, text_content, from_email,
                                    recipients)
                                msg.attach_alternative(html_content,
                                                       "text/html")
                                msg.content_subtype = "html"  # Main content is now text/html

                                # Posljemo mail
                                connection = mail.get_connection()

                                # Manually open the connection
                                connection.open()

                                # Construct an email message that uses the connection
                                msg.send()

                                connection.close()
                else:
                    if action == 'return-to-draft':
                        current_status = 'Draft'
                        resource.set_resource_status(current_status, user)
                        empty_errors_cache()
                        errors = []
                        actions = get_possible_actions(current_status, False,
                                                       user)
                    else:
                        if action == 'publish':
                            current_status = 'Published'
                            resource.set_resource_status(current_status, user)
                            empty_errors_cache()
                            errors = []
                            actions = get_possible_actions(
                                current_status, False, user)
        form.update(data, request.FILES)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid
            print "Redirect_resource_manager"
            return redirect('resource_manager',
                            resourcetypeid=resourcetypeid,
                            form_id=form_id,
                            resourceid=resourceid)

    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))

    if request.method == 'GET':
        if form != None:
            lang = request.GET.get('lang', settings.LANGUAGE_CODE)
            current_status = resource.get_current_status()
            if (resourceid != ''):
                current_group = resource.get_current_group()
            else:
                current_group = group_ownership
            print "Current status: "
            print current_status
            print "Current group: "
            print current_group
            actions = []
            user_can_edit_document = get_user_can_edit_document(
                current_status, current_group, user, resourcetypeid,
                user_groups, group_ownership)
            form.load(lang, current_group)

            # If user can not edit resource, there will be no validate and delete resource options
            if (not user_can_edit_document):
                for form_group in resource.form_groups:
                    for form1 in form_group["forms"]:
                        if (form1["id"] == 'delete-resource'
                                or form1["id"] == 'validate-resource'):
                            form_group["forms"].remove(form1)
            # If status is not Draft and user is not superuser, delete is disabled
            if (current_status <> 'Draft' and not user.is_superuser):
                for form_group in resource.form_groups:
                    for form1 in form_group["forms"]:
                        if (form1["id"] == 'delete-resource'):
                            form_group["forms"].remove(form1)
            if form_id == 'validate-resource':
                errors = resource.validate_resource()
                cache.set('errors', errors, 1000)
                cache.set('resourceid', resourceid, 1000)
                errorsExists = False
                for error in errors:
                    print error
                    if error['type'] == 'error':
                        errorsExists = True
                        break
                actions = get_possible_actions(current_status, errorsExists,
                                               user)
            else:
                saved_resourceid = cache.get('resourceid')
                if (resourceid == saved_resourceid):
                    errors = cache.get('errors')
                else:
                    empty_errors_cache()
                    errors = []

            return render_to_response('resource-manager.htm', {
                'form':
                form,
                'formdata':
                JSONSerializer().serialize(form.data),
                'form_template':
                'views/forms/' + form_id + '.htm',
                'form_id':
                form_id,
                'resourcetypeid':
                resourcetypeid,
                'resourceid':
                resourceid,
                'main_script':
                'resource-manager',
                'active_page':
                'ResourceManger',
                'resource':
                resource,
                'resource_name':
                resource.get_primary_name(),
                'resource_type_name':
                resource.get_type_name(),
                'resource_icon':
                settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]["icon_class"],
                'form_groups':
                resource.form_groups,
                'min_date':
                min_max_dates['val__min'].year
                if min_max_dates['val__min'] != None else 0,
                'max_date':
                min_max_dates['val__max'].year
                if min_max_dates['val__min'] != None else 1,
                'timefilterdata':
                JSONSerializer().serialize(Concept.get_time_filter_data()),
                'current_status':
                current_status,
                'user_groups':
                user_groups,
                'errors':
                errors,
                'actions':
                actions,
                'user_can_edit_document':
                user_can_edit_document,
                'region_coordinates':
                JSONSerializer().serialize(settings.REGION_COORDINATES),
                'help':
                settings.HELP['resource_manager']
            },
                                      context_instance=RequestContext(request))
        else:
            return HttpResponseNotFound('<h1>Arches form not found.</h1>')
Exemplo n.º 29
0
def resource_manager(request,
                     resourcetypeid='',
                     form_id='default',
                     resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    if form_id == 'default':
        form_id = resource.form_groups[0]['forms'][0]['id']

    form = resource.get_form(form_id)

    if request.method == 'DELETE':
        resource.delete_index()
        se = SearchEngineFactory().create()
        realtionships = resource.get_related_resources(return_entities=False)
        for realtionship in realtionships:
            se.delete(index='resource_relations',
                      doc_type='all',
                      id=realtionship.resourcexid)
            realtionship.delete()
        resource.delete()
        return JSONResponse({'success': True})

    if request.method == 'POST':
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        form.set_user(request.user)
        form.update(data, request.FILES)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid

            if request.is_ajax():
                return JSONResponse({
                    "url":
                    reverse('resource_manager',
                            kwargs={
                                'resourcetypeid': resourcetypeid,
                                'form_id': form_id,
                                'resourceid': resourceid
                            })
                })

            return redirect('resource_manager',
                            resourcetypeid=resourcetypeid,
                            form_id=form_id,
                            resourceid=resourceid)

    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))

    if request.method == 'GET':
        if form != None:

            ## geom will be a geojson FeatureCollection or 'null'
            geom = JSONSerializer().serialize(resource.get_geom())

            lang = request.GET.get('lang', request.LANGUAGE_CODE)
            form.load(lang)
            return render_to_response('resource-manager.htm', {
                'form':
                form,
                'formdata':
                JSONSerializer().serialize(form.data),
                'form_template':
                'views/forms/' + form_id + '.htm',
                'form_id':
                form_id,
                'resourcetypeid':
                resourcetypeid,
                'resourceid':
                resourceid,
                'main_script':
                'resource-manager',
                'active_page':
                'ResourceManger',
                'resource':
                resource,
                'resource_name':
                resource.get_primary_name(),
                'resource_type_name':
                resource.get_type_name(),
                'form_groups':
                resource.form_groups,
                'min_date':
                min_max_dates['val__min'].year
                if min_max_dates['val__min'] != None else 0,
                'max_date':
                min_max_dates['val__max'].year
                if min_max_dates['val__min'] != None else 1,
                'timefilterdata':
                JSONSerializer().serialize(Concept.get_time_filter_data()),
                'resource_icon':
                settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]['icon_class'],
                'resource_geom':
                geom,
                'child_resource':
                'HERITAGE_FEATURE.E24' if resourcetypeid
                == 'HERITAGE_RESOURCE_GROUP.E27' else 'HERITAGE_COMPONENT.B2'
            },
                                      context_instance=RequestContext(request))
        else:
            return HttpResponseNotFound('<h1>Arches form not found.</h1>')