예제 #1
0
 def __init__(self):
     self.resource_type_configs = settings.RESOURCE_TYPE_CONFIGS()
     self.default_mapping = {
         "NAME":
         "ArchesResourceExport",
         "SCHEMA": [
             {
                 "field_name": "prime_name",
                 "source": "primaryname",
                 "data_type": "str",
                 "data_length": "128"
             },
             {
                 "field_name": "arches_id",
                 "source": "entityid",
                 "data_type": "str",
                 "data_length": "128"
             },
             {
                 "field_name": "type",
                 "source": "resource_name",
                 "data_type": "str",
                 "data_length": "128"
             },
         ],
         "RESOURCE_TYPES": {},
         "RECORDS": []
     }
예제 #2
0
def get_page(request):
    resource_id = request.GET.get('resourceid', '')
    # 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)
        if (user.is_staff or user.is_superuser):
           # Grupe vzamemo kar iz nastavitev
           user_groups = []
           all_groups = settings.RESOURCE_TYPE_CONFIGS()
           for group in all_groups:
               user_groups.append('EDIT_' + group)
               user_groups.append('PUBLISH_' + group)
        else:
           user_groups = user.groups.values_list('name', flat=True)
    else:
		user_groups = []
    json_user_groups = JSONSerializer().serialize(user_groups)
    return render_to_response('map.htm', {
            'main_script': 'map',
            'active_page': 'Map',
            'resource_id': resource_id,
            'user_groups': json_user_groups,
            'help' : settings.HELP['map']
        },
        context_instance=RequestContext(request))
예제 #3
0
def home_page(request):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    min_max_dates = models.Dates.objects.aggregate(Min('val'), Max('val'))

    if (request.user.username != 'anonymous'):
        user = User.objects.get(username=request.user.username)
        if (user.is_staff or user.is_superuser):
           # Grupe vzamemo kar iz nastavitev
           user_groups = []
           all_groups = settings.RESOURCE_TYPE_CONFIGS()
           for group in all_groups:
               print group
               user_groups.append(group)
        else:
           user_groups = user.groups.values_list('name', flat=True)
    else:
        user_groups = []
    
    search_context =  get_search_contexts(request)
    
    return render_to_response('search.htm', {
            'main_script': 'search',
            'active_page': 'Search',
            '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()),
            'user_groups': user_groups,
            'search_context': search_context,
            'help' :  settings.HELP['search']
        }, 
        context_instance=RequestContext(request))
예제 #4
0
    def test_form_classes(self):

        print "\n\n==== TESTING ALL FORM CLASSES ===="

        # These are considered exceptions because they are often included
        # in a resource type but actually enter data for a different resource
        # type. So they will throw errors below, even if they are valid.
        form_exceptions = [
            'man-made', 'man-made-component', 'related-files',
            'related-resources', 'file-upload'
        ]

        for restype in sorted(settings.RESOURCE_TYPE_CONFIGS()):
            print "\n\n--- {} FORMS ---".format(restype)

            q = Entity().get_mapping_schema(restype)
            restypenodes = set(q.keys())

            res = Resource({"entitytypeid": restype})

            for group in res.form_groups:
                for form in group['forms']:
                    invalid_nodes = []
                    fclass = form['class']
                    formid = fclass.get_info()['id']
                    if formid in form_exceptions:
                        continue
                    print "\nFORM:", formid

                    template_errors = self.test_template(formid, restypenodes)

                    print "{} invalid node{} in the template".format(
                        len(template_errors),
                        "s" if len(template_errors) != 1 else "")
                    if len(template_errors) > 0:
                        print "  ", template_errors

                    a = res.get_form(formid)
                    try:
                        a.load("en-US")
                    except Exception as e:
                        print "ERROR LOADING THIS FORMS.PY CLASS"
                        print traceback.print_exc()
                        continue

                    for key in a.data:
                        if not key in restypenodes and "." in key:
                            invalid_nodes.append(key)
                        domains = a.data[key].get('domains', [])
                        for domainnode in domains:
                            if not domainnode in restypenodes:
                                invalid_nodes.append(domainnode)

                    print "{} invalid node{} in the forms.py class".format(
                        len(invalid_nodes),
                        "s" if len(invalid_nodes) != 1 else "")
                    if len(invalid_nodes) > 0:
                        print "  ", invalid_nodes
예제 #5
0
    def create_uniqueids(
        self,
        entitytype,
        is_new_resource=False
    ):  # Method that creates UniqueIDs and its correlated Entity when a new resource is saved, or else only a UniqueId when the entity is already present (this will happen if the entities are created via importer.py

        uniqueid_node = settings.RESOURCE_TYPE_CONFIGS()[
            self.get_root().entitytypeid]['primary_name_lookup']['entity_type']
        if entitytype in settings.EAMENA_RESOURCES:
            type = settings.EAMENA_RESOURCES[entitytype]
        else:
            type = re.split("\W+|_", entitytype)[0]

        if is_new_resource:
            entity2 = archesmodels.Entities()
            entity2.entitytypeid = archesmodels.EntityTypes.objects.get(
                pk=uniqueid_node)
            entity2.entityid = str(uuid.uuid4())
            entity2.save()
            rule = archesmodels.Rules.objects.get(
                entitytypedomain=self.entitytypeid,
                entitytyperange=entity2.entitytypeid)
            archesmodels.Relations.objects.create(
                entityiddomain=archesmodels.Entities.objects.get(
                    pk=self.entityid),
                entityidrange=entity2,
                ruleid=rule)

        uniqueidmodel = self._get_model('uniqueids')
        uniqueidmodelinstance = uniqueidmodel()
        uniqueidmodelinstance.entityid = entity2 if is_new_resource else archesmodels.Entities.objects.get(
            pk=self.entityid)
        uniqueidmodelinstance.id_type = type

        if self.value:
            # Setting ID to value already defined
            num = int(self.value[-settings.ID_LENGTH:])
            uniqueidmodelinstance.val = str(num)
        else:
            try:
                last_uniqueid = archesmodels.UniqueIds.objects.filter(
                    id_type=type).extra({
                        'valint': "CAST(val as INT)"
                    }).latest('valint')
                IdInt = int(last_uniqueid.val) + 1
                uniqueidmodelinstance.val = str(IdInt)

            except ObjectDoesNotExist:
                uniqueidmodelinstance.val = str(1)

        uniqueidmodelinstance.order_date = datetime.datetime.now()
        uniqueidmodelinstance.save()
        if is_new_resource:
            return entity2.entityid
예제 #6
0
    def get_descriptive_name(self):
        """
        Gets the human readable name to display for entity instances
        """

        name_nodes = self.find_entities_by_type_id(settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]['description_node'])
        if len(name_nodes) > 0:
            name = name_nodes[0].value
        else:
            name = "(unnamed)"
        
        return name
예제 #7
0
 def __init__(self):
     self.resource_type_configs = settings.RESOURCE_TYPE_CONFIGS()
     self.default_mapping = {
         "NAME": "ArchesResourceExport",
         "SCHEMA": [
             {"field_name":"primary_name", "source":"primaryname"},
             {"field_name":"arches_id", "source":"entityid"},
             {"field_name":"arches_resource_type", "source":"resource_name"},
             {"field_name":"geometry", "source":"geometries"},
         ],
         "RESOURCE_TYPES" : {},
         "RECORDS":[]
     }  
예제 #8
0
    def get_names(self):
        """
        Gets the human readable name to display for entity instances

        """

        names = []
        name_nodes = self.find_entities_by_type_id(settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]['primary_name_lookup']['entity_type'])
        if len(name_nodes) > 0:
            for name in name_nodes:
                names.append(name)

        return names
예제 #9
0
def user_permissions(request):
    '''defines all user permissions'''

    # get all group names for user
    group_names = [i.name for i in request.user.groups.all()]
    resource_types = [
        v['name'] for v in settings.RESOURCE_TYPE_CONFIGS().values()
    ]

    # these are the entities that a user is allowed to edit
    user_can_edit = False
    entities_allowed = [i for i in group_names if i in resource_types]
    if len(entities_allowed) > 0:
        user_can_edit = True

    # check whether user can create new resources
    can_create = False
    if "DATA CREATORS" in group_names:
        can_create = True

    # if user is part of the data creators group, they can create new resources
    rdm_access = False
    if "RDM ACCESS" in group_names:
        rdm_access = True

    # give superuser all access
    if request.user.is_superuser:
        rdm_access = True
        user_can_edit = True
        can_create = True
        entities_allowed = resource_types

    return {
        'user_can_edit': user_can_edit,
        'user_permissions': {
            'can_rdm': rdm_access,
            'can_create': can_create,
            'entities_allowed': entities_allowed
        }
    }
예제 #10
0
 def __init__(self):
     self.resource_type_configs = settings.RESOURCE_TYPE_CONFIGS()
     self.default_mapping = {
         "NAME":
         "ArchesResourceExport",
         "SCHEMA": [
             {
                 "field_name": "PRIMARY NAME",
                 "source": "primaryname"
             },
             {
                 "field_name": "ARCHES ID",
                 "source": "entityid"
             },
             {
                 "field_name": "ARCHES RESOURCE TYPE",
                 "source": "resource_name"
             },
         ],
         "RESOURCE_TYPES": {},
         "RECORDS": []
     }
