from flask import render_template, flash, redirect, session, url_for, request, g, abort
from flask_login import login_user, logout_user, current_user, login_required
-from app import app, lm, tools
+from app import app, lm, tools, db
from .forms import LoginForm, RegisterForm, UpdateForm, PwForm, SearchForm, DeleteForm, PortfolioForm, AlbumForm
from .models import User, Portfolio, Album, AccessRight, Role, MyAnonymous
from werkzeug.security import generate_password_hash, check_password_hash
@lm.user_loader
def load_user(id):
- return User.query.get(int(id))
+ return User.query(id=id)[0]
@app.route('/')
@app.route('/index')
return redirect(request.referrer)#redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
- user = User.query.filter_by(username=form.username.data).first()
+ user = User.query(username=form.username.data)[0]
if user is None:
flash('Unknown username. Please try again or register.')
else:
- app.logger.info("db-pwd: %s form-pwd: %s" % (user.password, form.password.data))
if check_password_hash(user.password, form.password.data):
app.logger.info("Login: %s" % user)
login_user(user, remember=False)
+ g.user = user
+ user.addObserver(db)
next = request.args.get('next')
if next is not None:
if not tools.is_safe_url(next):
query = tools.DBQuery()
portfolios = query.get_portfolios(u)
new_album = Album(name=form.name.data, public=form.public.data, visible=form.visible.data, portfolio_id=portfolios[0].id)
- db.session.add(new_album)
- db.session.commit()
+ db.store(new_album)
app.logger.warning("Created album: %s" % new_album)
except DBException as ex:
- db.session.rollback()
flash(ex)
- except exc.IntegrityError as ex:
- db.session.rollback()
- flash('Create album failed: %s' % ex)
except Exception as ex:
- db.session.rollback()
- flash("Unknown error {0}".format(ex))
+ flash('Create album failed: %s' % ex)
+# except Exception as ex:
+# flash("Unknown error {0}".format(ex))
return redirect(request.referrer)
return render_template('album.html',
title='Create Album',
uname=form.username.data
email=form.email.data
name=form.name.data
- user = User.query.filter_by(username=username).first()
+ user = User.query(username=username)[0]
if uname != username:
- u = User.query.filter_by(username=uname).first()
+ u = User.query(username=uname)[0]
if u is not None:
raise DBException("%s: Username exist" % uname)
user.username = uname
referrer = "/user/%s" % user.username
if email != user.email:
- e = User.query.filter_by(email=email).first()
+ e = User.query(email=email)[0]
if e is not None:
raise DBException("%s: Email exist" % email)
user.email = email
if name != user.name:
user.name = name
except DBException as ex:
- db.session.rollback()
+ user.rollback()
flash("{0}".format(ex))
app.logger.warning("Update user failed: {0}".format(ex))
- except exc.IntegrityError as ex:
- db.session.rollback()
+ except Exception as ex:
+ user.rollback()
flash("Update user failed: {0}".format(ex))
app.logger.warning("Update user failed: {0}".format(ex))
- except Exception as ex:
- db.session.rollback()
- flash("Unknown error {0}".format(ex))
- app.logger.warning("Update user failed: Unknown error {0}".format(ex))
+# except Exception as ex:
+# db.rollback()
+# flash("Unknown error {0}".format(ex))
+# app.logger.warning("Update user failed: Unknown error {0}".format(ex))
else:
try:
- db.session.commit()
+ user.commit()
login_user(user, remember=False)
app.logger.warning("Updated user: %s" % user)
flash("Userdata successfully updated")
- except exc.IntegrityError as ex:
- db.session.rollback()
- flash("Update user failed: {0}".format(ex))
except Exception as ex:
- db.session.rollback()
- flash("Unknown error {0}".format(ex))
+ flash("Update user failed: {0}".format(ex))
+# except Exception as ex:
+# flash("Unknown error {0}".format(ex))
return redirect(referrer)
elif pwform.pwchange.data and pwform.validate_on_submit():
- user = User.query.filter_by(username=username).first()
+ user = User.query(username=username)[0]
if pwform.password.data == pwform.passwordchk.data and check_password_hash(user.password, pwform.passwordcur.data):
hashed_password = generate_password_hash(pwform.password.data, method='sha256')
user.password = hashed_password
try:
- db.session.commit()
+ user.commit()
login_user(user, remember=False)
app.logger.warning("Updated user - password: %s" % user)
flash("Password successfully changed")
- except exc.IntegrityError as ex:
- db.session.rollback()
- flash("Update user failed: {0}".format(ex))
except Exception as ex:
- db.session.rollback()
- flash("Unknown error {0}".format(ex))
+ flash("Update user failed: {0}".format(ex))
+# except Exception as ex:
+# flash("Unknown error {0}".format(ex))
else:
flash('Current password does not match or password different from password check')
return redirect(referrer)
else:
- user = User.query.filter_by(username=username).first()
+ user = User.query(username=username)[0]
if user is None:
flash('User %s not found.' % username)
return redirect(url_for('index'))
app.logger.info("Show profile: %s" % user)
query = tools.DBQuery()
portfolios = query.get_portfolios(user)
+ app.logger.info("Portfolios: {0}".format(portfolios))
private = []
for p in portfolios:
albums = query.get_albums(p)
- p.set_user_count(len(query.get_users(p)))
+ #p.set_user_count(len(query.get_users(p)))
+ p.set_user_count(1)
a1 = []
for a in albums:
a.set_user_count(len(query.get_users(a)))
hashed_password = generate_password_hash(form.password.data, method='sha256')
new_user = User(name=form.name.data, username=form.username.data, email=form.email.data, password=hashed_password)
except DBException as ex:
- db.session.rollback()
flash(ex)
- except exc.IntegrityError as ex:
- db.session.rollback()
- flash('Create user failed: %s' % ex)
except Exception as ex:
- db.session.rollback()
- flash("Unknown error {0}".format(ex))
+ flash('Create user failed: %s' % ex)
+# except Exception as ex:
+# flash("Unknown error {0}".format(ex))
else:
try:
portfolio = Portfolio(name = new_user.name, owner = new_user)
- db.session.add(portfolio)
+ #db.session.add(portfolio)
new_user.portfolios.append(portfolio)
- db.session.add(new_user)
+ #db.session.add(new_user)
acl = AccessRight(right = Role.read, user = new_user)
- db.session.add(acl)
+ #db.session.add(acl)
acl = AccessRight(right = Role.write, user = new_user)
- db.session.add(acl)
+ #db.session.add(acl)
acl = AccessRight(right = Role.admin, user = new_user)
- db.session.add(acl)
- db.session.commit()
+ #db.session.add(acl)
+ #db.session.commit()
app.logger.warning("Registered: %s" % new_user)
flash("You have been registered with username: " + form.username.data + os.linesep)
flash("Default Portfolio: " + portfolio.name)
return redirect(url_for('login'))
- except exc.IntegrityError as ex:
- db.session.rollback()
+ except Exception as ex:
flash('Create user failed: %s' % ex)
except Exception as ex:
- db.session.rollback()
flash("Unknown error {0}".format(ex))
else:
flash('Password did not match password check')