Beispiel #1
0
    def get_context_data(self, **kwargs):
        context = super(InstUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('DirIns')
        user_id = self.request.user.id
        if self.object:
            user_data['is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        context['user_can_edit'] = True if not self.object or self.object.cooperative_center_code in ['BR1.1', user_data['user_cc']] else False
        if user_role == 'doc':
            context['user_can_change_status'] = False
        else:
            context['user_can_change_status'] = True

        context['settings'] = settings
        context['help_fields'] = get_help_fields('institution')

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_person'] = PersonFormSet(instance=self.object)
            context['formset_phone'] = PhoneFormSet(instance=self.object)
            context['formset_email'] = EmailFormSet(instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_adm'] = AdmFormSet(instance=self.object)
            context['formset_unitlevel'] = UnitLevelFormSet(instance=self.object)

        return context
Beispiel #2
0
    def get_context_data(self, **kwargs):
        context = super(BiblioRefUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('LILDBI')
        user_id = self.request.user.id
        if self.object:
            user_data['is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        if user_role == 'editor_llxp':
            context['user_can_edit'] = True if not self.object or (self.object.status != 1 and self.object.cooperative_center_code == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        elif user_role == 'doc':
            context['user_can_edit'] = True if not self.object or self.object.status < 1 or (self.object.status != 1 and self.object.cooperative_center_code == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        else:
            context['user_can_edit'] = True
            context['user_can_change_status'] = True

        context['settings'] = settings
        context['help_fields'] = get_help_fields('biblioref')

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(instance=self.object)
            context['formset_library'] = LibraryFormSet(instance=self.object,
                                                        queryset=ReferenceLocal.objects.filter(cooperative_center_code=user_data['user_cc']))
            context['formset_complement'] = ComplementFormSet(instance=self.object)

        # source/analytic edition
        if self.object:
            context['document_type'] = "{0}{1}".format(self.object.literature_type, self.object.treatment_level)
        # new source
        else:
            document_type = self.request.GET.get('document_type')
            source_id = self.request.GET.get('source')

            if document_type:
                context['document_type'] = document_type
            elif source_id:
                reference_source = ReferenceSource.objects.get(pk=source_id)
                literature_type = reference_source.literature_type
                if literature_type == 'S':
                    document_type = 'Sas'
                else:
                    document_type = "{0}am".format(literature_type)

                context['document_type'] = document_type


        return context
Beispiel #3
0
    def get_context_data(self, **kwargs):
        context = super(MediaUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('Multimedia')
        user_id = self.request.user.id
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        context['user_data'] = user_data
        context['role'] = user_role
        context['settings'] = settings
        context['help_fields'] = get_help_fields('multimedia')

        if self.request.method == 'GET':
            # special treatment for user of type documentalist is edit media from other user
            # add in the context list of descriptor, keyword and thematic already set for the media
            if user_role == 'doc' and self.object:
                c_type = ContentType.objects.get_for_model(self.get_object())

                context['descriptor_list'] = Descriptor.objects.filter(
                    object_id=self.object.id,
                    content_type=c_type).exclude(created_by_id=user_id,
                                                 status=0)
                context['keyword_list'] = Keyword.objects.filter(
                    object_id=self.object.id,
                    content_type=c_type).exclude(created_by_id=user_id,
                                                 status=0)
                context['thematic_list'] = ResourceThematic.objects.filter(
                    object_id=self.object.id,
                    content_type=c_type).exclude(created_by_id=user_id,
                                                 status=0)

                pending_descriptor_from_user = Descriptor.objects.filter(
                    created_by_id=self.request.user.id, status=0)
                pending_keyword_from_user = Keyword.objects.filter(
                    created_by_id=user_id, status=0)
                pending_thematic_from_user = ResourceThematic.objects.filter(
                    created_by_id=user_id, status=0)

                context['formset_descriptor'] = DescriptorFormSet(
                    instance=self.object,
                    queryset=pending_descriptor_from_user)
                context['formset_keyword'] = KeywordFormSet(
                    instance=self.object, queryset=pending_keyword_from_user)
                context['formset_thematic'] = ResourceThematicFormSet(
                    instance=self.object, queryset=pending_thematic_from_user)
            else:
                context['formset_descriptor'] = DescriptorFormSet(
                    instance=self.object)
                context['formset_keyword'] = KeywordFormSet(
                    instance=self.object)
                context['formset_thematic'] = ResourceThematicFormSet(
                    instance=self.object)

        return context
Beispiel #4
0
    def get_context_data(self, **kwargs):
        context = super(MediaUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('Multimedia')
        user_id = self.request.user.id
        if self.object:
            user_data['is_owner'] = True if self.object.created_by == self.request.user else False
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        context['user_data'] = user_data
        context['role'] = user_role
        context['settings'] = settings
        context['help_fields'] = get_help_fields('multimedia')

        if self.request.method == 'GET':
            # special treatment for user of type documentalist is edit media from other user
            # add in the context list of descriptor, keyword and thematic already set for the media
            if user_role == 'doc' and self.object:
                c_type = ContentType.objects.get_for_model(self.get_object())

                context['descriptor_list'] = Descriptor.objects.filter(
                                                    object_id=self.object.id,
                                                    content_type=c_type).exclude(
                                                    created_by_id=user_id, status=0)
                context['keyword_list'] = Keyword.objects.filter(
                                                    object_id=self.object.id,
                                                    content_type=c_type).exclude(
                                                    created_by_id=user_id, status=0)
                context['thematic_list'] = ResourceThematic.objects.filter(
                                                    object_id=self.object.id,
                                                    content_type=c_type).exclude(
                                                    created_by_id=user_id, status=0)

                pending_descriptor_from_user = Descriptor.objects.filter(
                                                    created_by_id=self.request.user.id, status=0)
                pending_keyword_from_user = Keyword.objects.filter(
                                                    created_by_id=user_id, status=0)
                pending_thematic_from_user = ResourceThematic.objects.filter(
                                                    created_by_id=user_id, status=0)

                context['formset_descriptor'] = DescriptorFormSet(instance=self.object,
                                                                  queryset=pending_descriptor_from_user)
                context['formset_keyword'] = KeywordFormSet(instance=self.object,
                                                            queryset=pending_keyword_from_user)
                context['formset_thematic'] = ResourceThematicFormSet(instance=self.object,
                                                                      queryset=pending_thematic_from_user)
            else:
                context['formset_descriptor'] = DescriptorFormSet(instance=self.object)
                context['formset_keyword'] = KeywordFormSet(instance=self.object)
                context['formset_thematic'] = ResourceThematicFormSet(instance=self.object)

            context['formset_attachment'] = AttachmentFormSet(instance=self.object)

        return context
Beispiel #5
0
    def get_context_data(self, **kwargs):
        context = super(OERUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('OER')
        user_id = self.request.user.id
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        if user_role == 'doc':
            # documentalist can create and edit your own records
            context['user_can_edit'] = True if not self.object or (
                (self.object.created_by == self.request.user)
                and self.object.status < 1) else False
            context['user_can_change_status'] = False
        elif user_role == 'edi':
            # editor can create, edit and change status (publish) your and institution records
            context[
                'user_can_edit'] = True if not self.object or self.object.cooperative_center_code == user_data[
                    'user_cc'] else False
            context[
                'user_can_change_status'] = True if not self.object or self.object.cooperative_center_code == user_data[
                    'user_cc'] else False

        context['settings'] = settings
        context['help_fields'] = get_help_fields('oer')
        context['mandatory_fields'] = [
            'learning_objectives', 'description', 'type', 'learning_context',
            'language', 'creator', 'license'
        ]

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(
                instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(
                instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_relation'] = RelationFormSet(instance=self.object)
            context['formset_thematic'] = ResourceThematicFormSet(
                instance=self.object)

        if self.object:
            context['passive_relationship'] = Relationship.objects.filter(
                oer_referred=self.object)

        return context
Beispiel #6
0
    def get_context_data(self, **kwargs):
        context = super(OERUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('OER')
        user_cc = user_data['user_cc']
        user_id = self.request.user.id
        user_cvsp_node = None
        if self.object:
            user_data['is_owner'] = True if self.object.created_by == self.request.user else False

            # check if user participate of CVSP network
            user_network = user_data['networks']
            user_cvsp_node = next((node.lower() for node in user_network if node.startswith('CVSP')), None)
            if user_cvsp_node:
                user_cvsp_node = user_cvsp_node.split("-",1)[1]

        # create flag that control if user have permission to edit the reference
        obj = self.object
        if user_role == 'doc':
            # documentalist can create and edit your own records
            context['user_can_edit'] = True if not obj or ((obj.created_by == self.request.user) and obj.status < 1) else False
            context['user_can_change_status'] = False
        elif user_role == 'edi':
            # editor can create, edit and change status (publish) for oer of same center code or CVSP node
            context['user_can_edit'] = True if not obj or obj.cooperative_center_code == user_cc or obj.cvsp_node == user_cvsp_node else False
            context['user_can_change_status'] = True if not obj or obj.cooperative_center_code == user_cc or obj.cvsp_node == user_cvsp_node else False

        context['user_data'] = user_data
        context['user_role'] = user_role
        context['settings'] = settings
        context['help_fields'] = get_help_fields('oer')
        context['mandatory_fields'] = ['learning_objectives', 'description', 'type', 'learning_context',
                                       'language', 'creator', 'license']

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_relation'] = RelationFormSet(instance=self.object)
            context['formset_thematic'] = ResourceThematicFormSet(instance=self.object)

        if self.object:
            context['passive_relationship'] = Relationship.objects.filter(oer_referred=self.object)


        return context
Beispiel #7
0
    def get_context_data(self, **kwargs):
        context = super(LeisRefUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('LeisRef')
        user_id = self.request.user.id
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        if user_role == 'doc':
            context[
                'user_can_edit'] = True if not self.object or self.object.status == 0 or (
                    self.object.status != 1
                    and self.object.cooperative_center_code
                    == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        else:
            context['user_can_edit'] = True
            context['user_can_change_status'] = True

        context['settings'] = settings
        context['help_fields'] = get_help_fields('leisref')
        context['indexing_fields'] = [
            'local_descriptors', 'local_geo_descriptors',
            'institution_as_subject'
        ]

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(
                instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(
                instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_relation'] = RelationFormSet(instance=self.object)
            context['formset_thematic'] = ResourceThematicFormSet(
                instance=self.object)

        if self.object:
            context['passive_relationship'] = ActRelationship.objects.filter(
                act_referred=self.object)

        return context
Beispiel #8
0
    def get_context_data(self, **kwargs):
        context = super(LeisRefUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('LeisRef')
        user_id = self.request.user.id
        if self.object:
            user_data['is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        if user_role == 'doc':
            context['user_can_edit'] = True if not self.object or self.object.status == 0 or (self.object.status != 1 and self.object.cooperative_center_code == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        else:
            context['user_can_edit'] = True
            context['user_can_change_status'] = True

        context['settings'] = settings
        context['help_fields'] = get_help_fields('leisref')
        context['indexing_fields'] = ['local_descriptors', 'local_geo_descriptors', 'institution_as_subject']

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_relation'] = RelationFormSet(instance=self.object)
            context['formset_thematic'] = ResourceThematicFormSet(instance=self.object)

        if self.object:
            context['passive_relationship'] = ActRelationship.objects.filter(act_referred=self.object)


        return context
Beispiel #9
0
    def get_context_data(self, **kwargs):
        context = super(InstUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('DirIns')
        user_id = self.request.user.id
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        context[
            'user_can_edit'] = True if not self.object or self.object.cooperative_center_code in [
                'BR1.1', user_data['user_cc']
            ] else False
        if user_role == 'doc':
            context['user_can_change_status'] = False
        else:
            context['user_can_change_status'] = True

        context['settings'] = settings
        context['help_fields'] = get_help_fields('institution')

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_person'] = PersonFormSet(instance=self.object)
            context['formset_phone'] = PhoneFormSet(instance=self.object)
            context['formset_email'] = EmailFormSet(instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_adm'] = AdmFormSet(instance=self.object)
            context['formset_unitlevel'] = UnitLevelFormSet(
                instance=self.object)

        return context
Beispiel #10
0
    def get_context_data(self, **kwargs):
        context = super(OERUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('OER')
        user_cc = user_data['user_cc']
        user_id = self.request.user.id
        user_cvsp_node = None
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False

            # check if user participate of CVSP network
            user_network = user_data['networks']
            user_cvsp_node = next(
                (node.lower()
                 for node in user_network if node.startswith('CVSP')), None)
            if user_cvsp_node:
                user_cvsp_node = user_cvsp_node.split("-", 1)[1]

        # create flag that control if user have permission to edit the reference
        obj = self.object
        if user_role == 'doc':
            # documentalist can create and edit your own records
            context['user_can_edit'] = True if not obj or (
                (obj.created_by == self.request.user)
                and obj.status < 1) else False
            context['user_can_change_status'] = False
        elif user_role == 'edi':
            # editor can create, edit and change status (publish) for oer of same center code or CVSP node
            context[
                'user_can_edit'] = True if not obj or obj.cooperative_center_code == user_cc or obj.cvsp_node == user_cvsp_node else False
            context[
                'user_can_change_status'] = True if not obj or obj.cooperative_center_code == user_cc or obj.cvsp_node == user_cvsp_node else False

        context['user_data'] = user_data
        context['user_role'] = user_role
        context['settings'] = settings
        context['help_fields'] = get_help_fields('oer')
        context['mandatory_fields'] = [
            'learning_objectives', 'description', 'type', 'learning_context',
            'language', 'creator', 'license'
        ]

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(
                instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(
                instance=self.object)
            context['formset_url'] = URLFormSet(instance=self.object)
            context['formset_relation'] = RelationFormSet(instance=self.object)
            context['formset_thematic'] = ResourceThematicFormSet(
                instance=self.object)

        if self.object:
            context['passive_relationship'] = Relationship.objects.filter(
                oer_referred=self.object)

        return context
Beispiel #11
0
    def get_context_data(self, **kwargs):
        context = super(BiblioRefUpdate, self).get_context_data(**kwargs)

        user_data = additional_user_info(self.request)
        user_role = user_data['service_role'].get('LILDBI')
        user_id = self.request.user.id
        if self.object:
            user_data[
                'is_owner'] = True if self.object.created_by == self.request.user else False

        context['user_data'] = user_data
        context['user_role'] = user_role

        # create flag that control if user have permission to edit the reference
        if user_role == 'editor_llxp':
            context['user_can_edit'] = True if not self.object or (
                self.object.status != 1 and self.object.cooperative_center_code
                == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        elif user_role == 'doc':
            context[
                'user_can_edit'] = True if not self.object or self.object.status < 1 or (
                    self.object.status != 1
                    and self.object.cooperative_center_code
                    == user_data['user_cc']) else False
            context['user_can_change_status'] = False
        else:
            context['user_can_edit'] = True
            context['user_can_change_status'] = True

        view_mode = self.request.GET.get('view_mode', False)
        if view_mode:
            context['user_can_edit'] = False

        context['settings'] = settings
        context['view_mode'] = view_mode
        context['help_fields'] = get_help_fields('biblioref')

        if self.object:
            c_type = ContentType.objects.get_for_model(self.get_object())
            context['c_type'] = c_type

        if self.request.method == 'GET':
            context['formset_descriptor'] = DescriptorFormSet(
                instance=self.object)
            context['formset_attachment'] = AttachmentFormSet(
                instance=self.object)
            context['formset_library'] = LibraryFormSet(
                instance=self.object,
                queryset=ReferenceLocal.objects.filter(
                    cooperative_center_code=user_data['user_cc']))
            context['formset_complement'] = ComplementFormSet(
                instance=self.object)

        # source/analytic edition
        if self.object:
            context['document_type'] = "{0}{1}".format(
                self.object.literature_type, self.object.treatment_level)
        # new source
        else:
            document_type = self.request.GET.get('document_type')
            source_id = self.request.GET.get('source')

            if document_type:
                context['document_type'] = document_type
            elif source_id:
                reference_source = ReferenceSource.objects.get(pk=source_id)
                literature_type = reference_source.literature_type
                if literature_type == 'S':
                    document_type = 'Sas'
                else:
                    document_type = "{0}am".format(literature_type)

                context['document_type'] = document_type

        return context
Beispiel #12
0
def create_edit_resource(request, **kwargs):

    resource_id = kwargs.get('resource_id')
    resource = None
    form = None
    form_error_report = None
    formset_descriptor = None
    formset_thematic = None
    formset_keyword  = None
    descriptor_list  = None
    keyword_list     = None
    thematic_list    = None
    valid_for_publication = True
    output = {}

    if resource_id:
        resource = get_object_or_404(Resource, id=resource_id)
    else:
        resource = Resource(created_by=request.user)
        output['is_new'] = True

    user_data = additional_user_info(request)
    user_data['is_owner'] = True if resource.created_by_id == request.user.id else False
    user_role = user_data['service_role'].get('LIS')

    ct = ContentType.objects.get_for_model(resource)

    # save/update
    if request.POST:
        form = ResourceForm(request.POST, request.FILES, instance=resource, user=request.user, user_data=user_data)
        formset_descriptor = DescriptorFormSet(request.POST, instance=resource)
        formset_keyword    = KeywordFormSet(request.POST, instance=resource)
        formset_thematic   = ResourceThematicFormSet(request.POST, instance=resource)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_keyword_valid  = formset_keyword.is_valid()
        formset_thematic_valid = formset_thematic.is_valid()

        # for status = admitted check  if the resource have at least one descriptor and one thematica area
        valid_for_publication = is_valid_for_publication(form,
            [formset_descriptor, formset_keyword, formset_thematic])

        if (form_valid and formset_descriptor_valid and formset_keyword_valid
                and formset_thematic_valid and valid_for_publication):

            if not resource.id:
                resource = form.save()

            formset_descriptor.save()
            formset_keyword.save()
            formset_thematic.save()

            # update solr index
            form.save()
            form.save_m2m()

            output['alert'] = _("Resource successfully edited.")
            output['alerttype'] = "alert-success"

            return redirect('main.views.list_resources')
    # new/edit
    else:
        form = ResourceForm(instance=resource, user_data=user_data)

        form_error_report = ErrorReportForm()

        # if documentalist create a formset with descriptors created by the user
        if user_role == 'doc':
            descriptor_list = Descriptor.objects.filter(object_id=resource.id, content_type=ct).exclude(created_by_id=request.user.id, status=0)
            keyword_list = Keyword.objects.filter(object_id=resource.id, content_type=ct).exclude(created_by_id=request.user.id, status=0)
            thematic_list = ResourceThematic.objects.filter(object_id=resource.id, content_type=ct).exclude(created_by_id=request.user.id, status=0)

            pending_descriptor_from_user = Descriptor.objects.filter(created_by_id=request.user.id, status=0)
            pending_keyword_from_user = Keyword.objects.filter(created_by_id=request.user.id, status=0)
            pending_thematic_from_user = ResourceThematic.objects.filter(created_by_id=request.user.id, status=0)

            formset_descriptor = DescriptorFormSet(instance=resource, queryset=pending_descriptor_from_user)
            formset_keyword  = KeywordFormSet(instance=resource, queryset=pending_keyword_from_user)
            formset_thematic = ResourceThematicFormSet(instance=resource, queryset=pending_thematic_from_user)
        else:
            formset_descriptor = DescriptorFormSet(instance=resource)
            formset_keyword  = KeywordFormSet(instance=resource)
            formset_thematic = ResourceThematicFormSet(instance=resource)

    output['form'] = form
    output['formset_descriptor'] = formset_descriptor
    output['formset_keyword']  = formset_keyword
    output['formset_thematic'] = formset_thematic
    output['form_error_report'] = form_error_report
    output['valid_for_publication'] = valid_for_publication

    output['content_type'] = ct.id

    output['resource'] = resource
    output['descriptor_list'] = descriptor_list
    output['keyword_list'] = keyword_list
    output['thematic_list'] = thematic_list
    output['settings'] = settings
    output['user_data'] = user_data
    output['user_role'] = user_role
    output['help_fields'] = get_help_fields('resources')

    return render_to_response('main/edit-resource.html', output, context_instance=RequestContext(request))
Beispiel #13
0
def create_edit_resource(request, **kwargs):

    resource_id = kwargs.get('resource_id')
    resource = None
    form = None
    form_error_report = None
    formset_descriptor = None
    formset_thematic = None
    formset_keyword = None
    descriptor_list = None
    keyword_list = None
    thematic_list = None
    valid_for_publication = True
    output = {}

    if resource_id:
        resource = get_object_or_404(Resource, id=resource_id)
    else:
        resource = Resource(created_by=request.user)
        output['is_new'] = True

    user_data = additional_user_info(request)
    user_data[
        'is_owner'] = True if resource.created_by_id == request.user.id else False
    user_role = user_data['service_role'].get('LIS')

    ct = ContentType.objects.get_for_model(resource)

    # save/update
    if request.POST:
        form = ResourceForm(request.POST,
                            request.FILES,
                            instance=resource,
                            user=request.user,
                            user_data=user_data)
        formset_descriptor = DescriptorFormSet(request.POST, instance=resource)
        formset_keyword = KeywordFormSet(request.POST, instance=resource)
        formset_thematic = ResourceThematicFormSet(request.POST,
                                                   instance=resource)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_keyword_valid = formset_keyword.is_valid()
        formset_thematic_valid = formset_thematic.is_valid()

        # for status = admitted check  if the resource have at least one descriptor and one thematica area
        valid_for_publication = is_valid_for_publication(
            form, [formset_descriptor, formset_keyword, formset_thematic])

        if (form_valid and formset_descriptor_valid and formset_keyword_valid
                and formset_thematic_valid and valid_for_publication):

            if not resource.id:
                resource = form.save()

            formset_descriptor.save()
            formset_keyword.save()
            formset_thematic.save()

            # update solr index
            form.save()
            form.save_m2m()

            output['alert'] = _("Resource successfully edited.")
            output['alerttype'] = "alert-success"

            redirect_url = request.session.get("filtered_list",
                                               'main.views.list_resources')

            return redirect(redirect_url)
    # new/edit
    else:
        form = ResourceForm(instance=resource, user_data=user_data)

        form_error_report = ErrorReportForm()

        # if documentalist create a formset with descriptors created by the user
        if user_role == 'doc':
            descriptor_list = Descriptor.objects.filter(
                object_id=resource.id,
                content_type=ct).exclude(created_by_id=request.user.id,
                                         status=0)
            keyword_list = Keyword.objects.filter(
                object_id=resource.id,
                content_type=ct).exclude(created_by_id=request.user.id,
                                         status=0)
            thematic_list = ResourceThematic.objects.filter(
                object_id=resource.id,
                content_type=ct).exclude(created_by_id=request.user.id,
                                         status=0)

            pending_descriptor_from_user = Descriptor.objects.filter(
                created_by_id=request.user.id, status=0)
            pending_keyword_from_user = Keyword.objects.filter(
                created_by_id=request.user.id, status=0)
            pending_thematic_from_user = ResourceThematic.objects.filter(
                created_by_id=request.user.id, status=0)

            formset_descriptor = DescriptorFormSet(
                instance=resource, queryset=pending_descriptor_from_user)
            formset_keyword = KeywordFormSet(
                instance=resource, queryset=pending_keyword_from_user)
            formset_thematic = ResourceThematicFormSet(
                instance=resource, queryset=pending_thematic_from_user)
        else:
            formset_descriptor = DescriptorFormSet(instance=resource)
            formset_keyword = KeywordFormSet(instance=resource)
            formset_thematic = ResourceThematicFormSet(instance=resource)

    output['form'] = form
    output['formset_descriptor'] = formset_descriptor
    output['formset_keyword'] = formset_keyword
    output['formset_thematic'] = formset_thematic
    output['form_error_report'] = form_error_report
    output['valid_for_publication'] = valid_for_publication

    output['content_type'] = ct.id

    output['resource'] = resource
    output['descriptor_list'] = descriptor_list
    output['keyword_list'] = keyword_list
    output['thematic_list'] = thematic_list
    output['settings'] = settings
    output['user_data'] = user_data
    output['user_role'] = user_role
    output['help_fields'] = get_help_fields('resources')

    return render_to_response('main/edit-resource.html',
                              output,
                              context_instance=RequestContext(request))