예제 #11
0
파일: setup.py 프로젝트: legiongis/crip
def build_groups():
    '''builds all default AFRH-specific permissions, groups, and users'''

    print "\nREMOVING ARCHES-HIP PERMISSIONS & GROUPS\n-----------------------"
    all_perms = Permission.objects.filter()
    for p in all_perms:
        p.delete()
    print "  {} permissions removed".format(len(all_perms))

    all_groups = Group.objects.filter()
    for g in all_groups:
        g.delete()
    print "  {} groups removed".format(len(all_groups))

    print "\nCREATING GROUPS\n-----------------------"

    print "  creating groups...\n\n",
    Group.objects.get_or_create(name='RDM ACCESS')[0],
    Group.objects.get_or_create(name='DATA CREATORS')[0],

    for res in settings.RESOURCE_TYPE_CONFIGS().values():
        Group.objects.get_or_create(name=res['name'])
예제 #12
0
 def get_type_name(self):
     return settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]['name']
예제 #13
0
    def __init__(self, *args, **kwargs):
        super(Resource, self).__init__(*args, **kwargs)
        resource_name = _('Resource Description')
        if self.entitytypeid:
            resource_name = settings.RESOURCE_TYPE_CONFIGS()[
                self.entitytypeid]['name']
        description_group = {
            'id':
            'resource-description',
            'icon':
            'fa-folder',
            'name':
            resource_name,
            'forms': [
                forms.RelatedResourcesForm.get_info(),
                #forms.ExternalReferenceForm.get_info()
            ]
        }

        self.form_groups.append(description_group)

        if self.entitytypeid == 'HERITAGE_RESOURCE.E18':
            description_group['forms'][:0] = [
                forms.SummaryForm.get_info(),
                forms.ComponentForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.LocationForm.get_info(),
                forms.DatingForm.get_info(),
                forms.MeasurementForm.get_info(),
                forms.ConditionForm.get_info(),
                forms.RelatedFilesForm.get_info(),
            ]

            self.form_groups.append({
                'id':
                'others-resource',
                'icon':
                'fa-dashboard',
                'name':
                _('Other (unused) data'),
                'forms': [
                    forms.ClassificationForm.get_info(),
                    forms.DesignationForm.get_info(),
                    forms.EvaluationForm.get_info(),
                ]
            })

        elif self.entitytypeid == 'HERITAGE_RESOURCE_GROUP.E27':
            description_group['forms'][:0] = [
                forms.SummaryForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.LocationForm.get_info(),
                forms.MeasurementForm.get_info(),
                forms.ConditionForm.get_info(),
                forms.ExternalReferenceForm.get_info()
            ]

            self.form_groups.append({
                'id':
                'others-resource',
                'icon':
                'fa-dashboard',
                'name':
                _('Other (unused) data'),
                'forms': [
                    forms.DistrictClassificationForm.get_info(),
                    forms.EvaluationForm.get_info(),
                    forms.DesignationForm.get_info(),
                ]
            })

        elif self.entitytypeid == 'ACTIVITY.E7':
            description_group['forms'][:0] = [
                forms.ActivitySummaryForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.LocationForm.get_info(),
                forms.ActivityActionsForm.get_info(),
                forms.ExternalReferenceForm.get_info()
            ]

        elif self.entitytypeid == 'ACTOR.E39':
            description_group['forms'][:0] = [
                forms.ActorSummaryForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.LocationForm.get_info(),
                forms.RoleForm.get_info(),
                forms.ExternalReferenceForm.get_info()
            ]

        elif self.entitytypeid == 'HISTORICAL_EVENT.E5':
            description_group['forms'][:0] = [
                forms.HistoricalEventSummaryForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.LocationForm.get_info(),
                forms.PhaseForm.get_info(),
                forms.ExternalReferenceForm.get_info()
            ]

        elif self.entitytypeid == 'INFORMATION_RESOURCE.E73':
            description_group['forms'][:0] = [
                forms.InformationResourceSummaryForm.get_info(),
                forms.PublicationForm.get_info(),
                forms.CoverageForm.get_info(),
                forms.DescriptionForm.get_info(),
                forms.ExternalReferenceForm.get_info()
            ]
            description_group['forms'].append(forms.FileUploadForm.get_info())

        if self.entityid != '':
            self.form_groups.append({
                'id':
                'manage-resource',
                'icon':
                'fa-wrench',
                'name':
                _('Manage') + ' ' +
                settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]
                ['name'].replace(settings.REMOVE_EW_STRING, ''),
                'forms': [
                    forms.ValidateResourceForm.get_info(
                        settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]
                        ['name'].replace(settings.REMOVE_EW_STRING, '')),
                    EditHistory.get_info(),
                    DeleteResourceForm.get_info(
                        settings.RESOURCE_TYPE_CONFIGS()[self.entitytypeid]
                        ['name'].replace(settings.REMOVE_EW_STRING, ''))
                ]
            })
