Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using django filer out of admin area (or an alternative library)

Django filer is an awesome tool for managing files, it detects duplicates and organizes files based on their hashes in folders, has great UI for managing files and folders and handles file history and permissions.

I read some of the source code and realized it extensively uses django admin features in code and in templates; is there any way to use these features for non-staff members that are logged in? To give them tools for uploading and managing their own files and folders in their personal upload area (without reinventing the wheel)?

If there isn't an easy way, what alternatives are there and you suggest to provide such functionality with minimum changes in code?

like image 241
Mohammad Jafar Mashhadi Avatar asked Nov 09 '22 08:11

Mohammad Jafar Mashhadi


1 Answers

According to this django-filer is not supposed to work outside of the admin, but with some "glue" i was able to make the uploads work in a "normal" template. Here is some of my code:

    # forms.py

    class PollModelForm(forms.ModelForm):
        uploaded_image = forms.ImageField(required=False)

        class Meta:
            model = Poll
            fields = ['uploaded_image']

    # views.py
    # I used django-extra-views but you can use a normal cbv
class PollCreateView(LoginRequiredMixin, CreateWithInlinesView):
    model = Poll
    form_class = PollModelForm
    template_name = 'polls/poll_form.html'
    success_url = reverse_lazy('polls:poll-list')
    inlines = [ChoiceInline]

    # Powered by django-extra-views for the inlines so a bit different
    @transaction.atomic
    def forms_valid(self, form, inlines):
        # It's more secure this way.
        form.instance.user = self.request.user

        uploaded_file = form.cleaned_data['uploaded_image']
        image = Image.objects.create(
            name=str(uploaded_file), is_public=True, file=uploaded_file,
            description='Poll Image', owner=self.request.user
        )
        form.instance.image = image

        log.info('Poll image uploaded'.format(**locals()))

        return super(PollCreateView, self).forms_valid(form, inlines)

    # HTML
                              <div class="form-group">
                                <input type="file" name="uploaded_image" id="id_uploaded_image">
                                <p class="help-block">Upload image here.</p>
                              </div>
like image 179
psychok7 Avatar answered Nov 15 '22 04:11

psychok7