This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ flask-pyoidc | |
Flask-WTF | ||
PyYAML | ||
humanize | ||
boto3 | ||
-e git+https://github.com/tarekziade/restjson.git#egg=restjson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import logging | ||
|
||
logger = logging.getLogger('servicebook') | ||
__version__ = "0.1" | ||
__version__ = "2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import time | ||
from functools import wraps | ||
from contextlib import contextmanager | ||
import boto3 | ||
|
||
|
||
@contextmanager | ||
def s3bucket(name): | ||
s3 = boto3.resource('s3') | ||
bucket = s3.Bucket(name) | ||
yield bucket, s3 | ||
|
||
|
||
def upload_file(project_id, filename, data, name='servicebook'): | ||
key = '%s-%s' % (project_id, filename) | ||
with s3bucket(name) as (bucket, s3): | ||
data.seek(0) | ||
bucket.upload_fileobj(data, key) | ||
acl = s3.ObjectAcl(name, key) | ||
acl.put(ACL='public-read') | ||
|
||
|
||
_C = {} | ||
|
||
|
||
def cached(name, max_age=60): | ||
def _cached(func): | ||
@wraps(func) | ||
def __cached(*args, **kw): | ||
if name in _C: | ||
when, val = _C[name] | ||
if time.time() - when < max_age: | ||
return val | ||
val = func(*args, **kw) | ||
_C[name] = time.time(), val | ||
return val | ||
return __cached | ||
return _cached | ||
|
||
|
||
# XXX cache -- need a "has bucket changed?" | ||
@cached('list') | ||
def get_list(project_id, name='servicebook'): | ||
keys = [] | ||
filter = str(project_id) + '-' | ||
with s3bucket(name) as (bucket, s3): | ||
for ob in bucket.objects.filter(Prefix=filter): | ||
keys.append(ob.key) | ||
return keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from flask import redirect, Blueprint, request | ||
from flask import render_template | ||
|
||
from serviceweb.auth import only_for_editors | ||
from serviceweb.forms import get_form | ||
from serviceweb.screenshots import upload_file | ||
|
||
|
||
upload = Blueprint('upload', __name__) | ||
|
||
|
||
@upload.route("/project/<int:project_id>/upload", | ||
methods=['POST', 'GET']) | ||
@only_for_editors | ||
def _upload_screenshot(project_id): | ||
backlink = "/project/%d" % project_id | ||
if request.method == 'POST': | ||
data_file = request.files.get('file') | ||
file_name = data_file.filename | ||
upload_file(project_id, file_name, data_file) | ||
return redirect(backlink) | ||
else: | ||
form = get_form('upload')(request.form) | ||
action = "Upload screenshot" | ||
return render_template("edit.html", form=form, action=action, | ||
backlink=backlink, form_id='upload') |