# Path: weckan/model.py
# DB = meta.Session
# class Package(CkanPackage):
# class Tag(CkanTag):
# class User(CkanUser):
#     def get_url(self, ctx, *path, **query):
#     def display_name(self):
#     def active_resources(self):
#     def slug_to_name(cls, value, state = None):
#     def organizations(self):


from weckan.model import User, Member, Role, Group, Package, meta
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Weckan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


DB = meta.Session


def get_user_from_request(request):
    '''Simple user fetching from request'''
    username = request.environ.get('repoze.who.identity', {}).get('repoze.who.userid')
    return User.get(username) if username else None


def can_edit_dataset(user, dataset):
    '''Returns True if a given user can edit a given dataset'''
    if user is None:
        return False
    if user.sysadmin or dataset.owner_org is None:
        return True

    query = DB.query(Member).filter(

# Path: weckan/model.py
# DB = meta.Session
# class Package(CkanPackage):
# class Tag(CkanTag):
# class User(CkanUser):
#     def get_url(self, ctx, *path, **query):
#     def display_name(self):
#     def active_resources(self):
#     def slug_to_name(cls, value, state = None):
#     def organizations(self):


# Path: weckan/auth.py
from weckan.model import User, Member, Role, Group, Package, meta

# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Weckan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


DB = meta.Session


def get_user_from_request(request):
    '''Simple user fetching from request'''
    username = request.environ.get('repoze.who.identity', {}).get('repoze.who.userid')
    return User.get(username) if username else None


def can_edit_dataset(user, dataset):
    '''Returns True if a given user can edit a given dataset'''
    if user is None:
        return False
    if user.sysadmin or dataset.owner_org is None:
        return True

    query = DB.query(Member).filter(

        Member.capacity.in_(['admin', 'editor']),