Refactoring
This commit is contained in:
0
attract/application/modules/__init__.py
Normal file
0
attract/application/modules/__init__.py
Normal file
7
attract/application/modules/main/__init__.py
Normal file
7
attract/application/modules/main/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from application import app
|
||||
from application.modules.shots import index
|
||||
|
||||
@app.route("/")
|
||||
def homepage():
|
||||
"""Very minimal setup that returns the shot index view"""
|
||||
return index()
|
0
attract/application/modules/nodes/__init__.py
Normal file
0
attract/application/modules/nodes/__init__.py
Normal file
83
attract/application/modules/nodes/models.py
Normal file
83
attract/application/modules/nodes/models.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from application import app
|
||||
from application import db
|
||||
|
||||
import os
|
||||
import os.path as op
|
||||
import datetime
|
||||
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
from werkzeug import secure_filename
|
||||
|
||||
|
||||
def prefix_name(obj, file_data):
|
||||
# Collect name and extension
|
||||
parts = op.splitext(file_data.filename)
|
||||
# Get current time (for unique hash)
|
||||
timestamp = str(round(time.time()))
|
||||
# Has filename only (not extension)
|
||||
file_name = secure_filename(timestamp + '%s' % parts[0])
|
||||
# Put them together
|
||||
full_name = hashlib.md5(file_name).hexdigest() + parts[1]
|
||||
return full_name
|
||||
|
||||
|
||||
# Create directory for file fields to use
|
||||
file_path = op.join(op.dirname(__file__), 'static/files',)
|
||||
try:
|
||||
os.mkdir(file_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
class Status(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
name = db.Column(db.String(120), nullable=False)
|
||||
url = db.Column(db.String(120), nullable=False)
|
||||
description = db.Column(db.Text)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class NodeType(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
name = db.Column(db.String(120), nullable=False)
|
||||
description = db.Column(db.Text)
|
||||
url = db.Column(db.String(120), nullable=False)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
# Create Many to Many table
|
||||
# nodes_assets_table = db.Table('nodes_assets', db.Model.metadata,
|
||||
# db.Column('node_id', db.Integer, db.ForeignKey('node.id')),
|
||||
# db.Column('asset_id', db.Integer, db.ForeignKey('asset.id'))
|
||||
# )
|
||||
|
||||
|
||||
class Node(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
name = db.Column(db.String(120), nullable=False)
|
||||
url = db.Column(db.String(120))
|
||||
description = db.Column(db.Text)
|
||||
main_picture = db.Column(db.String(80))
|
||||
order = db.Column(db.Integer)
|
||||
creation_date = db.Column(db.DateTime(), default=datetime.datetime.now)
|
||||
edit_date = db.Column(db.DateTime())
|
||||
|
||||
parent_id = db.Column(db.Integer, db.ForeignKey('node.id'))
|
||||
parent = db.relationship('Node', remote_side=[id])
|
||||
|
||||
node_type_id = db.Column(db.Integer(), db.ForeignKey(NodeType.id))
|
||||
node_type = db.relationship(NodeType, backref='Node')
|
||||
|
||||
status_id = db.Column(db.Integer(), db.ForeignKey(Status.id))
|
||||
status = db.relationship(Status, backref='Node')
|
||||
|
||||
#assets = db.relationship('Asset', secondary=nodes_assets_table)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
30
attract/application/modules/projects/__init__.py
Normal file
30
attract/application/modules/projects/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from flask import (abort,
|
||||
Blueprint,
|
||||
jsonify,
|
||||
render_template,
|
||||
redirect,
|
||||
request)
|
||||
|
||||
from flask.ext.thumbnails import Thumbnail
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from application.modules.shots import Node
|
||||
from application.modules.shots import NodeType
|
||||
|
||||
# Name of the Blueprint
|
||||
projects = Blueprint('projects', __name__)
|
||||
|
||||
@projects.route("/")
|
||||
def index():
|
||||
projects = {}
|
||||
for project in Node.query.\
|
||||
join(NodeType).\
|
||||
filter(NodeType.url == 'project'):
|
||||
status = None
|
||||
if project.status:
|
||||
status = project.status.name
|
||||
projects[project.id] = dict(
|
||||
name=project.name,
|
||||
status=status)
|
||||
return jsonify(projects=projects)
|
129
attract/application/modules/shots/__init__.py
Normal file
129
attract/application/modules/shots/__init__.py
Normal file
@@ -0,0 +1,129 @@
|
||||
from flask import (abort,
|
||||
Blueprint,
|
||||
jsonify,
|
||||
render_template,
|
||||
redirect,
|
||||
request,
|
||||
flash)
|
||||
|
||||
from flask.ext.thumbnails import Thumbnail
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from flask_wtf import Form
|
||||
from wtforms import TextField
|
||||
from wtforms import BooleanField
|
||||
from wtforms import SelectField
|
||||
from wtforms import TextAreaField
|
||||
from wtforms import IntegerField
|
||||
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from application import db
|
||||
|
||||
from application.modules.nodes.models import Node, NodeType, Status
|
||||
from application.modules.shots.models import NodeShot
|
||||
|
||||
|
||||
# Name of the Blueprint
|
||||
shots = Blueprint('shots', __name__)
|
||||
|
||||
@shots.route("/")
|
||||
def index():
|
||||
shots = []
|
||||
for shot in Node.query.\
|
||||
join(NodeType).\
|
||||
filter(NodeType.url == 'shot'):
|
||||
status = None
|
||||
if shot.status:
|
||||
status = shot.status.name
|
||||
shots.append(dict(
|
||||
id=shot.id,
|
||||
name=shot.name,
|
||||
description=shot.description,
|
||||
duration=shot.node_shot[0].duration,
|
||||
status=status,
|
||||
notes=shot.node_shot[0].notes))
|
||||
return render_template('shots/index.html',
|
||||
title='shots',
|
||||
shots=shots)
|
||||
|
||||
|
||||
@shots.route("/view/<int:shot_id>")
|
||||
def view(shot_id):
|
||||
shot = Node.query.get(shot_id)
|
||||
if shot and shot.node_type.url == 'shot':
|
||||
return render_template('shots/view.html',
|
||||
title='shots',
|
||||
shot=shot,
|
||||
notes=shot.node_shot[0].notes)
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
|
||||
class ShotForm(Form):
|
||||
name = TextField('Shot Name', validators=[DataRequired()])
|
||||
description = TextAreaField('Description', validators=[DataRequired()])
|
||||
status_id = SelectField('Status', coerce=int)
|
||||
duration = IntegerField('Duration')
|
||||
notes = TextAreaField('Notes')
|
||||
|
||||
|
||||
@shots.route("/create", methods=('GET', 'POST'))
|
||||
def create():
|
||||
form = ShotForm()
|
||||
# Populate dropdown select with available Statuses
|
||||
form.status_id.choices = [(status.id, status.name) for status in Status.query.all()]
|
||||
if form.validate_on_submit():
|
||||
shot_type = NodeType.query.filter_by(url='shot').first()
|
||||
shot = Node(
|
||||
name=form.name.data,
|
||||
description=form.description.data,
|
||||
node_type_id=shot_type.id,
|
||||
status_id=form.status_id.data)
|
||||
# Create entry in the attached node table
|
||||
shot.node_shot = [NodeShot(
|
||||
duration=form.duration.data,
|
||||
notes=form.notes.data)]
|
||||
|
||||
db.session.add(shot)
|
||||
db.session.commit()
|
||||
return redirect('/')
|
||||
return render_template('shots/create.html', form=form)
|
||||
|
||||
|
||||
@shots.route("/edit/<int:shot_id>", methods=('GET', 'POST'))
|
||||
def edit(shot_id):
|
||||
shot = Node.query.get(shot_id)
|
||||
|
||||
form = ShotForm(
|
||||
name=shot.name,
|
||||
description=shot.description,
|
||||
duration=shot.node_shot[0].duration,
|
||||
note=shot.node_shot[0].notes)
|
||||
form.status_id.choices = [(status.id, status.name) for status in Status.query.all()]
|
||||
|
||||
if form.validate_on_submit():
|
||||
print shot.node_shot
|
||||
shot.name = form.name.data
|
||||
shot.description = form.description.data
|
||||
shot.node_shot[0].duration = form.duration.data
|
||||
shot.status_id = form.status_id.data
|
||||
shot.node_shot[0].notes = form.notes.data
|
||||
db.session.commit()
|
||||
return redirect('/')
|
||||
return render_template(
|
||||
'shots/edit.html',
|
||||
form=form,
|
||||
shot_id=shot_id)
|
||||
|
||||
|
||||
@shots.route("/delete/<int:shot_id>")
|
||||
def delete(shot_id):
|
||||
shot = Node.query.get(shot_id)
|
||||
if shot:
|
||||
db.session.delete(shot)
|
||||
db.session.commit()
|
||||
return redirect('/')
|
||||
else:
|
||||
abort(404)
|
53
attract/application/modules/shots/models.py
Normal file
53
attract/application/modules/shots/models.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from application import app
|
||||
from application import db
|
||||
|
||||
from application.modules.nodes.models import Node
|
||||
|
||||
class NodeShot(db.Model):
|
||||
"""docstring for NodeShot"""
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
duration = db.Column(db.Integer, nullable=False)
|
||||
notes = db.Column(db.Text)
|
||||
|
||||
node_id = db.Column(db.Integer, db.ForeignKey(Node.id))
|
||||
node = db.relationship(Node, backref='node_shot', uselist=False)
|
||||
|
||||
|
||||
|
||||
# Create Many to Many table
|
||||
"""
|
||||
assets_tags_table = db.Table('assets_tags', db.Model.metadata,
|
||||
db.Column('asset_id', db.Integer, db.ForeignKey('asset.id')),
|
||||
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
|
||||
)
|
||||
"""
|
||||
|
||||
# class Asset(db.Model):
|
||||
# id = db.Column(db.Integer, primary_key=True)
|
||||
# name = db.Column(db.String(120), nullable=False)
|
||||
# description = db.Column(db.Text, nullable=False)
|
||||
# link = db.Column(db.String(512))
|
||||
# picture = db.Column(db.String(80))
|
||||
# size = db.Column(db.String(7))
|
||||
# format = db.Column(db.String(15))
|
||||
# duration = db.Column(db.String(15))
|
||||
|
||||
# nodes = db.relationship('Node', secondary=nodes_assets_table)
|
||||
|
||||
# #tags = db.relationship('Tag', secondary=assets_tags_table)
|
||||
|
||||
# def __str__(self):
|
||||
# return self.name
|
||||
|
||||
"""
|
||||
|
||||
class Tag(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.Unicode(64))
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
"""
|
||||
|
||||
|
Reference in New Issue
Block a user