예제 #14
0
파일: setup.py 프로젝트: legiongis/crip
def create_indices():
    Resource().prepare_resource_relations_index(create=True)

    for res_config in settings.RESOURCE_TYPE_CONFIGS().values():
        Resource().prepare_search_index(res_config['resourcetypeid'],
                                        create=True)
예제 #15
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>')
예제 #16
0
def build_search_results_dsl(request):
#    Results are sorted ascendingly by the value of SITE_ID.E42, which is displayed as primary name of Heritage Resources. 
#    Must go back to this method once new Automatic Resource ID has been fully developed (AZ 10/08/16) Update 06/09/16: EAMENA_ID.E42 now used as sorting criterion.

    sorting = {
		"child_entities.label":  {
			"order" : "asc",
			"nested_path": "child_entities",
			"nested_filter": {
				"term": {"child_entities.entitytypeid" : "EAMENA_ID.E42"}
			}
		}
	}
    
    term_filter = request.GET.get('termFilter', '')
    
    
    spatial_filter = JSONDeserializer().deserialize(request.GET.get('spatialFilter', None)) 
    export = request.GET.get('export', None)
    page = 1 if request.GET.get('page') == '' else int(request.GET.get('page', 1))
    temporal_filter = JSONDeserializer().deserialize(request.GET.get('temporalFilter', None))
    boolean_search = request.GET.get('booleanSearch', '')
    filter_and_or = JSONDeserializer().deserialize(request.GET.get('termFilterAndOr', ''))
    filter_grouping = JSONDeserializer().deserialize(request.GET.get('termFilterGroup', ''))
    
    filter_combine_flags = JSONDeserializer().deserialize(request.GET.get('termFilterCombineWithPrev', ''))
    #Ignore first entry as it is a dummy
    filter_combine_flags = filter_combine_flags[1:]
    # filter_combine_flags = [False, True, False, False, False]
    
    # filter_groups = JSONDeserializer().deserialize(request.GET.get('termFilterGroups', ''))
    # Not here yet, so put in some bogus data
    # filter_groups = [
    #     'NAME.E41',
    #     'NAME.E41',
    #     'DISTURBANCE_STATE.E3',
    #     'THREAT_STATE.E3'
    # ]
    
    se = SearchEngineFactory().create()

    if export != None:
        limit = settings.SEARCH_EXPORT_ITEMS_PER_PAGE  
    else:
        limit = settings.SEARCH_ITEMS_PER_PAGE
    
    query = Query(se, start=limit*int(page-1), limit=limit)
    boolquery = Bool()
    boolfilter = Bool()
    is_empty_temporal_filter = True

    # store each search term in an initially. These will be combined based on the global and/or and the optional groupings
    terms_queries = [];

    # logging.warning("-------QUERY-------")

    if term_filter != '' or not is_empty_temporal_filter:
        for index, select_box in enumerate(JSONDeserializer().deserialize(term_filter)):
            selectbox_boolfilter = Bool()
            
            groupid = filter_grouping[index]
            if not groupid == 'No group':
                # build a nested query against the nested_entities

                # build a nested query for each resource type
                for resourcetype in settings.RESOURCE_TYPE_CONFIGS().keys():
                    # trace the path from each term to the group root
                    term_paths = []
                    for term in select_box:

                        # trace path from group root to this term
                        if term['type'] == 'concept':

                            # get all the parent concepts for this value i.e. the field
                            concept_relations = models.ConceptRelations.objects.filter(conceptidto=term['value'], relationtype="member")
                            for relation in concept_relations:
                                term_parent_concept = models.Concepts.objects.get(conceptid=relation.conceptidfrom)

                                # get the steps from the root to that concept
                                if term_parent_concept.nodetype.nodetype == "Collection":
                                    term_schema = Entity.get_mapping_schema_to(term_parent_concept.legacyoid)
                                elif term_parent_concept.nodetype.nodetype == 'Concept':
                                    # need to get at the parent until we reach the root collection. concepts are arranged hierarchically
                                    parent_relations_to = models.ConceptRelations.objects.filter(conceptidto=term_parent_concept.conceptid, relationtype='member')
                                    grandparent = models.Concepts.objects.filter(conceptid=parent_relations_to[0].conceptidfrom)
                                    term_schema = Entity.get_mapping_schema_to(grandparent[0].legacyoid)

                                #this path begins at the root, and ends up at the node in question
                                if resourcetype in term_schema:
                                    term_path = term_schema[resourcetype]['steps']

                                    term_paths.append({
                                        'term': term,
                                        'path': term_path
                                    })
                                    break

                        elif term['type'] == 'term':

                            concept = models.Concepts.objects.get(conceptid=term['context'])
                            term_schema = Entity.get_mapping_schema_to(concept.legacyoid)
                            if resourcetype in term_schema:
                                term_path = term_schema[resourcetype]['steps']

                                term_paths.append({
                                    'term': term,
                                    'path': term_path
                                })

                        elif term['type'] == 'string':
                            term_schema = Entity.get_mapping_schema_to(groupid)
                            if resourcetype in term_schema:
                                term_path = term_schema[resourcetype]['steps']

                                term_paths.append({
                                    'term': term,
                                    'path': term_path
                                })

                    if 'year_min_max' in temporal_filter[index] and len(temporal_filter[index]['year_min_max']) == 2:
                        start_date = date(temporal_filter[index]['year_min_max'][0], 1, 1)
                        end_date = date(temporal_filter[index]['year_min_max'][1], 12, 31)
                        if start_date:
                            start_date = start_date.isoformat()
                        if end_date:
                            end_date = end_date.isoformat()

                        if 'inverted' not in temporal_filter[index]:
                            inverted_temporal_filter = False
                        else:
                            if temporal_filter[index]['inverted']:
                                inverted_temporal_filter = True
                            else:
                                inverted_temporal_filter = False

                        term_paths.append({
                            'term': {
                                'date_operator': '3',
                                'start_date': start_date,
                                'end_date': end_date,
                                'type': 'date',
                                'inverted': inverted_temporal_filter
                            },
                            'path': term_path
                        })


                    if 'filters' in temporal_filter[index]:
                        term_schema = Entity.get_mapping_schema_to(groupid)
                        if resourcetype in term_schema:
                            term_path = term_schema[resourcetype]['steps']

                            for temporal_filter_item in temporal_filter[index]['filters']:
                                date_type = ''
                                searchdate = ''
                                date_operator = ''
                                for node in temporal_filter_item['nodes']:
                                    if node['entitytypeid'] == 'DATE_COMPARISON_OPERATOR.E55':
                                        date_operator = node['value']
                                    elif node['entitytypeid'] == 'date':
                                        searchdate = node['value']
                                    else:
                                        date_type = node['value']

                                date_value = datetime.strptime(searchdate, '%Y-%m-%d').isoformat()
                                if 'inverted' not in temporal_filter[index]:
                                    inverted_temporal_filter = False
                                else:
                                    if temporal_filter[index]['inverted']:
                                        inverted_temporal_filter = True
                                    else:
                                        inverted_temporal_filter = False

                                term_paths.append({
                                    'term': {
                                        'date_operator': date_operator,
                                        'date_value': date_value,
                                        'type': 'date',
                                        'inverted': inverted_temporal_filter
                                    },
                                    'path': term_path
                                })

                    # combine the traced path to build a nested query
                    group_query = nested_query_from_pathed_values(term_paths, 'nested_entity.child_entities')


                    # add nested query to overall query
                    selectbox_boolfilter.should(group_query)
                
                # logging.warning("BOX QUERY - %s", JSONSerializer().serialize(selectbox_boolfilter, indent=2))

            else:    
                for term in select_box:
                    
                    if term['type'] == 'term':
                        entitytype = models.EntityTypes.objects.get(conceptid_id=term['context'])
                        boolfilter_nested = Bool()
                        boolfilter_nested.must(Terms(field='child_entities.entitytypeid', terms=[entitytype.pk]))
                        boolfilter_nested.must(Match(field='child_entities.value', query=term['value'], type='phrase'))
                        nested = Nested(path='child_entities', query=boolfilter_nested)
                        if filter_and_or[index] == 'or':
                            if not term['inverted']:
                                selectbox_boolfilter.should(nested)
                        else:
                            if term['inverted']:
                                selectbox_boolfilter.must_not(nested)
                            else:    
                                selectbox_boolfilter.must(nested)
                                
                    elif term['type'] == 'concept':
                        concept_ids = _get_child_concepts(term['value'])
                        terms = Terms(field='domains.conceptid', terms=concept_ids)
                        nested = Nested(path='domains', query=terms)
                        if filter_and_or[index] == 'or':
                            if not term['inverted']:
                                    selectbox_boolfilter.should(nested)
                        else:
                            if term['inverted']:
                                selectbox_boolfilter.must_not(nested)
                            else:
                                selectbox_boolfilter.must(nested)
                                
                    elif term['type'] == 'string':
                        boolquery2 = Bool() #This bool contains the subset of nested string queries on both domains and child_entities paths
                        boolfilter_folded = Bool() #This bool searches by string in child_entities, where free text strings get indexed
                        boolfilter_folded2 = Bool() #This bool searches by string in the domains path,where controlled vocabulary concepts get indexed
                        boolfilter_folded.should(Match(field='child_entities.value', query=term['value'], type='phrase_prefix', fuzziness='AUTO', operator='and'))
                        boolfilter_folded.should(Match(field='child_entities.value.folded', query=term['value'], type='phrase_prefix', fuzziness='AUTO', operator='and'))
                        boolfilter_folded.should(Match(field='child_entities.value.folded', query=term['value'], fuzziness='AUTO', operator='and'))
                        nested = Nested(path='child_entities', query=boolfilter_folded)
                        boolfilter_folded2.should(Match(field='domains.label', query=term['value'], type='phrase_prefix', fuzziness='AUTO', operator='and'))
                        boolfilter_folded2.should(Match(field='domains.label.folded', query=term['value'], type='phrase_prefix', fuzziness='AUTO', operator='and'))
                        boolfilter_folded2.should(Match(field='domains.label.folded', query=term['value'], fuzziness='AUTO', operator='and'))
                        nested2 = Nested(path='domains', query=boolfilter_folded2)
                        boolquery2.should(nested)
                        boolquery2.should(nested2)
                        if filter_and_or[index] == 'or':
                            if not term['inverted']:
                                # use boolfilter here instead of boolquery because boolquery
                                # can't be combined with other boolfilters using boolean OR
                                selectbox_boolfilter.should(boolquery2)
                        else:
                            if term['inverted']:
                                selectbox_boolfilter.must_not(boolquery2)
                            else:    
                                selectbox_boolfilter.must(boolquery2)
                            
                if 'year_min_max' in temporal_filter[index] and len(temporal_filter[index]['year_min_max']) == 2:
                    start_date = date(temporal_filter[index]['year_min_max'][0], 1, 1)
                    end_date = date(temporal_filter[index]['year_min_max'][1], 12, 31)
                    if start_date:
                        start_date = start_date.isoformat()
                    if end_date:
                        end_date = end_date.isoformat()
                    range = Range(field='dates.value', gte=start_date, lte=end_date)
                    nested = Nested(path='dates', query=range)
            
                    if 'inverted' not in temporal_filter[index]:
                        temporal_filter[index]['inverted'] = False

                    if temporal_filter[index]['inverted']:
                        selectbox_boolfilter.must_not(nested)
                    else:
                        selectbox_boolfilter.must(nested)
                        
                if 'filters' in temporal_filter[index]:
                    for temporal_filter_item in temporal_filter[index]['filters']:
                        date_type = ''
                        searchdate = ''
                        date_operator = ''
                        for node in temporal_filter_item['nodes']:
                            if node['entitytypeid'] == 'DATE_COMPARISON_OPERATOR.E55':
                                date_operator = node['value']
                            elif node['entitytypeid'] == 'date':
                                searchdate = node['value']
                            else:
                                date_type = node['value']


                        date_value = datetime.strptime(searchdate, '%Y-%m-%d').isoformat()

                        if date_operator == '1': # equals query
                            range = Range(field='dates.value', gte=date_value, lte=date_value)
                        elif date_operator == '0': # greater than query 
                            range = Range(field='dates.value', lt=date_value)
                        elif date_operator == '2': # less than query
                            range = Range(field='dates.value', gt=date_value)
                        
                        nested = Nested(path='dates', query=range)
                        if 'inverted' not in temporal_filter[index]:
                            temporal_filter[index]['inverted'] = False

                        if temporal_filter[index]['inverted']:
                            selectbox_boolfilter.must_not(nested)
                        else:
                            selectbox_boolfilter.must(nested)


            terms_queries.append(selectbox_boolfilter)
            # if not selectbox_boolfilter.empty:
            #     if boolean_search == 'or':
            #         boolfilter.should(selectbox_boolfilter)
            #     else:
            #         boolfilter.must(selectbox_boolfilter)
        
        # We now have individual query terms for each of the search components. Combine into one group now
        # Start by building a an array of groups which will be combined according to the global And/Or
        # Queries within one of these groups will be combined by the complement of the global And/Or
        # We may end up with [ [A,B], [C], [D,E] ], which would translate to either:
        #    (A || B) && C && (D || E)
        #       or
        #    (A && B) || C || (D && E)
        # for global AND or OR respectively
        
        # logging.warning("TERMS QUERIES %s", terms_queries)
        
        bool_components = [];
        
        for i, term_query in enumerate(terms_queries):
            if i is 0:
                bool_components.append([term_query])
            else:
                should_group_with_previous = filter_combine_flags[i-1]
                if should_group_with_previous:
                    bool_components[-1].append(term_query)
                else:
                    bool_components.append([term_query])
            
        # logging.warning("BOOL COMPONENTS %s", bool_components)
        # Now build the ES queries
        for bool_component in bool_components:
            if len(bool_component) is 1:
                # just combine this on its own
                q = bool_component[0]
            else:
                q = Bool()
                for sub_component in bool_component:
                    if boolean_search == 'or':
                        #apply the OPPOSITE of the global boolean operator
                        q.must(sub_component)
                    else:
                        q.should(sub_component)
                        
            # combine to the overall query according to the global boolean operator
            if boolean_search == 'or':
                boolfilter.should(q)
            else:
                boolfilter.must(q)

    if 'geometry' in spatial_filter and 'type' in spatial_filter['geometry'] and spatial_filter['geometry']['type'] != '':
        geojson = spatial_filter['geometry']
        if geojson['type'] == 'bbox':
            coordinates = [[geojson['coordinates'][0],geojson['coordinates'][3]], [geojson['coordinates'][2],geojson['coordinates'][1]]]
            geoshape = GeoShape(field='geometries.value', type='envelope', coordinates=coordinates )
            nested = Nested(path='geometries', query=geoshape)
        else:
            buffer = spatial_filter['buffer']
            geojson = JSONDeserializer().deserialize(_buffer(geojson,buffer['width'],buffer['unit']).json)
            geoshape = GeoShape(field='geometries.value', type=geojson['type'], coordinates=geojson['coordinates'] )
            nested = Nested(path='geometries', query=geoshape)

        if 'inverted' not in spatial_filter:
            spatial_filter['inverted'] = False

        if spatial_filter['inverted']:
            boolfilter.must_not(nested)
        else:
            boolfilter.must(nested)

    if not boolquery.empty:
        query.add_query(boolquery)

    if not boolfilter.empty:
        query.add_filter(boolfilter)
    
