Migrate quote adding page to use sqlalchemy
This commit is contained in:
parent
fc2cf68500
commit
c07b2a6695
2 changed files with 26 additions and 42 deletions
|
@ -1,6 +1,13 @@
|
||||||
from smash import db
|
from smash import db
|
||||||
|
|
||||||
|
|
||||||
|
tags_to_quotes = db.Table(
|
||||||
|
'tagsToQuotes',
|
||||||
|
db.Column('tagid', db.Integer, db.ForeignKey('tags.id')),
|
||||||
|
db.Column('quoteid', db.Integer, db.ForeignKey('quotes.id'))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Quote(db.Model):
|
class Quote(db.Model):
|
||||||
__tablename__ = 'quotes'
|
__tablename__ = 'quotes'
|
||||||
|
|
||||||
|
@ -10,6 +17,11 @@ class Quote(db.Model):
|
||||||
approved = db.Column(db.Boolean)
|
approved = db.Column(db.Boolean)
|
||||||
author_ip = db.Column(db.String())
|
author_ip = db.Column(db.String())
|
||||||
time = db.Column(db.String())
|
time = db.Column(db.String())
|
||||||
|
tags = db.relationship(
|
||||||
|
'Tag',
|
||||||
|
secondary=tags_to_quotes,
|
||||||
|
backref=db.backref('quotes', lazy='dynamic')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, content, author_ip, time):
|
def __init__(self, content, author_ip, time):
|
||||||
|
@ -27,12 +39,5 @@ class Tag(db.Model):
|
||||||
name = db.Column(db.String())
|
name = db.Column(db.String())
|
||||||
|
|
||||||
|
|
||||||
def __init(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
tags_to_quotes = db.Table(
|
|
||||||
'tagsToQuotes',
|
|
||||||
db.Column('tagid', db.Integer, db.ForeignKey('tags.id')),
|
|
||||||
db.Column('quoteid', db.Integer, db.ForeignKey('quotes.id'))
|
|
||||||
)
|
|
||||||
|
|
|
@ -3,11 +3,16 @@ import logging
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from flask import render_template, Markup, request, abort, session
|
from flask import render_template, Markup, request, abort, session
|
||||||
|
|
||||||
|
from smash.models_sqlalchemy import *
|
||||||
from smash import app, conf, db
|
from smash import app, conf, db
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def timestamp():
|
||||||
|
return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
welcome = "<p>Welcome to the quote archive.</p>"
|
welcome = "<p>Welcome to the quote archive.</p>"
|
||||||
|
@ -137,44 +142,18 @@ def add_new():
|
||||||
quote_body = request.form["newquote"]
|
quote_body = request.form["newquote"]
|
||||||
quote_tags = request.form["tags"].split(',')
|
quote_tags = request.form["tags"].split(',')
|
||||||
|
|
||||||
cur = db.insert(
|
quote = Quote(quote_body, request.remote_addr, timestamp())
|
||||||
"quotes",
|
quote_tags = [Tag(tag) for tag in quote_tags]
|
||||||
"rating, content, approved, author_ip, time",
|
|
||||||
"?, ?, ?, ?, ?",
|
|
||||||
(0, quote_body, 0, request.remote_addr, datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y"),)
|
|
||||||
)
|
|
||||||
qid = cur.lastrowid
|
|
||||||
|
|
||||||
for tag in quote_tags:
|
quote.tags.extend(quote_tags)
|
||||||
tid = -1
|
|
||||||
try:
|
db.session.add(quote)
|
||||||
cur = db.insert(
|
db.session.commit()
|
||||||
"tags",
|
|
||||||
"name",
|
|
||||||
"?",
|
|
||||||
(tag,)
|
|
||||||
)
|
|
||||||
tid = cur.lastrowid
|
|
||||||
except psycopg2.IntegrityError:
|
|
||||||
logger.warning("Tag {} already exists".format(tag))
|
|
||||||
|
|
||||||
if tid != -1:
|
|
||||||
try:
|
|
||||||
db.insert(
|
|
||||||
"tagsToQuotes",
|
|
||||||
"tagid, quoteid",
|
|
||||||
"?, ?",
|
|
||||||
(tid, qid)
|
|
||||||
)
|
|
||||||
except psycopg2.Error:
|
|
||||||
logger.warning("Database error while inserting into tagsToQuotes")
|
|
||||||
return render_template(
|
|
||||||
"message.html",
|
|
||||||
alertclass="alert-danger",
|
|
||||||
message="Could not add your quote. Try again later."
|
|
||||||
)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"message.html",
|
"message.html",
|
||||||
|
appname=conf.config['APPNAME'],
|
||||||
|
appbrand=conf.config['APPBRAND'],
|
||||||
alertclass="alert-success",
|
alertclass="alert-success",
|
||||||
message="Quote added succesfully. It will need to be reviewed by the administrators before it shows up."
|
message="Quote added succesfully. It will need to be reviewed by the administrators before it shows up."
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue