Example #1
0
    def post(self, request, category, forum, idtopic, *args, **kwargs):
        # Get topic
        topic = get_object_or_404(models.Topic,
                                  idtopic=idtopic,
                                  user_id=request.user.id)
        file_name = topic.attachment

        # Get form
        form = forms.FormEditTopic(request.POST, request.FILES, instance=topic)
        file_path = settings.MEDIA_ROOT

        if form.is_valid():

            obj = form.save(commit=False)

            title = conditional_escape(request.POST['title'])
            slug = defaultfilters.slugify(request.POST['title'])

            obj.title = title
            obj.slug = slug

            # If check field clear, remove file when update
            if 'attachment-clear' in request.POST:
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    utils.remove_file(route_file)
                except Exception:
                    pass

            # If has attachment
            if 'attachment' in request.FILES:

                if not topic.id_attachment:
                    id_attachment = get_random_string(length=32)
                    obj.id_attachment = id_attachment

                file_name_post = request.FILES['attachment']
                obj.attachment = file_name_post

                # Route previous file
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    # If a previous file exists it removed
                    utils.remove_file(route_file)
                except Exception:
                    pass

            # Update topic
            form.save()

            messages.success(
                request,
                _("The topic '%(topic)s' was successfully edited") %
                {'topic': obj.title})
            return self.form_valid(form, **kwargs)
        else:
            messages.error(request, _("Invalid form"))
            return self.form_invalid(form, **kwargs)
Example #2
0
    def post(self, request, username, *args, **kwargs):
        profile = get_object_or_404(
            models.Profile, iduser=request.user.id
        )

        file_name = profile.photo

        form = forms.FormEditProfile(
            request.POST, request.FILES, instance=profile
        )
        file_path = settings.MEDIA_ROOT

        if form.is_valid():

            obj = form.save(commit=False)
            about = request.POST['about']
            obj.about = about

            # If check field clear, remove file when update
            if 'attachment-clear' in request.POST:
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    utils.remove_file(route_file)
                except Exception:
                    pass

            if 'attachment' in request.FILES:

                if not obj.id_attachment:
                    id_attachment = get_random_string(length=32)
                    obj.id_attachment = id_attachment

                file_name_post = request.FILES['photo']
                obj.photo = file_name_post

                # Route previous file
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    # If a previous file exists it removed
                    utils.remove_file(route_file)
                except Exception:
                    pass

            # Update profile
            form.save()

            messages.success(
                request,
                _("Your profile was successfully edited")
            )
            return self.form_valid(form, **kwargs)
        else:
            messages.error(request, _("Invalid form"))
            return self.form_invalid(form, **kwargs)
Example #3
0
    def post(self, request, category, forum, idtopic, *args, **kwargs):
        # Get topic
        topic = get_object_or_404(
            models.Topic, idtopic=idtopic, user_id=request.user.id
        )
        file_name = topic.attachment

        # Get form
        form = forms.FormEditTopic(request.POST, request.FILES, instance=topic)
        file_path = settings.MEDIA_ROOT

        if form.is_valid():

            obj = form.save(commit=False)

            title = conditional_escape(request.POST['title'])
            slug = defaultfilters.slugify(request.POST['title'])

            obj.title = title
            obj.slug = slug

            # If check field clear, remove file when update
            if 'attachment-clear' in request.POST:
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    utils.remove_file(route_file)
                except Exception:
                    pass

            # If has attachment
            if 'attachment' in request.FILES:

                if not topic.id_attachment:
                    id_attachment = get_random_string(length=32)
                    obj.id_attachment = id_attachment

                file_name_post = request.FILES['attachment']
                obj.attachment = file_name_post

                # Route previous file
                route_file = utils.get_route_file(file_path, file_name.name)

                try:
                    # If a previous file exists it removed
                    utils.remove_file(route_file)
                except Exception:
                    pass

            # Update topic
            form.save()

            messages.success(
                request,
                _("The topic '%(topic)s' was successfully edited")
                % {'topic': obj.title}
            )
            return self.form_valid(form, **kwargs)
        else:
            messages.error(request, _("Invalid form"))
            return self.form_invalid(form, **kwargs)