Unverified Commit 24366dec authored by CapsLock's avatar CapsLock

[Init] hello world

from bottle import Bottle, route, run, template, request, static_file
import bottle_pgsql
import bottle
import datetime
from urllib.parse import urlparse
import os
domain_name = 'coin.pan'
db_name = 'avecmonfai'
db_username = 'avecmonfai'
db_password = 'avecmonfai'
# CREATE USER avecmonfai with password 'avecmonfai';
# CREATE DATABASE avecmonfai owner avecmonfai;
# CREATE TABLE pictures(id serial PRIMARY KEY, filename VARCHAR(250) NOT NULL, description VARCHAR(500), created_on TIMESTAMP NOT NULL);
# CREATE TABLE domain_names(id serial PRIMARY KEY, name VARCHAR(800) NOT NULL, related_to integer, created_on TIMESTAMP NOT NULL);
# CREATE TABLE pictures_domain_names(id serial PRIMARY KEY, picture_id integer, domain_name_id integer, created_on TIMESTAMP NOT NULL,
# avecmonfai(# CONSTRAINT pictures_domain_names_picture_id_fkey FOREIGN KEY(picture_id) REFERENCES pictures(id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION,
# avecmonfai(# CONSTRAINT pictures_domain_names_domain_name_id_fkey FOREIGN KEY(domain_name_id) REFERENCES domain_names(id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION)
# grant all on pictures to avecmonfai
# grant all on domain_names to avecmonfai
# grant all on pictures_domain_names to avecmonfai
# grant all on pictures_id_seq to avecmonfai;
# grant all on domain_names_id_seq to avecmonfai;
# grant all on pictures_id_seq to avecmonfai;
# grant all on pictures_domain_names_id_seq to avecmonfai;
# grant all on domain_names_related_to_seq to avecmonfai;
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
app = Bottle()
plugin = bottle_pgsql.Plugin(
'dbname=%s user=%s password=%s' % (db_name, db_username, db_password))
def get_host_name():
result = ''
url_parse = urlparse(request.url)
if url_parse is not None:
result = url_parse.hostname
result = result.replace(".%s" % domain_name, "")
if result == domain_name:
result = ''
return result
# accueil
def default(db):
# if row:
# return template('showitem', page=row)
# #return HTTPError(404, "Entity not found")
# return 'not found'
domain_names = []
pictures = []
if get_host_name() == '':
db.execute('select * from domain_names dn')
domain_names = db.fetchall()
'select p.* from pictures p, pictures_domain_names pdn, domain_names dn' \
' where p.id = pdn.picture_id' \
' and pdn.domain_name_id = dn.id' \
' and dn.name = %s', (get_host_name(),))
pictures = db.fetchall()
return template('domains', pictures=pictures, domain_names=domain_names)
# create new alias
def upload_form():
current_domain = get_host_name()
return template('newAlias', current_domain=current_domain)
# upload new pictures
@app.route('/new', method='POST')
def do_upload(db):
domain = request.forms.get('domain')
description = request.forms.get('domain')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png', '.jpg', '.jpeg', 'gif'):
return 'File extension not allowed.'
destination_filename = os.path.join('uploads', '%s%s' %
"INSERT INTO pictures(filename, description, created_on) values (%s, %s, %s)",
(destination_filename, description, datetime.datetime.now()))
db.execute("select lastval()")
picture_id = db.fetchone()['lastval']
db.execute("select * from domain_names where name=%s", (domain, ))
domain_entity = db.fetchone()
domain_id = None
if not domain_entity:
"insert into domain_names (name, created_on) values (%s, %s)",
(domain, datetime.datetime.now()))
db.execute("select lastval()")
domain_id = db.fetchone()['lastval']
domain_id = domain_entity['id']
"INSERT INTO pictures_domain_names(picture_id, domain_name_id, created_on) values (%s, %s, %s)",
(picture_id, domain_id, datetime.datetime.now()))
save_path = os.path.join(PROJECT_PATH, destination_filename)
upload.save(save_path) # appends upload.filename automatically
return 'OK'
# serves static files
def send_static(filename):
return static_file(filename, root=os.path.join(PROJECT_PATH, 'static'))
def send_static(filename):
return static_file(filename, root=os.path.join(PROJECT_PATH, 'uploads'))
if __name__ == "__main__":
run(app, host='localhost', port=8080)
This diff is collapsed.
% for d in domain_names:
% for p in pictures:
<img src="{{p['filename']}}" style="max-width: 320px; max-height: 240px;"/>
% end
\ No newline at end of file
<form action="/new" method="post" enctype="multipart/form-data">
domain name: <input type="text" name="domain" value="{{current_domain}}"/>
description: <input type="text" name="description" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment