-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·54 lines (40 loc) · 1.74 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from flask.ext.script import Manager
import settings
from app import app, db
from app.core.models import File
from app.core.utils import UnexpectedFileException
from unistorage import (RegularFile as UnistorageRegularFile,
PendingFile as UnistoragePendingFile)
from unistorage.client import UnistorageClient, UnistorageError, UnistorageTimeout
manager = Manager(app)
@manager.command
def create_db():
"""Creates all tables."""
db.create_all()
@manager.command
def update_expiring_files():
"""Updates file entries that expires in an hour."""
next_hour = datetime.utcnow() + timedelta(hours=1)
unistorage = UnistorageClient(settings.UNISTORAGE_URL,
settings.UNISTORAGE_ACCESS_TOKEN)
for db_file in File.query.filter(File.unistorage_valid_until != None,
File.unistorage_valid_until <= next_hour).all():
try:
unistorage_file = unistorage.get_file(db_file.unistorage_resource_uri)
if isinstance(unistorage_file, UnistoragePendingFile):
raise UnexpectedFileException()
if isinstance(unistorage_file, UnistorageRegularFile):
db_file.unistorage_valid_until = None
else:
db_file.unistorage_valid_until = datetime.utcnow() + \
timedelta(seconds=unistorage_file.ttl)
db_file.unistorage_url = unistorage_file.url
db.session.add(db_file)
db.session.commit()
except (UnistorageError, UnistorageTimeout, UnexpectedFileException) as e:
print str(e)
if __name__ == '__main__':
manager.run()