Example #1
0
    def form_valid(self, form):
        formset_descriptor = DescriptorFormSet(self.request.POST, instance=self.object)
        formset_url = URLFormSet(self.request.POST, instance=self.object)
        formset_attachment = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object)
        formset_relation = RelationFormSet(self.request.POST, instance=self.object)
        formset_thematic = ResourceThematicFormSet(self.request.POST, instance=self.object)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()

        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_attachment_valid = formset_attachment.is_valid()
        formset_thematic_valid = formset_thematic.is_valid()
        formset_relation_valid = formset_relation.is_valid()
        formset_url_valid = formset_url.is_valid()

        user_data = additional_user_info(self.request)
        # run cross formsets validations
        valid_for_publication = is_valid_for_publication(form,
                                                         [formset_descriptor, formset_thematic])

        if (form_valid and formset_descriptor_valid and formset_url_valid and formset_thematic_valid and
           formset_attachment_valid and formset_relation_valid and valid_for_publication):

                self.object = form.save()

                formset_descriptor.instance = self.object
                formset_descriptor.save()

                formset_attachment.instance = self.object
                formset_attachment.save()

                formset_url.instance = self.object
                formset_url.save()

                formset_relation.instance = self.object
                formset_relation.save()

                formset_thematic.instance = self.object
                formset_thematic.save()

                # update solr index
                # form.save()
                form.save_m2m()
                return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                           self.get_context_data(form=form,
                                                 formset_descriptor=formset_descriptor,
                                                 formset_attachment=formset_attachment,
                                                 formset_url=formset_url,
                                                 formset_relation=formset_relation,
                                                 formset_thematic=formset_thematic,
                                                 valid_for_publication=valid_for_publication))
Example #2
0
    def form_valid(self, form):
        formset_descriptor = DescriptorFormSet(self.request.POST,
                                               instance=self.object)
        formset_keyword = KeywordFormSet(self.request.POST,
                                         instance=self.object)
        formset_thematic = ResourceThematicFormSet(self.request.POST,
                                                   instance=self.object)
        formset_attachment = AttachmentFormSet(self.request.POST,
                                               self.request.FILES,
                                               instance=self.object)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()
        formset_keyword_valid = formset_keyword.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_thematic_valid = formset_thematic.is_valid()
        formset_attachment_valid = formset_attachment.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
                and formset_attachment_valid):

            self.object = form.save()
            formset_descriptor.instance = self.object
            formset_descriptor.save()

            formset_keyword.instance = self.object
            formset_keyword.save()

            formset_thematic.instance = self.object
            formset_thematic.save()

            formset_attachment.instance = self.object
            formset_attachment.save()

            # update solr index
            form.save()
            # save many-to-many relation fields
            form.save_m2m()
            return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                self.get_context_data(
                    form=form,
                    formset_descriptor=formset_descriptor,
                    formset_keyword=formset_keyword,
                    formset_thematic=formset_thematic,
                    formset_attachment=formset_attachment,
                    valid_for_publication=valid_for_publication))
Example #3
0
    def form_valid(self, form):
        formset_descriptor = DescriptorFormSet(self.request.POST, instance=self.object)
        formset_keyword = KeywordFormSet(self.request.POST, instance=self.object)
        formset_thematic = ResourceThematicFormSet(self.request.POST, instance=self.object)

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

        # if document is created by other user assume formsets descriptor and thematic valid
        if self.object and (self.object.created_by != self.request.user):
            formset_descriptor_valid = True
            formset_thematic_valid = True
        else:
            formset_descriptor_valid = formset_descriptor.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):

                self.object = form.save()
                formset_descriptor.instance = self.object
                formset_descriptor.save()

                formset_keyword.instance = self.object
                formset_keyword.save()

                formset_thematic.instance = self.object
                formset_thematic.save()

                # update solr index
                form.save()
                form.save_m2m()
                return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                           self.get_context_data(form=form,
                                  formset_descriptor=formset_descriptor,
                                  formset_keyword=formset_keyword,
                                  formset_thematic=formset_thematic,
                                  valid_for_publication=valid_for_publication))
Example #4
0
    def form_valid(self, form):
        formset_descriptor = DescriptorFormSet(self.request.POST, instance=self.object)
        formset_keyword = KeywordFormSet(self.request.POST, instance=self.object)
        formset_thematic = ResourceThematicFormSet(self.request.POST, instance=self.object)
        formset_attachment = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()
        formset_keyword_valid = formset_keyword.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_thematic_valid = formset_thematic.is_valid()
        formset_attachment_valid = formset_attachment.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 and formset_attachment_valid):

                self.object = form.save()
                formset_descriptor.instance = self.object
                formset_descriptor.save()

                formset_keyword.instance = self.object
                formset_keyword.save()

                formset_thematic.instance = self.object
                formset_thematic.save()

                formset_attachment.instance = self.object
                formset_attachment.save()

                # update solr index
                form.save()
                # save many-to-many relation fields
                form.save_m2m()
                return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                           self.get_context_data(form=form,
                                                 formset_descriptor=formset_descriptor,
                                                 formset_keyword=formset_keyword,
                                                 formset_thematic=formset_thematic,
                                                 formset_attachment=formset_attachment,
                                                 valid_for_publication=valid_for_publication))
Example #5
0
    def form_valid(self, form):
        formset_links = OnlineResourcesFormSet(self.request.POST, instance=self.object)
        formset_specialty = BVSSpecialtyFormSet(self.request.POST, instance=self.object)
        formset_variance = TitleVarianceFormSet(self.request.POST, instance=self.object)
        formset_indexrange = IndexRangeFormSet(self.request.POST, instance=self.object)
        formset_audit = AuditFormSet(self.request.POST, instance=self.object)
        formset_descriptor = DescriptorFormSet(self.request.POST, instance=self.object)
        formset_keyword = KeywordFormSet(self.request.POST, instance=self.object)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()

        formset_links_valid = formset_links.is_valid()
        formset_specialty_valid = formset_specialty.is_valid()
        formset_variance_valid = formset_variance.is_valid()
        formset_indexrange_valid = formset_indexrange.is_valid()
        formset_audit_valid = formset_audit.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_keyword_valid = formset_keyword.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_links, formset_specialty, formset_variance, formset_indexrange, formset_audit, formset_descriptor, formset_keyword])

        if (form_valid and formset_links_valid and formset_specialty_valid and formset_variance_valid and formset_indexrange_valid and formset_audit_valid and formset_descriptor_valid and formset_keyword_valid and valid_for_publication):

            action = self.request.POST['action']

            if action in ('preview', 'edit'):
                view = 'title/form_preview.html' if action == 'preview' else 'title/title_form.html'

                return render(
                            self.request,
                            view,
                            self.get_context_data(form=form,
                                    formset_links=formset_links,
                                    formset_specialty=formset_specialty,
                                    formset_variance=formset_variance,
                                    formset_indexrange=formset_indexrange,
                                    formset_audit=formset_audit,
                                    formset_descriptor=formset_descriptor,
                                    formset_keyword=formset_keyword,
                                    valid_for_publication=valid_for_publication))
            else:
                self.object = form.save()

                formset_links.instance = self.object
                formset_links.save()

                formset_specialty.instance = self.object
                formset_specialty.save()

                formset_variance.instance = self.object
                formset_variance.save()

                formset_indexrange.instance = self.object
                formset_indexrange.save()

                formset_audit.instance = self.object
                formset_audit.save()

                formset_descriptor.instance = self.object
                formset_descriptor.save()

                formset_keyword.instance = self.object
                formset_keyword.save()

                # update solr index
                form.save()
                form.save_m2m()
                return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                            self.get_context_data(form=form,
                                formset_links=formset_links,
                                formset_specialty=formset_specialty,
                                formset_variance=formset_variance,
                                formset_indexrange=formset_indexrange,
                                formset_audit=formset_audit,
                                formset_descriptor=formset_descriptor,
                                formset_keyword=formset_keyword,
                                valid_for_publication=valid_for_publication))
Example #6
0
    def form_valid(self, form):
        formset_links = OnlineResourcesFormSet(self.request.POST, instance=self.object)
        formset_specialty = BVSSpecialtyFormSet(self.request.POST, instance=self.object)
        formset_variance = TitleVarianceFormSet(self.request.POST, instance=self.object)
        formset_indexrange = IndexRangeFormSet(self.request.POST, instance=self.object)
        formset_audit = AuditFormSet(self.request.POST, instance=self.object)
        formset_descriptor = DescriptorFormSet(self.request.POST, instance=self.object)
        formset_keyword = KeywordFormSet(self.request.POST, instance=self.object)

        # run all validation before for display formset errors at form
        form_valid = form.is_valid()

        formset_links_valid = formset_links.is_valid()
        formset_specialty_valid = formset_specialty.is_valid()
        formset_variance_valid = formset_variance.is_valid()
        formset_indexrange_valid = formset_indexrange.is_valid()
        formset_audit_valid = formset_audit.is_valid()
        formset_descriptor_valid = formset_descriptor.is_valid()
        formset_keyword_valid = formset_keyword.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_links, formset_specialty, formset_variance, formset_indexrange, formset_audit, formset_descriptor, formset_keyword])

        if (form_valid and formset_links_valid and formset_specialty_valid and formset_variance_valid and formset_indexrange_valid and formset_audit_valid and formset_descriptor_valid and formset_keyword_valid and valid_for_publication):

            action = self.request.POST['action']

            if action in ('preview', 'edit'):
                view = 'title/form_preview.html' if action == 'preview' else 'title/title_form.html'

                return render(
                            self.request,
                            view,
                            self.get_context_data(form=form,
                                    formset_links=formset_links,
                                    formset_specialty=formset_specialty,
                                    formset_variance=formset_variance,
                                    formset_indexrange=formset_indexrange,
                                    formset_audit=formset_audit,
                                    formset_descriptor=formset_descriptor,
                                    formset_keyword=formset_keyword,
                                    valid_for_publication=valid_for_publication))
            else:
                self.object = form.save()

                formset_links.instance = self.object
                formset_links.save()

                formset_specialty.instance = self.object
                formset_specialty.save()

                formset_variance.instance = self.object
                formset_variance.save()

                formset_indexrange.instance = self.object
                formset_indexrange.save()

                formset_audit.instance = self.object
                formset_audit.save()

                formset_descriptor.instance = self.object
                formset_descriptor.save()

                formset_keyword.instance = self.object
                formset_keyword.save()

                # update solr index
                form.save()
                form.save_m2m()
                return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
                            self.get_context_data(form=form,
                                formset_links=formset_links,
                                formset_specialty=formset_specialty,
                                formset_variance=formset_variance,
                                formset_indexrange=formset_indexrange,
                                formset_audit=formset_audit,
                                formset_descriptor=formset_descriptor,
                                formset_keyword=formset_keyword,
                                valid_for_publication=valid_for_publication))
Example #7
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

    return render_to_response('main/edit-resource.html', output, context_instance=RequestContext(request))
Example #8
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))