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
|
||||
|
||||
|
||||
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):
|
||||
__tablename__ = 'quotes'
|
||||
|
||||
|
@ -10,6 +17,11 @@ class Quote(db.Model):
|
|||
approved = db.Column(db.Boolean)
|
||||
author_ip = 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):
|
||||
|
@ -27,12 +39,5 @@ class Tag(db.Model):
|
|||
name = db.Column(db.String())
|
||||
|
||||
|
||||
def __init(self, name):
|
||||
def __init__(self, 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
|
||||
from flask import render_template, Markup, request, abort, session
|
||||
|
||||
from smash.models_sqlalchemy import *
|
||||
from smash import app, conf, db
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def timestamp():
|
||||
return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
welcome = "<p>Welcome to the quote archive.</p>"
|
||||
|
@ -137,44 +142,18 @@ def add_new():
|
|||
quote_body = request.form["newquote"]
|
||||
quote_tags = request.form["tags"].split(',')
|
||||
|
||||
cur = db.insert(
|
||||
"quotes",
|
||||
"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
|
||||
quote = Quote(quote_body, request.remote_addr, timestamp())
|
||||
quote_tags = [Tag(tag) for tag in quote_tags]
|
||||
|
||||
for tag in quote_tags:
|
||||
tid = -1
|
||||
try:
|
||||
cur = db.insert(
|
||||
"tags",
|
||||
"name",
|
||||
"?",
|
||||
(tag,)
|
||||
)
|
||||
tid = cur.lastrowid
|
||||
except psycopg2.IntegrityError:
|
||||
logger.warning("Tag {} already exists".format(tag))
|
||||
quote.tags.extend(quote_tags)
|
||||
|
||||
db.session.add(quote)
|
||||
db.session.commit()
|
||||
|
||||
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(
|
||||
"message.html",
|
||||
appname=conf.config['APPNAME'],
|
||||
appbrand=conf.config['APPBRAND'],
|
||||
alertclass="alert-success",
|
||||
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