-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (38 loc) · 1.63 KB
/
app.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
# -*-coding:UTF-8 -*
#Standar import
from flask import Flask
import base64
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_utils.functions import database_exists, create_database
# Init application
app = Flask(__name__)
# DB configuration
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://tojo:tiger@localhost:5432/easy_share'
db = SQLAlchemy(app)
# Check if db exists if not create it
if not database_exists('postgresql://tojo:tiger@localhost:5432/easy_share'):
create_database('postgresql://tojo:tiger@localhost:5432/easy_share')
# import all models and create it
import models
db.create_all()
#Import all routes and generate it
import routes
app.register_blueprint(routes.app)
def create(record):
db.session.add(record)
db.session.commit()
# Create admin user
from models.users import Users
from models.photo import Photo
if not Users.query.filter_by(username='admin').first():
create(Users(username = 'admin', email = '[email protected]'))
create(Users(username = 'demo', email = '[email protected]'))
if not Photo.query.filter_by(name='photo1').first():
with open("/Users/tojo/Personel/easy_photo_share/static/images/portfolio/1.jpg", "rb") as image_file:
create(Photo(name= 'photo1', base64 = base64.b64encode(image_file.read())))
with open("/Users/tojo/Personel/easy_photo_share/static/images/portfolio/2.jpg", "rb") as image_file:
create(Photo(name = 'photo2', base64 = base64.b64encode(image_file.read())))
with open("/Users/tojo/Personel/easy_photo_share/static/images/portfolio/3.jpg", "rb") as image_file:
create(Photo(name = 'photo3', base64 = base64.b64encode(image_file.read())))
if __name__ == "__main__":
app.run()