Setup a basic flask-admin interface

master
Pierre Jaury 9 years ago
parent 9df197f412
commit a5ffcfdc90

@ -0,0 +1,16 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# Create application
app = Flask(__name__)
app.config.update({
'SQLALCHEMY_DATABASE_URI': 'sqlite:////tmp/freeposte.db'
})
# Create the database
db = SQLAlchemy(app)
from freeposte import views

@ -0,0 +1,34 @@
from freeposte import db
class Domain(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
max_users = db.Column(db.Integer)
max_aliases = db.Column(db.Integer)
def __str__(self):
return self.name
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
domain_id = db.Column(db.Integer, db.ForeignKey(Domain.id))
domain = db.relationship(Domain, backref='users')
password = db.Column(db.String(255))
quota_bytes = db.Column(db.Integer())
def __str__(self):
return '{0}@{1}'.format(self.username, self.domain.name)
class Alias(db.Model):
id = db.Column(db.Integer, primary_key=True)
localpart = db.Column(db.String(80))
domain_id = db.Column(db.Integer, db.ForeignKey(Domain.id))
domain = db.relationship(Domain, backref='aliases')
destination = db.Column(db.String())
def __str__(self):
return '{0}@{1}'.format(self.username, self.domain.name)

@ -0,0 +1,26 @@
import flask_admin as admin
from flask_admin.contrib import sqla
from freeposte import app, db, models
# Flask admin
admin = admin.Admin(app, name='Freeposte.io', template_mode='bootstrap3')
class DomainModelView(sqla.ModelView):
pass
class UserModelView(sqla.ModelView):
pass
class AliasModelView(sqla.ModelView):
pass
# Add views
admin.add_view(DomainModelView(models.Domain, db.session))
admin.add_view(UserModelView(models.User, db.session))
admin.add_view(AliasModelView(models.Alias, db.session))

@ -0,0 +1,2 @@
Flask
Flask-SQLAlchemy

@ -0,0 +1,5 @@
from freeposte import app
if __name__ == '__main__':
app.run(debug=True)

@ -0,0 +1,7 @@
from freeposte import db, models
if __name__ == "__main__":
db.drop_all()
db.create_all()
db.session.commit()

@ -3,8 +3,14 @@ connect = /data/freeposte.db
# Return the user hashed password # Return the user hashed password
password_query = \ password_query = \
SELECT password FROM users, domains WHERE username = '%n' AND domain = '%d' SELECT password \
FROM users INNER JOIN domains ON users.domain_id = domains.id \
WHERE domains.name = '%d' \
AND users.username = '%n'
# Mostly get the user quota # Mostly get the user quota
user_query = \ user_query = \
SELECT '*:bytes=' || quota_bytes AS quota_rule FROM users WHERE username = '%n' AND domain = '%d' SELECT '*:bytes=' || users.quota_bytes AS quota_rule \
FROM users INNER JOIN domains ON users.domain_id = domains.id \
WHERE domains.name = '%d' \
AND users.username = '%n'

@ -1,2 +1,6 @@
dbpath = /data/freeposte.db dbpath = /data/freeposte.db
query = SELECT destination FROM aliases WHERE localpart = '%u' AND domain = '%d' query = \
SELECT destination \
FROM aliases INNER JOIN domains ON aliases.domain_id = domains.id \
WHERE domains.name = '%d' \
AND aliases.localpart = '%n'

@ -1,2 +1,2 @@
dbpath = /data/freeposte.db dbpath = /data/freeposte.db
query = SELECT domain FROM domains WHERE domain='%s' query = SELECT name FROM domains WHERE domain='%s'

Loading…
Cancel
Save