Beispiel #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)
Beispiel #2
0
    def get(self, request, category, forum, idtopic, *args, **kwargs):
        # Get topic
        topic = get_object_or_404(models.Topic,
                                  idtopic=idtopic,
                                  user_id=request.user.id)

        # Init fields form
        form = forms.FormEditTopic(instance=topic)

        data = {
            'form': form,
            'forum': forum,
            'topic': topic,
            'category': category,
        }

        return render(request, self.template_name, data)