0

User should upload his passport data to site. This data is private, so it will be better, if it will be placed outside of server's directories.

Here is a model:

passport_storage = FileSystemStorage(setting('ROOT_PATH'))

class Passport(models.Model):
    user                = models.OneToOneField(User)

    scan                = models.ImageField(verbose_name=u'Scan or photo', storage=passport_storage, upload_to='passport', blank=True)

    def preview_scan(self):
        return '<a href="%s"> <img src="%s%s/?thumb=1"></img> </a>' % (self.id, PASSPORT_URL, self.id)
    preview_scan.allow_tags = True

I wrote view method for getting passport, or it's thumb:

@login_required
def get_passport(request, **kwargs):
    id = kwargs.pop('id', None)
    user = request.user

    passport = Passport.objects.filter(pk=id)
    if user.username != 'admin': # говнокод
        passport = passport.filter(user=user)
    passport = passport[0]
    scan = passport.scan
    path = scan.path
    sz = scan.size

    if request.GET.get('thumb', None):
        path = get_thumb(path)
        sz = os.path.getsize(path)

    filename = os.path.basename(path)
    mt = mimetypes.guess_type(filename)[0]

    wrapper = FileWrapper(file(path))
    response = HttpResponse(wrapper, mimetype=mt)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    response['Content-Length'] = sz

    return response

But ModelForm from Passport model displays old path from /media/. Where can i change form's ImageField link?

night-crawler
  • 1,409
  • 1
  • 26
  • 39

1 Answers1

0

I'd overwrite admin form's widgets to use plain upload widget instead of more complex admin upload widget with a link and delete checkbox. If it is not acceptable, you can write your own widget based on admin file widget, and provide necessary URL there.

ilvar
  • 5,718
  • 1
  • 20
  • 17