# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2013 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# 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.
#
# PyBossa 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 PyBossa.  If not, see <http://www.gnu.org/licenses/>.

from flask.ext.login import current_user
from pybossa.model.task_run import TaskRun
from werkzeug.exceptions import Forbidden


def create(taskrun=None):
    authorized = (TaskRun.query.filter_by(app_id=taskrun.app_id)
                    .filter_by(task_id=taskrun.task_id)
                    .filter_by(user=taskrun.user)
                    .filter_by(user_ip=taskrun.user_ip)
                    .first()) is None
    if not authorized:
        raise Forbidden
    return authorized


def read(taskrun=None):
    return True


def update(taskrun):
    return False


def delete(taskrun):
    if current_user.is_anonymous():
        return False
    if taskrun.user_id is None:
        return current_user.admin
    else:
        return current_user.admin or taskrun.user_id == current_user.id


@@ -830,44 +830,26 @@
 rom 
-werkzeug.exceptions import Forbidden
+flask import abort
 %0A%0A%0Ad
@@ -1179,17 +1179,18 @@
 ise 
-Forbidden
+abort(403)
 %0A   
@@ -1440,22 +1440,8 @@
 min%0A
-    else:%0A    
     

Raise abort (flask specific) rather than Forbidden (Werkzeug specific) exception