From 971b37c4d2858a26422173bc0c4d010286eae96c Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Sun, 1 Feb 2015 17:56:09 +0000 Subject: [PATCH] Refactoring --- attract/application/__init__.py | 7 +- attract/application/controllers/shots.py | 98 ------------- .../{models => helpers}/__init__.py | 0 attract/application/modules/__init__.py | 0 .../{controllers => modules/main}/__init__.py | 2 +- attract/application/modules/nodes/__init__.py | 0 .../model.py => modules/nodes/models.py} | 61 +-------- .../projects/__init__.py} | 5 +- attract/application/modules/shots/__init__.py | 129 ++++++++++++++++++ attract/application/modules/shots/models.py | 53 +++++++ .../application/templates/shots/create.html | 12 ++ attract/application/templates/shots/edit.html | 16 +++ .../application/templates/shots/index.html | 14 +- attract/application/templates/shots/view.html | 7 + 14 files changed, 240 insertions(+), 164 deletions(-) delete mode 100644 attract/application/controllers/shots.py rename attract/application/{models => helpers}/__init__.py (100%) create mode 100644 attract/application/modules/__init__.py rename attract/application/{controllers => modules/main}/__init__.py (74%) create mode 100644 attract/application/modules/nodes/__init__.py rename attract/application/{models/model.py => modules/nodes/models.py} (57%) rename attract/application/{controllers/projects.py => modules/projects/__init__.py} (89%) create mode 100644 attract/application/modules/shots/__init__.py create mode 100644 attract/application/modules/shots/models.py diff --git a/attract/application/__init__.py b/attract/application/__init__.py index 4bcd4f63..d2e2995e 100644 --- a/attract/application/__init__.py +++ b/attract/application/__init__.py @@ -23,9 +23,10 @@ thumb = Thumbnail(app) assets = Environment(app) # Import controllers -from models import model -from controllers.shots import shots -from controllers.projects import projects +#from models import model +from application.modules.main import homepage +from application.modules.shots import shots +from application.modules.projects import projects # Register blueprints for the imported controllers app.register_blueprint(filemanager) diff --git a/attract/application/controllers/shots.py b/attract/application/controllers/shots.py deleted file mode 100644 index 5a8c9d02..00000000 --- a/attract/application/controllers/shots.py +++ /dev/null @@ -1,98 +0,0 @@ -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 flask_wtf import Form -from wtforms import TextField, BooleanField -from wtforms.validators import DataRequired - -from application import db - -from application.models.model import ( - Node, - NodeType) - -# 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, - status=status)) - return render_template('shots/index.html', - title='shots', - shots=shots) - - -@shots.route("/view/") -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) - else: - abort(404) - - -class ShotForm(Form): - name = TextField('Shot Name', validators=[DataRequired()]) - description = TextField('Description', validators=[DataRequired()]) - -@shots.route("/create", methods=('GET', 'POST')) -def create(): - form = ShotForm() - if form.validate_on_submit(): - shot_type = NodeType.query.filter_by(name='shot').first() - shot = Node( - name=form.name.data, - description=form.description.data, - node_type_id=shot_type.id) - db.session.add(shot) - db.session.commit() - return redirect('/') - return render_template('shots/create.html', form=form) - - -@shots.route("/edit/", methods=('GET', 'POST')) -def edit(shot_id): - shot = Node.query.get(shot_id) - form = ShotForm( - name=shot.name, - description=shot.description) - if form.validate_on_submit(): - shot.name = form.name.data - shot.description=form.description.data - db.session.commit() - return redirect('/') - return render_template( - 'shots/edit.html', - form=form, - shot_id=shot_id) - - -@shots.route("/delete/") -def delete(shot_id): - shot = Node.query.get(shot_id) - if shot: - db.session.delete(shot) - return redirect('/') - else: - abort(404) diff --git a/attract/application/models/__init__.py b/attract/application/helpers/__init__.py similarity index 100% rename from attract/application/models/__init__.py rename to attract/application/helpers/__init__.py diff --git a/attract/application/modules/__init__.py b/attract/application/modules/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/attract/application/controllers/__init__.py b/attract/application/modules/main/__init__.py similarity index 74% rename from attract/application/controllers/__init__.py rename to attract/application/modules/main/__init__.py index a7bb11e3..94bf4fe7 100644 --- a/attract/application/controllers/__init__.py +++ b/attract/application/modules/main/__init__.py @@ -1,5 +1,5 @@ from application import app -from application.controllers.shots import index +from application.modules.shots import index @app.route("/") def homepage(): diff --git a/attract/application/modules/nodes/__init__.py b/attract/application/modules/nodes/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/attract/application/models/model.py b/attract/application/modules/nodes/models.py similarity index 57% rename from attract/application/models/model.py rename to attract/application/modules/nodes/models.py index 3bffb810..8af71412 100644 --- a/attract/application/models/model.py +++ b/attract/application/modules/nodes/models.py @@ -1,7 +1,6 @@ from application import app from application import db - import os import os.path as op import datetime @@ -10,17 +9,6 @@ import hashlib import time from werkzeug import secure_filename -from sqlalchemy import event -from sqlalchemy.event import listens_for - -from flask import ( - render_template, - jsonify, - redirect, - url_for, - request) - -from flask.ext.sqlalchemy import SQLAlchemy def prefix_name(obj, file_data): @@ -63,10 +51,10 @@ class NodeType(db.Model): # 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')) - ) +# 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): @@ -88,47 +76,8 @@ class Node(db.Model): status_id = db.Column(db.Integer(), db.ForeignKey(Status.id)) status = db.relationship(Status, backref='Node') - assets = db.relationship('Asset', secondary=nodes_assets_table) + #assets = db.relationship('Asset', secondary=nodes_assets_table) def __str__(self): return self.name - - -# 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 - -""" - - diff --git a/attract/application/controllers/projects.py b/attract/application/modules/projects/__init__.py similarity index 89% rename from attract/application/controllers/projects.py rename to attract/application/modules/projects/__init__.py index 392a3810..01640e6b 100644 --- a/attract/application/controllers/projects.py +++ b/attract/application/modules/projects/__init__.py @@ -9,9 +9,8 @@ from flask.ext.thumbnails import Thumbnail from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy.orm import aliased -from application.models.model import ( - Node, - NodeType) +from application.modules.shots import Node +from application.modules.shots import NodeType # Name of the Blueprint projects = Blueprint('projects', __name__) diff --git a/attract/application/modules/shots/__init__.py b/attract/application/modules/shots/__init__.py new file mode 100644 index 00000000..a4d0d95c --- /dev/null +++ b/attract/application/modules/shots/__init__.py @@ -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/") +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/", 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/") +def delete(shot_id): + shot = Node.query.get(shot_id) + if shot: + db.session.delete(shot) + db.session.commit() + return redirect('/') + else: + abort(404) diff --git a/attract/application/modules/shots/models.py b/attract/application/modules/shots/models.py new file mode 100644 index 00000000..9f1c23ac --- /dev/null +++ b/attract/application/modules/shots/models.py @@ -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 + +""" + + diff --git a/attract/application/templates/shots/create.html b/attract/application/templates/shots/create.html index 5ac9716f..5f7eb68e 100644 --- a/attract/application/templates/shots/create.html +++ b/attract/application/templates/shots/create.html @@ -15,6 +15,18 @@ {{ form.description.label }} {{ form.description(size=20, class='form-control') }} +
+ {{ form.duration.label }} + {{ form.duration(size=20, class='form-control') }} +
+
+ {{ form.status_id.label }} + {{ form.status_id(class='form-control') }} +
+
+ {{ form.notes.label }} + {{ form.notes(class='form-control') }} +
diff --git a/attract/application/templates/shots/edit.html b/attract/application/templates/shots/edit.html index 6fa28b26..8f8886bd 100644 --- a/attract/application/templates/shots/edit.html +++ b/attract/application/templates/shots/edit.html @@ -15,7 +15,23 @@ {{ form.description.label }} {{ form.description(size=20, class='form-control') }} +
+ {{ form.duration.label }} + {{ form.duration(size=20, class='form-control') }} +
+
+ {{ form.status_id.label }} + {{ form.status_id(class='form-control') }} +
+
+ {{ form.notes.label }} + {{ form.notes(class='form-control') }} +
+
+ Cancel + Delete +
diff --git a/attract/application/templates/shots/index.html b/attract/application/templates/shots/index.html index bdc3d9de..ba69d830 100644 --- a/attract/application/templates/shots/index.html +++ b/attract/application/templates/shots/index.html @@ -20,11 +20,19 @@ {% for shot in shots %} {{shot['name']}} - {{shot['description']}} - + + {% if shot['description'] %} + {{shot['description']|truncate(25)}} + {% endif %} + + {{shot['duration']}} {{shot['status']}} - + + {% if shot['notes'] %} + {{shot['notes']|truncate(25)}} + {% endif %} + Edit diff --git a/attract/application/templates/shots/view.html b/attract/application/templates/shots/view.html index 7246f0ad..f1521795 100644 --- a/attract/application/templates/shots/view.html +++ b/attract/application/templates/shots/view.html @@ -9,6 +9,13 @@

{{shot['description']}}

+

+ {% if notes %} + {{notes}} + {% else %} + No notes at the moment + {% endif %} +