#!/usr/bin/env python
# Copyright (c) 2014 Eugene Frolov <eugene@frolov.net.ru>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from restalchemy.common import exceptions as exc
from restalchemy.dm import models
from restalchemy.dm import properties


def relationship(*args, **kwargs):
    for arg in args:
        if not issubclass(arg, models.Model):
            raise exc.RelationshipModelError(model=arg)
    kwargs['property_class'] = kwargs.get('property_class', Relationship)
    return properties.property(*args, **kwargs)


class BaseRelationship(properties.AbstractProperty):
    pass


class Relationship(BaseRelationship):

    def __init__(self, property_types, default=None, required=False,
                 read_only=False, value=None):
        self._types = property_types
        self._required = bool(required)
        self._read_only = bool(read_only)
        self._default = None
        if default:
            default = default() if callable(default) else default
            self._default = (default()) if callable(
                default) else self._safe_value(default)
        self._value = value

    def _safe_value(self, value):
        if value is None or isinstance(value, self._types):
            if value is None and self.is_required():
                raise exc.PropertyRequired()
            return value
        else:
            raise exc.TypeError(value=value, property_type=self._type)

    def is_read_only(self):
        return self._read_only

    def is_required(self):
        return self._required

    @property
    def value(self):
        return self._value or self._default

    @value.setter
    def value(self, value):
        if (self.is_read_only()):
            raise exc.ReadOnlyProperty()
        self._value = self._safe_value(value)

    def set_value_force(self, value):
        self._value = self._safe_value(value)

@@ -761,16 +761,31 @@
 ionship(
+property_type, 
 *args, *
@@ -790,24 +790,24 @@
  **kwargs):%0A
-
     for arg 
@@ -1022,16 +1022,45 @@
 roperty(
+property_type=property_type, 
 *args, *
@@ -1209,17 +1209,16 @@
 rty_type
-s
 , defaul
@@ -1306,17 +1306,16 @@
 lf._type
-s
  = prope
@@ -1322,17 +1322,16 @@
 rty_type
-s
 %0A       
@@ -1684,32 +1684,32 @@
 e(self, value):%0A
+
         if value
@@ -1748,17 +1748,16 @@
 lf._type
-s
 ):%0A     

Add property_type parameter to relationships.relationship