Skip to content

Commit

Permalink
Reformat and remove trailing whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautier committed Mar 25, 2015
1 parent f7b6c95 commit cdc287f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
38 changes: 21 additions & 17 deletions file_resubmit/admin.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os
import uuid

from django import forms
from django.forms.widgets import FILE_INPUT_CONTRADICTION
from django.db import models
Expand All @@ -19,17 +19,17 @@


class AdminResubmitBaseWidget(BaseWidget):

def __init__(self, attrs=None, field_type=None):
super(AdminResubmitBaseWidget, self).__init__()
self.cache_key = ''
self.cache_key = ''
self.field_type = field_type

def value_from_datadict(self, data, files, name):
upload = super(AdminResubmitBaseWidget, self).value_from_datadict(data, files, name)
upload = super(AdminResubmitBaseWidget, self).value_from_datadict(
data, files, name)
if upload == FILE_INPUT_CONTRADICTION:
return upload

self.input_name = "%s_cache_key" % name
self.cache_key = data.get(self.input_name, "")

Expand All @@ -43,47 +43,51 @@ def value_from_datadict(self, data, files, name):
upload = restored
files[name] = upload
return upload

def random_key(self):
return uuid.uuid4().hex

def output_extra_data(self, value):
output = ''
if value and self.cache_key:
output += ' ' + self.filename_from_value(value)
if self.cache_key:
output += forms.HiddenInput().render(self.input_name, self.cache_key, {})
output += forms.HiddenInput().render(
self.input_name,
self.cache_key,
{},
)
return output

def filename_from_value(self, value):
if value:
if value:
return os.path.split(value.name)[-1]


class AdminResubmitFileWidget(AdminResubmitBaseWidget):
template_with_initial = ClearableFileInput.template_with_initial
template_with_clear = ClearableFileInput.template_with_clear

def render(self, name, value, attrs=None):
output = ClearableFileInput.render(self, name, value, attrs)
output += self.output_extra_data(value)
return mark_safe(output)


class AdminResubmitImageWidget(AdminResubmitBaseWidget):

def render(self, name, value, attrs=None):
output = super(AdminResubmitImageWidget, self).render(name, value, attrs)
output = super(AdminResubmitImageWidget, self).render(
name, value, attrs)
output += self.output_extra_data(value)
return mark_safe(output)


class AdminResubmitMixin(object):

def formfield_for_dbfield(self, db_field, **kwargs):
if isinstance(db_field, ImageField):
return db_field.formfield(widget=AdminResubmitImageWidget)
elif isinstance(db_field, models.FileField):
return db_field.formfield(widget=AdminResubmitFileWidget)
else:
return super(AdminResubmitMixin, self).formfield_for_dbfield(db_field, **kwargs)
return super(AdminResubmitMixin, self).formfield_for_dbfield(
db_field, **kwargs)
19 changes: 9 additions & 10 deletions file_resubmit/cache.py
Expand Up @@ -9,7 +9,6 @@


class FileCache(object):

def __init__(self):
self.backend = self.get_backend()

Expand All @@ -29,19 +28,19 @@ def set(self, key, upload):
def get(self, key, field_name):
upload = None
state = self.backend.get(key)
if state:
if state:
f = BytesIO()
f.write(state["content"])
upload = InMemoryUploadedFile(
file=f,
field_name=field_name,
name=state["name"],
content_type=state["content_type"],
size=state["size"],
charset=state["charset"])
file=f,
field_name=field_name,
name=state["name"],
content_type=state["content_type"],
size=state["size"],
charset=state["charset"],
)
upload.file.seek(0)
return upload

def delete(self, key):
self.backend.delete(key)

0 comments on commit cdc287f

Please sign in to comment.