def resolver(cls, attname, default_value, root, info, **args):
        retval = getattr(root, attname, default_value)

        # If object is a django model
        if isinstance(retval, Model):
            if isinstance(retval, mdr_models._concept):
                # Use user_can_view to determine if we display
                if perms.user_can_view(info.context.user, retval):
                    return retval
                else:
                    return None

            if isinstance(retval, mdr_models.aristotleComponent):
                # Use user_can_view to determine if we display
                if perms.user_can_view(info.context.user, retval):
                    return retval
                else:
                    return None

            if isinstance(retval, mdr_models.RegistrationAuthority):
                if retval.is_visible:
                    return retval
                else:
                    return None

            return None

        elif isinstance(retval, Manager):

            # Need this for when related manager is returned when querying object.related_set
            # Can safely return restricted queryset
            queryset = retval.get_queryset()

            if queryset.model == slots_models.Slot:
                instance = getattr(retval, 'instance', None)
                if instance:
                    return get_allowed_slots(instance, info.context.user)
                else:
                    return filter_slot_perms(queryset, info.context.user)

            if hasattr(queryset, 'visible'):
                return queryset.visible(info.context.user)

            if issubclass(queryset.model, mdr_models.aristotleComponent):
                return queryset
            
            return None
            
        elif isinstance(retval, QuerySet):

            # In case a queryset is returned
            if hasattr(retval, 'visible'):
                return retval.visible(info.context.user)
            if issubclass(retval.model, mdr_models.aristotleComponent):
                return retval

            return None

        return retval
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        if self.request.user.is_anonymous():
            context['isFavourite'] = False
        else:
            context['isFavourite'] = self.request.user.profile.is_favourite(self.item)

        from reversion.models import Version
        context['last_edit'] = Version.objects.get_for_object(self.item).first()
        # Only display viewable slots
        context['slots'] = get_allowed_slots(self.item, self.user)
        context['item'] = self.item
        context['statuses'] = self.item.current_statuses
        context['discussions'] = self.item.relatedDiscussions.all()
        context['activetab'] = 'item'
        return context
Пример #3
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        if self.request.user.is_anonymous():
            context['isFavourite'] = False
        else:
            context['isFavourite'] = self.request.user.profile.is_favourite(self.item)

        from reversion.models import Version
        context['last_edit'] = Version.objects.get_for_object(self.item).first()
        # Only display viewable slots
        context['slots'] = get_allowed_slots(self.item, self.user)
        context['item'] = self.item
        context['statuses'] = self.item.current_statuses
        context['discussions'] = self.item.relatedDiscussions.all()
        context['vue'] = True

        # Tags
        if self.request.user.is_authenticated():
            item_tags = Favourite.objects.filter(
                tag__profile=self.request.user.profile,
                tag__primary=False,
                item=self.item
            ).order_by('created').values_list('tag__name', flat=True)

            user_tags = Tag.objects.filter(
                profile=self.request.user.profile,
                primary=False
            ).values_list('name', flat=True)

            item_tags = list(item_tags)
            user_tags = list(user_tags)

            context['tags'] = {
                'item': item_tags,
                'user': user_tags
            }
        else:
            context['tags'] = {
                'item': [],
                'user': []
            }

        return context
Пример #4
0
def render_if_condition_met(request,
                            condition,
                            objtype,
                            iid,
                            model_slug=None,
                            name_slug=None,
                            subpage=None):
    item = get_object_or_404(objtype, pk=iid).item
    if item._meta.model_name != model_slug or not slugify(
            item.name).startswith(str(name_slug)):
        return redirect(url_slugify_concept(item))
    if not condition(request.user, item):
        if request.user.is_anonymous():
            return redirect(
                reverse('friendly_login') + '?next=%s' % request.path)
        else:
            raise PermissionDenied

    # We add a user_can_edit flag in addition
    # to others as we have odd rules around who can edit objects.
    isFavourite = request.user.is_authenticated(
    ) and request.user.profile.is_favourite(item)
    from reversion.models import Version
    last_edit = Version.objects.get_for_object(item).first()

    # Only display viewable slots
    slots = get_allowed_slots(item, request.user)

    default_template = "%s/concepts/%s.html" % (
        item.__class__._meta.app_label, item.__class__._meta.model_name)
    return render(
        request,
        [default_template, item.template],
        {
            'item': item,
            'slots': slots,
            # 'view': request.GET.get('view', '').lower(),
            'isFavourite': isFavourite,
            'last_edit': last_edit
        })
Пример #5
0
    def get_dump_object(self, obj):
        data = OrderedDict([('model', force_text(obj._meta))])
        if not self.use_natural_primary_keys or not hasattr(
                obj, 'natural_key'):
            data["pk"] = force_text(obj._get_pk_val(), strings_only=True)
        data['fields'] = self._current

        if 'aristotle_mdr.contrib.identifiers' in settings.INSTALLED_APPS:
            data['identifiers'] = [{
                'namespace': {
                    'naming_authority':
                    scoped_id.namespace.naming_authority.uuid,
                    'shorthand_prefix': scoped_id.namespace.shorthand_prefix,
                },
                'id': scoped_id.identifier,
                'version': scoped_id.version
            } for scoped_id in obj.identifiers.all()]

        if 'aristotle_mdr.contrib.slots' in settings.INSTALLED_APPS:

            try:
                user = self.options['context']['request'].user
            except KeyError:
                user = None

            if user:
                allowed_slots = get_allowed_slots(obj, user)
            else:
                allowed_slots = []

            data['slots'] = [{
                'name': slot.name,
                'type': slot.type,
                'value': slot.value
            } for slot in allowed_slots]

        # if 'aristotle_mdr.contrib.links' in settings.INSTALLED_APPS:
        #     from aristotle_mdr.contrib.links import models as link_models
        #     obj_links = link_models.Link.objects.filter(linkend__concept=obj).all().distinct()

        #     data['links'] = [
        #     [{
        #         'relation': {
        #             link.relation.uuid,
        #         },
        #         'members': [{
        #             'concept': linkend.concept.uuid,
        #             'role': linkend.role.name,
        #             }]
        #         }
        #     }
        #         for linkend in link.linkend_set.all()
        #     ]
        #     for link in obj_links
        # ]

        data['statuses'] = [{
            "change_details":
            status.changeDetails,
            "until_date":
            status.until_date,
            "registration_authority":
            status.registrationAuthority.uuid,
            "state":
            status.state,
            "state_meaning":
            status.get_state_display(),
            "registration_date":
            status.registrationDate
        } for status in obj.current_statuses()]
        return data