Last active
November 7, 2015 09:57
-
-
Save takwas/f1d1f5dce02f975c0529 to your computer and use it in GitHub Desktop.
Snippets from Sancta Crux app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Path:/sancta_crux_app/__init__.py | |
# Module that initialises and runs the app | |
from flask import Flask, render_template | |
from flask.ext.bootstrap import Bootstrap | |
from flask.ext.moment import Moment | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from config import config | |
from flask.ext.script import Manager | |
from flask.ext.migrate import Migrate, MigrateCommand | |
import template_filters | |
from inspect import getmembers, isfunction | |
bootstrap = Bootstrap() # enable bootstrap templates using extension | |
moment = Moment() | |
db = SQLAlchemy() | |
def create_app(config_key): | |
# create Flask app instance | |
app = Flask(__name__) | |
print "%r" %config #DEBUG PRINT | |
print "TAGAIIINN! %r" %app #DEBUG PRINT | |
# get config settings based on config_key (subclass of | |
# Config class, e.g TestingConfig; | |
# ProductionConfig, ...) in config dict | |
app.config.from_object(config[config_key]) | |
# Call init_app method | |
config[config_key].init_app(app, [bootstrap, moment, db]) | |
# import blueprints | |
from admin_mod import admin as admin_blueprint | |
from user_mod import user as user_blueprint | |
# register template filters with blueprints | |
for name, function in getmembers(template_filters): | |
if isfunction(function): | |
admin_blueprint.add_app_template_filter(function) | |
user_blueprint.add_app_template_filter(function) | |
# register blueprints | |
app.register_blueprint(admin_blueprint) | |
app.register_blueprint(user_blueprint) | |
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Path: /sancta_crux_app/db_ops.py | |
# This is a Python script with a bunch | |
# of helper functions to ease data entry | |
# and retrieval over the SQLAlchemy ORM | |
# in Python | |
from . import db | |
from models import datetime, User, UserVerification, UserDetails, Project, ProjectImage, Category, Location, \ | |
ProjectComment, ProjectLike, CommentLike, UserNotification, Message, Contribution, SlideImage | |
import models | |
#from flask import current_app as app | |
#with app.app_context(): | |
# db.create_all() | |
# insert given record into specified table(model) | |
def insert_val(model, param_dict): | |
row = model(**param_dict) | |
db.session.add(row) | |
commit_db() | |
# insert given list of values into specified | |
# table (model) | |
def insert_vals(model, dict_list): | |
for param_dict in dict_list: | |
insert_val(model, param_dict) | |
# update database with data | |
# model: DB table to be updated | |
# param_dict_ret: dict holding keyword args with which to retrieve previous data, before updation | |
# param_dict_ins: dict holding keyword args with which to insert new data | |
def update_row(model, param_dict_ret, param_dict_ins): | |
row = model.query.filter_by(**param_dict_ret).update(param_dict_ins) | |
commit_db() | |
# Retreive record from specified table (model) using | |
# given key (for field) and value (as record) | |
def ret_val(model, param_dict): | |
row = model.query.filter_by(**param_dict).first() | |
return row | |
# Retreive records from specified table (model) using | |
# given key (for field) and value (as record) | |
def ret_all_val(model, param_dict): | |
row = model.query.filter_by(**param_dict).all() | |
return row | |
# Retreive all records from specified table (model) | |
def ret_all(model): | |
rows = model.query.all() | |
return rows | |
# commit changes to the database | |
def commit_db(): | |
db.session.commit() | |
def get_obj(model, obj): | |
ret_val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Path: /sancta_crux_app/models.py | |
import datetime | |
from . import db | |
print "HE+REEEE! %r" %db # DEBUG PRINT | |
from flask import current_app as app | |
print "AGAIIINN! %r" %app # DEBUG PRINT | |
############################################################################# | |
# Database Model(s) | |
# Users table | |
class User(db.Model): # This 'model' represents a table named 'User' in the database | |
__tablename__ = 'users' # Use proper pluralized table name | |
# primary key | |
user_id = db.Column(db.Integer, primary_key=True) # id field; unique id for each record | |
username = db.Column(db.String(24), index=True, unique=True, nullable=False) # username field/column | |
email = db.Column(db.String(64), unique=True, nullable=False) # email field | |
password = db.Column(db.String(128), nullable=False, default='password') | |
# object initialization method | |
def __init__(self, username, email, password=None): | |
self.username = username | |
self.email = email | |
if password is None: | |
# provide a default password as 'password' | |
password = 'password' | |
self.password = password | |
# string representation for this object | |
def __repr__(self): | |
return '<User %r: %r; %r>' % (self.user_id, self.username, self.email) | |
#User Details table | |
class UserDetails(db.Model): | |
__tablename__ = 'users_details' | |
# primary key | |
user_details_id = db.Column(db.Integer, primary_key=True) | |
f_name = db.Column(db.String(32), default='Anonymous', nullable=True) # f_name field/column in db | |
l_name = db.Column(db.String(32), default='User', nullable=True) # l_name field/column in db | |
gender = db.Column(db.Enum('Male', 'Female'), default='Male', nullable=False) # gender field | |
dob = db.Column(db.DateTime, nullable=True) | |
address = db.Column(db.Text, nullable=True) # address field | |
contact_no = db.Column(db.String(32), unique=True, nullable=True) # phone-number field | |
bio = db.Column(db.Text, nullable=True) | |
profile_img_name = db.Column(db.String(1024), unique=False, nullable=True, default=app.config['DEFAULT_SILHOUETTE_UNKNOWN']) ####TODO:---> # unique:True # user profile image url field | |
reg_time = db.Column(db.DateTime, nullable=True, default=datetime.datetime.utcnow()) # user account creation date and time | |
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id')) | |
user = db.relationship('User', uselist=False, backref=db.backref('user_details', uselist=False)) | |
# object initialization method | |
def __init__(self, user_id, f_name=None, l_name=None, gender=None, dob=None, \ | |
address=None, contact_no= None, bio=None, profile_img_name=None, reg_time=None): | |
self.user_id = user_id | |
if f_name is None: | |
f_name = '' | |
self.f_name = f_name | |
if l_name is None: | |
l_name = '' | |
self.l_name = l_name | |
if gender is None: | |
gender = 'Male' | |
self.gender = gender | |
if dob is None: | |
dob = datetime.date.today() | |
self.dob = dob | |
if address is None: | |
address = '' | |
self.address = address | |
#if contact_no is None: | |
# contact_no = 0 | |
self.contact_no = contact_no | |
if bio is None: | |
bio = '' | |
self.bio = bio | |
if profile_img_name is None: | |
if gender=='Male': | |
profile_img_name = app.config['DEFAULT_SILHOUETTE_MALE'] | |
elif gender=='Female': | |
profile_img_name = app.config['DEFAULT_SILHOUETTE_FEMALE'] | |
self.profile_img_name = profile_img_name | |
if reg_time is None: | |
reg_time = datetime.datetime.utcnow() | |
self.reg_time = reg_time | |
# string representation for this object | |
def __repr__(self): | |
return '<UserDet (name) for %r: %r %r>' % (self.user, self.f_name, self.l_name) | |
# ... | |
#### *Other models removed for brevity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Path: /run.py | |
#!/usr/bin/env python | |
import os | |
from sancta_crux_app import create_app, db | |
#from sancta_crux_app.models import User, Role | |
from flask.ext.script import Manager, Shell | |
from flask.ext.migrate import Migrate, MigrateCommand | |
app = create_app(os.getenv('FLASK_CONFIG_KEY') or 'default') # FLASK_CONFIG_KEY: E.g. ProductionConfig, DevelopmentConfig | |
print "AGAIIINN! %r" %app | |
app_manager = Manager(app) # enable command-line interaction (cmd arg parsing) using extension; run prog with this object instead | |
migrate = Migrate(app, db) | |
# create db file | |
db.create_all() | |
def make_shell_context(): | |
return dict(app=app, db=db)#, User=User, Role=Role) | |
app_manager.add_command("shell", Shell(make_context=make_shell_context)) | |
app_manager.add_command('db', MigrateCommand) | |
# run program | |
if __name__ == '__main__': | |
app_manager.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── config.py | |
├── gitnore | |
│ ├── start_sancta_crux_app.py | |
│ ├── temp.py | |
│ └── tree | |
├── readme.md | |
├── requirements | |
├── run.py | |
├── run.sh | |
├── sancta_crux_app | |
│ ├── admin_mod | |
│ │ ├── controllers.py | |
│ │ ├── errors.py | |
│ │ ├── forms.py | |
│ │ ├── __init__.py | |
│ ├── db_ops.py | |
│ ├── __init__.py | |
│ ├── models.py | |
│ ├── sancta_crux_app_data.sqlite | |
│ ├── static | |
│ │ ├── ... | |
│ ├── template_filters.py | |
│ ├── templates | |
│ │ ├── 404.html | |
│ │ ├── admin | |
│ │ └── user | |
│ │ ├── base.html | |
│ │ ├── ... | |
│ ├── user_mod | |
│ │ ├── controllers.py | |
│ │ ├── errors.py | |
│ │ ├── forms.py | |
│ │ ├── __init__.py | |
│ └── utils.py | |
└── scripts.py | |
29 directories, 337 files | |
*(Manaually edited to remove some unnecessary files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment