diff options
author | Rogelio Mita <rogeliomita@activitycentral.com> | 2013-07-31 13:40:04 (GMT) |
---|---|---|
committer | Rogelio Mita <rogeliomita@activitycentral.com> | 2013-07-31 13:40:04 (GMT) |
commit | edb1a63d07eea65289768e566aeac73a2056b3b8 (patch) | |
tree | f4b457d49ffbe6af4f4cf462934f725d97b89332 | |
parent | 4cf5c2b581d12dd8c23efcf7c7b667e9e4efde80 (diff) |
base64 encode for fields images
-rw-r--r-- | webapp/polls/models.py | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py index f4d9ed1..1ceac6f 100644 --- a/webapp/polls/models.py +++ b/webapp/polls/models.py @@ -665,6 +665,8 @@ class Field(AbstractObject, ComponentStructure): def __init__(self, data={}, *args, **kwargs): super(Field, self).__init__(*args, **kwargs) + self.poll_id = None + order = data.get('order', None) self.order = int(order) if order else order self.name = data.get('name', None) @@ -831,7 +833,12 @@ class Field(AbstractObject, ComponentStructure): def get_img_name(self): return getattr(self.uploaded_file, "name", self.img) - def to_python(self, with_errors=False, img_serialize=False): + def get_img_absolute_path(self, poll_id): + path = settings.IMAGES_ROOT + '/%s' % poll_id + return path + "/%s" % self.get_img_name() + + # TODO: Need refactoring for receive poll_id in other way + def to_python(self, with_errors=False, img_serialize=False, poll_id=None): data = {} data.update({ @@ -844,7 +851,16 @@ class Field(AbstractObject, ComponentStructure): img_name = self.get_img_name() if img_name: - data.update({"img": img_name}) + if img_serialize: + img_path = self.get_img_absolute_path(poll_id) + + img_file = open(img_path, 'rb') + image_string = base64.b64encode(img_file.read()) + img_file.close() + + data.update({'img': image_string}) + else: + data.update({'img': img_name}) if with_errors: data.update({'errors': self.errors}) @@ -891,7 +907,7 @@ class Group(AbstractObject, ComponentStructure): if len(self.errors): raise Group.ValidationError(str(self.errors)) - def to_python(self, with_errors=False, img_serialize=False): + def to_python(self, with_errors=False, img_serialize=False, poll_id=None): data = {'name': self.name, 'fields': {}} if with_errors: @@ -899,7 +915,9 @@ class Group(AbstractObject, ComponentStructure): for field_obj in self.fields: field_data = field_obj.to_python( - with_errors=with_errors, img_serialize=img_serialize + with_errors=with_errors, + img_serialize=img_serialize, + poll_id=poll_id ) data['fields'].update(field_data) @@ -1062,7 +1080,9 @@ class Structure(AbstractObject, ComponentStructure): for group_obj in self.groups: data['groups'].update( group_obj.to_python( - with_errors=with_errors, img_serialize=img_serialize + with_errors=with_errors, + img_serialize=img_serialize, + poll_id=self.poll_id ) ) @@ -1173,16 +1193,6 @@ class Structure(AbstractObject, ComponentStructure): dst.write(chunk) dst.close() - def get_image_tmp_path(self): - path = settings.IMAGES_ROOT + '/%s/tmp' % self.poll_id - - try: - os.makedirs(path) - except OSError: - pass - - return path - def get_image_path(self): path = settings.IMAGES_ROOT + '/%s' % self.poll_id |