Skip to content

Commit

Permalink
Merge branch 'release/1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
goors committed Nov 13, 2014
2 parents a5a1bdd + 4bc4b00 commit ab2894e
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 146 deletions.
4 changes: 2 additions & 2 deletions PostModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def posts(self, fe = None, tag = None):

db = DB()
if fe:
sql = '''SELECT * FROM Post p LIMIT 0, 3'''
sql = '''SELECT * FROM Post p LIMIT 0, 30'''
query = db.query(sql )
elif tag:
sql = '''SELECT * FROM Post p LEFT JOIN PostTag pt ON p.Id=pt.Post WHERE pt.Tag=%s GROUP BY pt.Post '''
query = db.query(sql, (tag, ))
else:
sql = '''SELECT * FROM Post p WHERE p.User=%s'''
query = db.query(sql, (session["auth"]["Id"], ))
query = db.query(sql, (session["Id"], ))

posts = query.fetchall()
db.close()
Expand Down
25 changes: 7 additions & 18 deletions TagModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

class TagModel:

def __init__(self):
self.cryptkey = appconfig.CRYPT_KEY


def tags(self):

db = DB()
sql = '''SELECT * FROM Tag'''

query = db.query(sql)
tags = query.fetchall()

db.close()
from models.Tag import Tag
tags = Tag.query.all()

if tags:
return tags
Expand All @@ -34,6 +26,7 @@ def addtag(self, name):
#todo return id or something

def getRepeats(self, id):

db = DB()
sql = '''SELECT COUNT(*) as c FROM PostTag WHERE Tag=%s'''

Expand All @@ -43,16 +36,12 @@ def getRepeats(self, id):
return tags['c']

def getTagByName(self, name):
print name
db = DB()
sql = '''SELECT * FROM Tag WHERE TagName=%s'''

query = db.query(sql, (name, ))
tags = query.fetchone()
from models.Tag import Tag
tag = Tag.query.filter_by(TagName=name).first()

db.close()

if tags:
return tags['Id']
if tag:
return tag.Id
return False

68 changes: 27 additions & 41 deletions UserModel.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,58 @@
from DB import *

import appconfig
import datetime
from flask import session
from werkzeug.security import generate_password_hash, check_password_hash
class UserModel:

def __init__(self):
self.cryptkey = appconfig.CRYPT_KEY


def login(self, email, password):

db = DB()
sql = '''SELECT
u.Email as email,
u.Role as role,
u.Nick as nick,
u.Password as password,
u.Id as Id
class UserModel:

FROM User u
WHERE u.Email=%s'''

def login(self, email, password):

query = db.query(sql, (email, ))
user = query.fetchone()
db.close()
from models.User import User
user = User.query.filter_by(Email = email).first()
if user and user.check_password(password):

if user and check_password_hash(user["password"], password):
session['auth'] = user
session['email'] = user.Email
session['nick'] = user.Nick
session['Id'] = user.Id
return True
return False



def register(self, email, password, nick, role, id = None):


db = DB()
from models.User import User
from models.User import db

newuser = User(nick, email, role, password)
if id:
sql = '''UPDATE User set Nick=%s, Email=%s, Role=%s, Password=%s '''
u = User.query.filter_by(Id=id).first()
u.Email = email
u.Role = role
u.set_password(password)
u.Nick = nick

else:
sql = '''INSERT INTO User (Id, Nick, Email, Role, Password) VALUES (NULL, %s, %s, %s, %s) '''
db.session.add(newuser)
res = db.session.commit()
print res

q = db.query(sql, (nick, email, role, generate_password_hash(password)))
db.conn.commit()
#Todo add send email method
return q.lastrowid

def list(self):
db = DB()
sql = '''SELECT * FROM User '''

query = db.query(sql)
users = query.fetchall()
db.close()

from models.User import User
users = User.query.all()
if users:
return users
return False

def getUser(self, id):
db = DB()
sql = '''SELECT * FROM User WHERE Id=%s'''

query = db.query(sql, (id,))
user = query.fetchone()
db.close()
from models.User import User
user = User.query.filter_by(Id=id).first()


if user:
return user
Expand Down
3 changes: 2 additions & 1 deletion blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from controllers import static
from controllers import admin
from controllers import fe

from appconfig import *


account_api = account.account_api
Expand All @@ -19,6 +19,7 @@
app.register_blueprint(fe)