#  Sorting criterion added to query (AZ 10/08/16)
    query.dsl.update({'sort': sorting})
    # logging.warning("-=-==-=-===-=--=-==-=-===-=- query: -=-==-=-===-=--=-==-=-===-=-> %s", query)

    return query
예제 #17
0
def resource_types(request):
    sorted_resource_types = sorted(settings.RESOURCE_TYPE_CONFIGS().items(),
                                   key=lambda v: v[1]['sort_order'])
    return {'resource_types': sorted_resource_types}
예제 #18
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>')
예제 #19
0
def search_terms(request):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    query = build_search_terms_dsl(request)
    results = query.search(index='term', doc_type='value')

    group_root_node = request.GET.get('group_root_node', '')
    delete_results = []
    for result in results['hits']['hits']:
        prefLabel = get_preflabel_from_conceptid(result['_source']['context'], lang)
        result['_source']['options']['context_label'] = prefLabel['value']
        
        entity_type = None
        
        # if a group is selected we have to filter out the results that don't belong to the selected group
        if group_root_node != 'No group':
            entities = []
            if 'conceptid' in result['_source']['options']:
                # concept: find the entity_type to check if it is connected to the selected group
                valueid = result['_source']['options']["conceptid"]
                
                value_relations_to = models.ConceptRelations.objects.filter(conceptidto=valueid, relationtype='member')
                
                if value_relations_to:
                    for value_relations_to_concept in value_relations_to:
                        value_parent_concept = models.Concepts.objects.filter(conceptid=value_relations_to_concept.conceptidfrom)
                        parent_relations_to = models.ConceptRelations.objects.filter(conceptidto=value_parent_concept[0].conceptid, relationtype='member')

                        if value_parent_concept[0].nodetype.nodetype == 'Concept':
                            # need to get at the parent until we reach the root collection. concepts are arranged hierarchically
                            grandparent = models.Concepts.objects.filter(conceptid=parent_relations_to[0].conceptidfrom)
                            entity_type = grandparent[0].legacyoid
                            entities.append(entity_type)

                        elif value_parent_concept[0].nodetype.nodetype == 'Collection':
                            entity_type = value_parent_concept[0].legacyoid
                            entities.append(entity_type)
                        else:
                            logging.warning("Not a concept or collection")

            else:
                # not a concept - possibly a name field or similar. Use the context
                entity_type = models.EntityTypes.objects.filter(conceptid=result['_source']['context'])
                entities.append(entity_type)

            delete_result = True
            # check the if the entity_type is under the selected root group node
            # so that it can be deleted later
            if entities:
                for entity_type in entities:
                    res = Entity().get_mapping_schema_to(entity_type)

                    # search parents for group_root_node
                    for resourcetype in settings.RESOURCE_TYPE_CONFIGS().keys():
                        if resourcetype in res:
                            for parent in res[resourcetype]['steps']:
                                if parent['entitytyperange'] == group_root_node:
                                    delete_result = False
                                    break
                
            if delete_result:
                delete_results.append(result)

    # deleted the flagged results
    for result in delete_results:
        results['hits']['hits'].remove(result);

    results['hits']['total'] = len(results['hits']['hits'])
        
    return JSONResponse(results)
