-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
187 lines (160 loc) · 6.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2013 Alexandre Bulté <alexandre[at]bulte[dot]net>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
from PIL import Image
import exifread
from datetime import datetime
from flask.ext.script import Server, Manager
from webapp import app, db, user_datastore
from webapp.models import Photo, Menu, Role
manager = Manager(app)
manager.add_command("runserver", Server(host="0.0.0.0"))
ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png']
def get_files_r():
files = []
for dirname, dirnames, filenames in os.walk(app.config.get('PHOTOS_PATH')):
for filename in filenames:
if not 'pyenv' in dirname and not app.config.get('THUMBS_PATH') in dirname:
if filename.split('.')[-1] in ALLOWED_EXTENSIONS:
files.append(os.path.join(dirname, filename))
return files
def get_thumbname_from_name(name):
return name.replace('./', '').replace('/', '_')
def generate_thumbnails(filenames):
saved_files = []
counter = 0
for name in filenames:
counter+= 1
# app.logger.debug("Trying to open %s" % name)
try:
date = None
f = open(name, 'rb')
tags = exifread.process_file(f)
if 'EXIF DateTimeOriginal' in tags.keys():
date = tags['EXIF DateTimeOriginal']
try:
date = datetime.strptime(str(date), '%Y:%m:%d %H:%M:%S')
except:
app.logger.error("Can't parse EXIF date %s" % date)
im = Image.open(name)
im.thumbnail( (75,75) )
tname = get_thumbname_from_name(name)
# app.logger.debug("Saving thumb-%s" % tname)
tpath = "%s/%s" % (app.config.get('THUMBS_PATH'), tname)
im.save(tpath)
saved_files.append({
'path': name,
'rpath': name.replace(app.config.get('PHOTOS_PATH'), ''),
'thumb_path': tname,
'name': name.split('/')[-1] if '/' in name else name,
'date': date
})
sys.stdout.write("\r%f%%" % (counter*100.0/len(filenames)))
sys.stdout.flush()
except IOError:
app.logger.debug("File %s cannot be parsed by PIL, ignoring" % name)
return saved_files
def menu_item(path_array, menu):
if len(path_array) > 0:
menu[path_array[0]] = {}
if len(path_array) > 1:
menu[path_array[0]] = menu_item(path_array[1:], menu[path_array[0]])
return menu
@manager.command
def build_menu():
""" Build menu from photos and shelve it """
# TODO: clean the obsolete paths
root_len = len(app.config.get('PHOTOS_PATH').split('/'))
last_items = []
for dirname, dirnames, filenames in os.walk(app.config.get('PHOTOS_PATH')):
rel_path = dirname.split(app.config.get('PHOTOS_PATH'))[-1]
# exclude root
if rel_path != '':
name = rel_path.split('/')[-1]
cur_len = len(dirname.split('/'))
level = cur_len - root_len - 1
if level >= 0:
if level == 0:
parent = None
query = Menu.query.filter_by(name=name, level=level)
else:
parent = last_items[level-1]
query = Menu.query.filter_by(name=name, level=level, parent_id=parent.id)
if query.first() is None:
menu_item = Menu(name=name, level=level, parent=parent)
db.session.add(menu_item)
db.session.commit()
else:
menu_item = query.first()
if len(last_items) == level:
last_items.append(menu_item)
elif level + 1 > len(last_items):
app.logger.debug('THIS SHOULD NOT HAPPEN')
else:
last_items[level] = menu_item
print 'Menu is built and stored.'
@manager.command
def clean_index():
""" Remove photos index and menus """
photos = Photo.query.all()
for photo in photos:
db.session.delete(photo)
menus = Menu.query.all()
for menu in menus:
db.session.delete(menu)
db.session.commit()
print "Clean."
@manager.command
def index():
""" Indexes in DB photos and builds menu """
# TODO: remove obsolete items
print
print 'Generating thumbnails...'
files = get_files_r()
saved_files = generate_thumbnails(files)
print 'Saving files to DB...'
for photo in saved_files:
# relative path rpath is the 'unique key'
existing = Photo.query.filter_by(rpath=photo['rpath']).first()
if existing is None:
photo_rec = Photo(rpath=photo['rpath'], path=photo['path'], name=photo['name'], thumb_path=photo['thumb_path'], taken=photo['date'])
db.session.add(photo_rec)
db.session.commit()
else:
existing.path = photo['path']
existing.name = photo['name']
existing.taken = photo['date']
existing.thumb_path = photo['thumb_path']
db.session.commit()
print 'Building menu...'
build_menu()
print 'Photos are indexed.'
@manager.command
def initdb(clean=False):
""" Creates db schema """
if clean:
clean_index()
db.create_all()
print "DB inited."
@manager.command
def create_user(email, password, admin=False):
""" Create a test user """
db.create_all()
user = user_datastore.create_user(email=email, password=password)
db.session.commit()
print "User created."
if admin:
admin_role = Role.query.filter_by(name='admin').first()
if admin_role is None:
admin_role = Role(name='admin', description='Administrators')
db.session.add(admin_role)
admin_role.users.append(user)
db.session.commit()
if __name__ == "__main__":
manager.run()