app.config.from_object(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://'+USER+':'+PASS+'@'+HOST+'/'+DATABASE

app.config.update(dict(
DEBUG=True,
Expand Down
135 changes: 55 additions & 80 deletions controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,133 +5,108 @@

admin_api = Blueprint('admin_api', __name__)

@admin_api.before_request
def before_request():
if 'Id' not in session and request.endpoint != '/admin/login':
return redirect("/admin/login")

@admin_api.route('/admin', methods=['GET'])
def posts():

if (session):
post = PostModel()
allPosts = post.posts()

post = PostModel()
allPosts = post.posts()

if(allPosts):
return render_template('admin/index.html', data=session["auth"], posts=allPosts)
return render_template('admin/index.html', data=session["auth"])
if(allPosts):
return render_template('admin/index.html', posts=allPosts)
return render_template('admin/index.html')

return redirect("/admin/login")

@admin_api.route('/admin/add-post', methods=['POST','GET'])
def addpost():

if (session):

tags = TagModel()
if request.method == "POST":
active = 1 if request.form.get('active') else 0
post = PostModel()
pid = post.addPost(request.form['title'], request.form['slug'], request.form['content'], request.files['photo'], active)
post.addTags(request.form.getlist('tags'), pid);
tags = TagModel()
if request.method == "POST":
active = 1 if request.form.get('active') else 0
post = PostModel()
pid = post.addPost(request.form['title'], request.form['slug'], request.form['content'], request.files['photo'], active)
post.addTags(request.form.getlist('tags'), pid);

if (tags.tags()):
return render_template('admin/add-post.html', data=session["auth"], tags=tags.tags())
return render_template('admin/add-post.html', data=session["auth"])
return render_template('admin/add-post.html', tags=tags.tags())

return redirect("/admin/login")

@admin_api.route('/admin/edit-post/<id>', methods=['POST','GET'])

def editpost(id=None):
post = PostModel()

if (session):

post = PostModel()

tags = TagModel()

if request.method == "POST":
tags = TagModel()

if request.method == "POST":

active = '1' if request.form.get('active') else '0'
print active
if request.files['photo']:

file = request.files['photo']
active = '1' if request.form.get('active') else '0'
print active
if request.files['photo']:

else:
file = False
file = request.files['photo']

else:
file = False

post.addPost(
request.form['title'],
request.form['slug'],
request.form['content'],
file,
active,
id)
post.addPost(
request.form['title'],
request.form['slug'],
request.form['content'],
file,
active,
id)

post.addTags(request.form.getlist('tags'), id);
single = post.getPost(id)
postTags = post.getPostTags(id);

post.addTags(request.form.getlist('tags'), id);
single = post.getPost(id)
postTags = post.getPostTags(id);

return render_template('admin/add-post.html', data=session["auth"], post=single, tags=tags.tags(), postTags=postTags)
return redirect("/admin/login")
return render_template('admin/add-post.html', post=single, tags=tags.tags(), postTags=postTags)

@admin_api.route('/admin/add-tag', methods=['POST'])
def addtag():
if request.method == "POST":

if (session):
if request.method == "POST":
if request.json['name']:
tags = TagModel()
id = tags.addtag(request.json['name'])
response = {'response': request.json['name'], 'status': 1, 'message': "Tag created", 'Id': id}
print response
return json.jsonify(response)

if request.json['name']:
tags = TagModel()
id = tags.addtag(request.json['name'])
response = {'response': request.json['name'], 'status': 1, 'message': "Tag created", 'Id':id}
print response
return json.jsonify(response)

#return redirect("/admin/login")
@admin_api.route('/admin/delete-post/<id>', methods=['GET'])
def deletepost(id=None):
post = PostModel()
post.deletePost(id)

if (session):
post = PostModel()
post.deletePost(id)

return redirect("/admin/login")


@admin_api.route('/admin/users', methods=['GET'])
def users():

if(session):
user = UserModel();
return render_template("admin/users.html", users=user.list(), data=session['auth'])
return redirect("/admin/login")
user = UserModel();
return render_template("admin/users.html", users=user.list())

@admin_api.route('/admin/edit-user/<id>', methods=['GET','POST'])
def edituser(id = None):
user = UserModel()

if(session):
user = UserModel()
if request.method == "POST":
user.register(request.form['email'], request.form['password'], request.form['nick'], request.form['role'], id)

if request.method == "POST":
user.register(request.form['email'], request.form['password'], request.form['nick'], request.form['role'], id)

return render_template("admin/add-user.html", data=session['auth'], user=user.getUser(id))
return redirect("/admin/login")
return render_template("admin/add-user.html", user=user.getUser(id))

@admin_api.route('/admin/register', methods=['GET','POST'])
def adduser():
user = UserModel()

if(session):
user = UserModel()

if request.method == "POST":
user.register(request.form['email'], request.form['password'], request.form['nick'], request.form['role'])

return render_template("admin/add-user.html", data=session['auth'])
return redirect("/admin/login")
if request.method == "POST":
user.register(request.form['email'], request.form['password'], request.form['nick'], request.form['role'])

return render_template("admin/add-user.html")


3 changes: 2 additions & 1 deletion controllers/fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ def snap(c):
def utility_processor():
def getnick(id):
u = UserModel()
return u.getUser(id)['Nick']
user = u.getUser(id)
return user.Nick
return dict(getnick=getnick)
Loading

0 comments on commit ab2894e

Please sign in to comment.