Repository: fossasia/open-event-server

Issue:
Implement "Use Amazon S3" in Admin Settings
Follow up issue to: https://github.com/fossasia/open-event-orga-server/issues/1020

Please create a tab in the admin section at `/admin/settings/` where admins can choose their upload resource - local or S3 and enter their Amazon S3 Bucket details and reference it in the upload code.

Implement
- [x] Radio buttons to choose "Use local" or "Use Amazon S3"
- [x] Option to enter necessary Amazon S3 credentials



Discussion:
Since admin is a site-level role, it means that if a admin decides to use local or a specific S3 bucket for storage, it will **affect the entire site**. So why bother providing option in the UI ?
To use S3, they can set the env vars. If they don't want to use S3, they can remove the env vars. 
Am I missing something ?

Yes, it is the goal to have control for the entire site. Is there any reason it should not be?

The option in the UI will give us the possibility to confirm the status of the system without looking into the code. Admins might not have access to the code and setup in some systems. And what if Admins change? They should be able to get an overview by looking at the settings.

I agree. Thanks for the explanation.
On Jun 20, 2016 15:21, "Mario Behling" notifications@github.com wrote:

> Yes, it is the goal to have control for the entire site. Is there any
> reason it should not be?
> 
> The option in the UI will give us the possibility to confirm the status of
> the system without looking into the code. Admins might not have access to
> the code and setup in some systems. And what if Admins change? They should
> be able to get an overview by looking at the settings.
> 
> —
> You are receiving this because you were assigned.
> Reply to this email directly, view it on GitHub
> https://github.com/fossasia/open-event-orga-server/issues/1049#issuecomment-227098274,
> or mute the thread
> https://github.com/notifications/unsubscribe/AD3C7fJF59o9_yQkrruQ6SwzyVmdHIayks5qNmKQgaJpZM4I5fBn
> .


Resolving patch:
diff --git a/migrations/versions/34be8a5853bd_.py b/migrations/versions/34be8a5853bd_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/34be8a5853bd_.py
@@ -0,0 +1,27 @@
+"""empty message
+
+Revision ID: 34be8a5853bd
+Revises: ffbb4d4dad39
+Create Date: 2016-06-29 12:03:01.736940
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '34be8a5853bd'
+down_revision = 'ffbb4d4dad39'
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('settings', sa.Column('storage_place', sa.String(), nullable=True))
+    ### end Alembic commands ###
+
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('settings', 'storage_place')
+    ### end Alembic commands ###
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
@@ -13,8 +13,9 @@ def upload(file, key, **kwargs):
     bucket_name = get_settings()['aws_bucket_name']
     aws_key = get_settings()['aws_key']
     aws_secret = get_settings()['aws_secret']
+    storage_place = get_settings()['storage_place']
     # upload
-    if bucket_name and aws_key and aws_secret:
+    if bucket_name and aws_key and aws_secret and storage_place == 's3':
         return upload_to_aws(bucket_name, aws_key, aws_secret, file, key, **kwargs)
     else:
         return upload_local(file, key, **kwargs)
diff --git a/open_event/models/setting.py b/open_event/models/setting.py
--- a/open_event/models/setting.py
+++ b/open_event/models/setting.py
@@ -18,11 +18,13 @@ class Setting(db.Model):
     sendgrid_key = db.Column(db.String)
     # App secret
     secret = db.Column(db.String)
+    # storage place, local, s3, .. can be more in future
+    storage_place = db.Column(db.String)
 
     def __init__(self, aws_key=None, aws_secret=None, aws_bucket_name=None,
                  google_client_id=None, google_client_secret=None,
                  fb_client_id=None, fb_client_secret=None, sendgrid_key=None,
-                 secret=None):
+                 secret=None, storage_place=None):
         self.aws_key = aws_key
         self.aws_secret = aws_secret
         self.aws_bucket_name = aws_bucket_name
@@ -32,6 +34,7 @@ def __init__(self, aws_key=None, aws_secret=None, aws_bucket_name=None,
         self.fb_client_secret = fb_client_secret
         self.sendgrid_key = sendgrid_key
         self.secret = secret
+        self.storage_place = storage_place
 
     def __repr__(self):
         return 'Settings'


Test patch: