def create_app():
    app = Flask(__name__)
    app.config['DEBUG'] = True
    app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    generate_routes(app)
    db.init_app(app)
    if not os.path.exists(SQLALCHEMY_DATABASE_URI):
        db.app = app
        db.create_all()
    return app
Esempio n. 2
0
class File(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    filename = db.Column(db.String(255), nullable=False, unique=True, index=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), index=True)
    user = db.relationship("User")
    tags = db.relationship("Tag", secondary=association_file_tag)

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False, index=True)
    used_count = db.Column(db.Integer, nullable=False) # How many files use this tag
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False, index=True)
    files = db.relationship("File", secondary=association_file_tag)
    user = db.relationship("User")

db.create_all()

def search_by_tags(include, exclude, user_id):
    sql_statement = "SELECT id, filename\
        FROM file\
        WHERE file.user_id=" + str(user_id)
    if include != None and len(include) > 0:
        include = include.split()
        include = list(map(lambda tag: str((lambda result: result.id if result != None else -1)(db.session.query(Tag.id).filter_by(name=tag,user_id=user_id).first())), include))
        include_count = len(include)
        if app.config["SQLALCHEMY_DATABASE_URI"].startswith("sqlite"):
            includeStatement = ",".join(["?" for i in range(len(include))])
        else:
            includeStatement = ",".join(["%s" for i in range(len(include))])
        sql_statement = sql_statement + " AND (SELECT COUNT(*)\
            FROM association_file_tag\
Esempio n. 3
0
 def setUp(self):
     db.create_all()