# -*- coding: utf-8 -*-
from flask import g
from flask.ext.lastuser.sqlalchemy import UserBase2, ProfileBase
from boxoffice.models import db, JsonDict


__all__ = ['User', 'Organization']


class User(UserBase2, db.Model):

    __tablename__ = 'user'

    def __repr__(self):
        return self.fullname

    @property
    def orgs(self):
        return Organization.query.filter(Organization.userid.in_(self.organizations_owned_ids())).all()


def default_user(context):
    return g.user.id if g.user else None


class Organization(ProfileBase, db.Model):
    __tablename__ = 'organization'
    __table_args__ = (db.UniqueConstraint('contact_email'),)

    details = db.Column(JsonDict, nullable=False, server_default='{}')
    contact_email = db.Column(db.Unicode(254), nullable=False)

    def permissions(self, user, inherited=None):
        perms = super(Organization, self).permissions(user, inherited)
        if self.userid in user.organizations_owned_ids():
            perms.update(['org_admin'])
        return perms

@@ -303,22 +303,8 @@
 me%0A%0A
-    @property%0A
     
@@ -420,14 +420,8 @@
 ()))
-.all()
 %0A%0A%0Ad
@@ -964,16 +964,12 @@
 rms.
-update(%5B
+add(
 'org
@@ -975,17 +975,16 @@
 g_admin'
-%5D
 )%0A      

add instead of update, return query instead of result