Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import Flask, render_template, request, g, session

from flask.ext.admin import Admin, BaseView, expose
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin.contrib.geoa import ModelView

from authomatic.providers import oauth2
from authomatic import Authomatic
Expand Down Expand Up @@ -48,9 +48,8 @@ def func():


def load_user():
if 'user_id' not in session.keys():
g.user = None
else:
g.user = None
if 'user_id' in session.keys():
g.user = User.query.filter_by(id=session['user_id']).first()


Expand All @@ -73,7 +72,7 @@ def create_app(config='config.ProductionDevelopmentConfig'):
app.register_blueprint(comments_bp, url_prefix='/comments')
app.register_blueprint(organizations_bp, url_prefix='/organizations')

admin = Admin(app)
admin = Admin(app, name='CIT-Admin')

# add admin views.
admin.add_view(AdminView(User, db.session))
Expand Down
2 changes: 1 addition & 1 deletion cit/auth/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def user_info():
def logout():
session.pop('authomatic:fb:state', None)
session.pop('user_id', None)
return jsonify({'status': 0})
return jsonify({}), 200


@auth_bp.route('/user/profile/', methods=['POST'])
Expand Down
6 changes: 3 additions & 3 deletions cit/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Spatial_ref_sys(db.Model):

class Vote(db.Model):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
vote = db.Column(db.Boolean)

# This is used to discriminate between the linked tables.
Expand All @@ -61,9 +61,9 @@ class Vote(db.Model):

target = generic_relationship(target_type, target_id)

author = db.relationship("User")
author = db.relationship('User')

def __init__(self, author="", vote="", target=""):
def __init__(self, author='', vote='', target=''):
self.author = author
self.vote = vote
self.target = target
25 changes: 15 additions & 10 deletions cit/comments/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from ..utils import login_required

from .models import Comment
from ..issues.models import Issue
from ..auth.models import User
from ..db import db

comments_bp = Blueprint('comments', __name__)
Expand All @@ -13,26 +11,33 @@
@login_required
def comment_add():
comment_id = 0
error = 400
if request.form['issue_id']:
comment = Comment(author=g.user, issue_id=request.form['issue_id'],
status = 400

if request.form['issue_id'] and request.form['msg']:
comment = Comment(author_id=g.user.id,
issue_id=request.form['issue_id'],
message=request.form['msg'])
db.session.add(comment)
db.session.commit()
comment_id = comment.id
error = 201
return jsonify({'id': comment_id}), error
status = 201
return jsonify({'id': comment_id}), status


@comments_bp.route('/comments/<int:comment_id>/', methods=['DELETE'])
@comments_bp.route('/<int:comment_id>/', methods=['DELETE'])
@login_required
def delete_comment(comment_id):
db.session.begin_nested()
comment_query = db.session.query(Comment)
comment_filtered = comment_query.filter(Comment.id == comment_id)
result = comment_filtered.delete(synchronize_session='fetch')
if result:
db.session.commit()
return jsonify({'status': 0})
status = 204
message = 'comment was successfully deleted'
else:
db.session.rollback()
return jsonify({'msg': 'comment not found', 'status': 1})
status = 404
message = 'comment not found'

return jsonify({'msg': message}), status
12 changes: 6 additions & 6 deletions cit/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
issue_id = db.Column(db.Integer, db.ForeignKey("issue.id"))
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'))
message = db.Column(db.String(400))

author = db.relationship("User")
issue = db.relationship("Issue")
author = db.relationship('User')
issue = db.relationship('Issue')

def __init__(self, author="", issue_id=None, message=""):
self.author = author
def __init__(self, author_id=None, issue_id=None, message=''):
self.author_id = author_id
self.issue_id = issue_id
self.message = message

Expand Down
61 changes: 38 additions & 23 deletions cit/organizations/controllers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from flask import redirect, render_template, request, make_response, g
from flask import Blueprint, session, jsonify
from urllib import quote
from flask import request, g
from flask import Blueprint, jsonify

from .models import Organization
from ..db import db
from ..utils import login_required, admin_required
import sqlalchemy.exc as sqlalchemy_exc
import sqlalchemy.orm.exc as sqlalchemy_orm_exc

organizations_bp = Blueprint('organizations', __name__)

Expand All @@ -27,27 +28,41 @@ def organization_create():

@organizations_bp.route('/', methods=['GET'])
def organizations_info():
organization_query = db.session.query(Organization)
organization_names = []
organization_query = db.session.query(Organization)
organizations_list = []
if organization_query:
for org in organization_query:
organization_dict = {}
organization_dict.update({
'id': org.id,
'name': org.name
})
organization_names.append(organization_dict)
return jsonify(organizations=organization_names)
else:
return jsonify({})


@organizations_bp.route('/organizations/<int:org_id>/add-user/', methods=['POST'])
organizations_list = \
[{'id': org.id, 'name': org.name} for org in organization_query]
return jsonify(organizations=organizations_list)


@organizations_bp.route('/<int:org_id>/add-user/',
methods=['POST'])
@login_required
def organization_user_add(org_id):
user = g.user
org = db.session.query(Organization).filter(Organization.id == org_id)
user.organizations.append(org.first())
db.session.add(user)
db.session.commit()
return jsonify({'status': 0}), 201
try:
db.session.begin_nested()
org = db.session.query(Organization).filter(Organization.id == org_id)
user.organizations.append(org.first())
db.session.add(user)
db.session.commit()
status = 201
message = 'user-organization relationship was established'
except sqlalchemy_exc.IntegrityError:
db.session.rollback()
status = 409
message = 'user-organization relationship was not established ' + \
'due to the IntegrityError transaction committing.\n' + \
'Please check whether the specific relationship is' + \
'already present in database.'
except sqlalchemy_orm_exc.FlushError:
db.session.rollback()
status = 404
message = 'user-organization relationship was not established ' + \
'due to the FlushError transaction committing.\n' + \
'Please check whether the specific organization is' + \
'present in database.'

return jsonify({'user_id': user.id, 'org_id': org_id,
'message': message}), status
3 changes: 1 addition & 2 deletions cit/organizations/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from flask import g
from geoalchemy2 import Geography
from ..db import db

Expand All @@ -8,7 +7,7 @@ class Organization(db.Model):
name = db.Column(db.String(120))
address = db.Column(Geography(geometry_type='GEOMETRY'))

def __init__(self, name="", address=""):
def __init__(self, name='', address=''):
self.name = name
self.address = address

Expand Down
7 changes: 4 additions & 3 deletions init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def populate_target(self, values):
mixer = MyOwnMixer()


class InitDB():
class InitDB:
def __init__(self, app):
self.app = app

Expand Down Expand Up @@ -51,9 +51,10 @@ def generate_test_data(self):
description=mixer.RANDOM,
coordinates='POINT(49.839357 24.028398)')
db.session.add(issue)
db.session.flush()
comment = mixer.blend(Comment,
author=user,
issue=issue,
author_id=user.id,
issue_id=issue.id,
message=mixer.RANDOM)
db.session.add(comment)
photo = mixer.blend(Photo,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-e git+git@github.com:peterhudec/authomatic.git@a3058fac78827d66f4d3a2e4e5739ee95cff0261#egg=Authomatic-master
Flask==0.10.1
Flask-Admin==1.1.0
Flask-Admin==1.2.0
Flask-Login==0.2.11
Flask-RESTful==0.3.2
Flask-SQLAlchemy==2.0
Expand Down
Loading