예제 #20
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id,
                                      include_subconcepts=False,
                                      include_parentconcepts=True,
                                      include_relatedconcepts=False,
                                      up_depth_limit=None,
                                      lang=lang)

        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path[
                    'label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    current_status = 'None'
    user_can_edit_document = False
    resourcetypeid = report_info['type']

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

        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        print "Current status for report: "
        print current_status
        user_can_edit_document = get_user_can_edit_document(
            current_status, 'same_group', user, resourcetypeid, user_groups,
            'same_group')

    else:
        user_groups = []
        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        if current_status != settings.PUBLISHED_LABEL:
            raise UserNotAuthorized(
                'Unauthenticated users can view only published resources!')

    concept_label_ids = set()
    uuid_regex = re.compile(
        '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'
    )

    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels',
                               id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source'][
                        'type'] == 'prefLabel' and concept_label['_source'][
                            'language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else:
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(
                        concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])
    # Podatke na visjih nivojih, ki zdruzujejo vec razlicnih nivojev, prestavimo na prvi nivo
    # To je potrebno zato, ker sicer ne moremo skrivati posameznih sklopov iz skupinske veje
    keys = [
        'REGION_E55', 'SETTLEMENT_TYPE_E55', 'CONTEXT_E55',
        'PLACE_ADDRESS_E45', 'PLACE_CADASTRAL_REFERENCE_E53',
        'ADMINISTRATIVE_SUBDIVISION_E48'
    ]
    old_key = 'PLACE_E53'
    for key in keys:
        if old_key in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key]:
                if key in data:
                    if key not in report_info['source']['graph']:
                        report_info['source']['graph'][key] = []
                    report_info['source']['graph'][key].append(
                        data.pop(key)[0])
    keys = ['SETTING_TYPE_E55', 'DESCRIPTION_OF_LOCATION_E62']
    old_key = 'PLACE_SITE_LOCATION_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(
                            data[old_key][0].pop(key)[0])
    keys = ['DESCRIPTION_OF_LOCATION_E62']
    old_key = 'SPATIAL_COVERAGE_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(
                            data[old_key][0].pop(key)[0])

    # PLY koncnico spremenimo za potrebe reporta v NXS
    if 'FILE_PATH_E62' in report_info['source']['graph']:
        report_info['source']['graph']['FILE_PATH_E62'][0][
            'FILE_PATH_E62__value'] = report_info['source']['graph'][
                'FILE_PATH_E62'][0]['FILE_PATH_E62__label'].replace(
                    ".ply", ".nxs")
        report_info['source']['graph']['FILE_PATH_E62'][0][
            'FILE_PATH_E62__value'] = report_info['source']['graph'][
                'FILE_PATH_E62'][0]['FILE_PATH_E62__value'].replace(
                    ".PLY", ".nxs")
        print 'Koncni path: ' + report_info['source']['graph'][
            'FILE_PATH_E62'][0]['FILE_PATH_E62__value']

    #print report_info['source']['graph']

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_JSC3D': [],
        'INFORMATION_RESOURCE_3DHOP': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity[
                        'entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity[
                        'entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource[
                        'file_path'] = settings.MEDIA_URL + entity['label']

                    related_resource[
                        'file_path'] = settings.MEDIA_URL + entity['label']
                    # PLY koncnico spremenimo za potrebe reporta v NXS
                    if related_resource['file_path'][-3:].lower() == 'ply':
                        related_resource['file_path'] = related_resource[
                            'file_path'].replace(".ply", ".nxs")
                        related_resource['file_path'] = related_resource[
                            'file_path'].replace(".PLY", ".nxs")
                        information_resource_type = '3DHOP'
                    if related_resource['file_path'][-3:].lower(
                    ) == 'stl' or related_resource['file_path'][-3:].lower(
                    ) == 'obj':
                        information_resource_type = 'JSC3D'

                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource[
                        'thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'

        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource[
                    'entityid'] or relationship[
                        'entityid2'] == related_resource['entityid']:
                related_resource['relationship'].append(
                    get_preflabel_from_valueid(
                        relationship['relationshiptype'], lang)['value'])
                if relationship['notes'] != '':
                    related_resource['relationship'].append(
                        html2text.html2text(relationship['notes']))

        if len(related_resource['relationship']) > 0:
            related_resource['relationship'] = '(%s)' % (', '.join(
                related_resource['relationship']))
        else:
            related_resource['relationship'] = ''

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey,
                                         information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    resource_type_config = settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]
    return render_to_response('resource-report.htm', {
        'geometry':
        JSONSerializer().serialize(report_info['source']['geometry']),
        'resourceid':
        resourceid,
        'report_template':
        'views/reports/' + report_info['type'] + '.htm',
        'report_info':
        report_info,
        'related_resource_dict':
        related_resource_dict,
        'main_script':
        'resource-report',
        'active_page':
        'ResourceReport',
        'RESOURCE_TYPE_CONFIGS':
        resource_type_config,
        'user_groups':
        user_groups,
        'current_status':
        current_status,
        'user_can_edit_document':
        user_can_edit_document,
        'help':
        settings.HELP['report']
    },
                              context_instance=RequestContext(request))