Repository: fossasia/open-event-server

Issue:
Wizard Create Event Step 1: Implement Upload form for Background Image
Please implement the upload form as below
![screenshot from 2016-06-28 12 11 05](https://cloud.githubusercontent.com/assets/1583873/16411867/91c21f8e-3d29-11e6-9e86-d4f2ed37a429.png)

Add features:
- [x] Upload
- [x] Max upload size 10MB
- [x] Show image after upload
- [x] Adjust/Crop Image

![screenshot from 2016-06-28 12 11 29](https://cloud.githubusercontent.com/assets/1583873/16411890/b138ee38-3d29-11e6-84ad-0a5730d7742a.png)

Also ensure, that Images:
- [x] Show up correctly in Front Page
- [ ] Show up in Search results (will be done in #1298)
- [x] Show up on the actual event page



Discussion:

Resolving patch:
diff --git a/open_event/helpers/data.py b/open_event/helpers/data.py
--- a/open_event/helpers/data.py
+++ b/open_event/helpers/data.py
@@ -4,7 +4,9 @@
 import random
 import traceback
 import json
+import types
 from datetime import datetime, timedelta
+import time
 
 from flask import flash, request, url_for, g
 from flask.ext import login
@@ -15,10 +17,10 @@
 from wtforms import ValidationError
 
 from open_event.models.notifications import Notification
-from open_event.helpers.helpers import string_empty, send_new_session_organizer
+from open_event.helpers.helpers import string_empty, send_new_session_organizer, string_not_empty
 from ..helpers.update_version import VersionUpdater
 from ..helpers.data_getter import DataGetter
-from open_event.helpers.storage import upload
+from open_event.helpers.storage import upload, UploadedFile
 from ..helpers import helpers as Helper
 from ..models import db
 from ..models.event import Event, EventsUsers
@@ -780,7 +782,6 @@ def create_event(form, img_files):
                       location_name=form['location_name'],
                       description=form['description'],
                       event_url=form['event_url'],
-                      background_url=form['background_url'],
                       type=form['type'],
                       topic=form['topic'],
                       privacy=form.get('privacy', u'public'),
@@ -824,6 +825,17 @@ def create_event(form, img_files):
             custom_forms_name = form.getlist('custom_form[name]')
             custom_forms_value = form.getlist('custom_form[value]')
 
+            background_image = form['background_url']
+            if string_not_empty(background_image):
+                filename = str(time.time()) + '.png'
+                file_path = os.path.realpath('.') + '/static/temp/' + filename
+                fh = open(file_path, "wb")
+                fh.write(background_image.split(",")[1].decode('base64'))
+                fh.close()
+                background_file = UploadedFile(file_path, filename)
+                background_url = upload(background_file, 'events/%d/background_image' % (int(event.id)))
+                event.background_url = background_url
+
             for index, name in enumerate(session_type_names):
                 if not string_empty(name):
                     session_type = SessionType(name=name, length=session_type_length[index], event_id=event.id)
@@ -904,7 +916,6 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         event.location_name = form['location_name']
         event.description = form['description']
         event.event_url = form['event_url']
-        event.background_url = form['background_url']
         event.type = form['type']
         event.topic = form['topic']
         event.privacy = form.get('privacy', 'public')
@@ -913,6 +924,17 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         event.code_of_conduct = form['code_of_conduct']
         event.ticket_url = form['ticket_url']
 
+        background_image = form['background_url']
+        if string_not_empty(background_image):
+            filename = str(time.time()) + '.png'
+            file_path = os.path.realpath('.') + '/static/temp/' + filename
+            fh = open(file_path, "wb")
+            fh.write(background_image.split(",")[1].decode('base64'))
+            fh.close()
+            background_file = UploadedFile(file_path, filename)
+            background_url = upload(background_file, 'events/%d/background_image' % (int(event.id)))
+            event.background_url = background_url
+
         state = form.get('state', None)
         if state and ((state == u'Published' and not string_empty(
             event.location_name)) or state != u'Published') and login.current_user.is_verified:
diff --git a/open_event/helpers/storage.py b/open_event/helpers/storage.py
--- a/open_event/helpers/storage.py
+++ b/open_event/helpers/storage.py
@@ -1,10 +1,28 @@
 import os
+from shutil import copyfile
+
 from boto.s3.connection import S3Connection
 from boto.s3.key import Key
 
 from open_event.settings import get_settings
 
 
+class UploadedFile(object):
+
+    def __init__(self, file_path, filename):
+        self.file_path = file_path
+        self.filename = filename
+        self.file = open(file_path)
+
+    def save(self, new_path):
+        copyfile(self.file_path, new_path)
+
+    def read(self):
+        return self.file.read()
+
+    def __exit__(self, *args, **kwargs):
+        self.file.close()
+
 def upload(file, key, **kwargs):
     """
     Upload handler


Test patch:
diff --git a/tests/api/utils_post_data.py b/tests/api/utils_post_data.py
--- a/tests/api/utils_post_data.py
+++ b/tests/api/utils_post_data.py
@@ -13,7 +13,7 @@
     "longitude": 0,
     "name": "TestEvent",
     'event_url': "http://site.com",
-    'background_url': "http://imgur.com/image.png",
+    'background_url': "",
     'description': "blah blah",
     "start_time": "2016-05-25T12:12:43",
     "closing_datetime": "2016-05-22T12:12:43",