Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sqlalchemy_utils/types/url.py: 62%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

29 statements  

1from sqlalchemy import types 

2 

3from .scalar_coercible import ScalarCoercible 

4 

5furl = None 

6try: 

7 from furl import furl 

8except ImportError: 

9 pass 

10 

11 

12class URLType(ScalarCoercible, types.TypeDecorator): 

13 """ 

14 URLType stores furl_ objects into database. 

15 

16 .. _furl: https://github.com/gruns/furl 

17 

18 :: 

19 

20 from sqlalchemy_utils import URLType 

21 from furl import furl 

22 

23 

24 class User(Base): 

25 __tablename__ = 'user' 

26 

27 id = sa.Column(sa.Integer, primary_key=True) 

28 website = sa.Column(URLType) 

29 

30 

31 user = User(website='www.example.com') 

32 

33 # website is coerced to furl object, hence all nice furl operations 

34 # come available 

35 user.website.args['some_argument'] = '12' 

36 

37 print user.website 

38 # www.example.com?some_argument=12 

39 """ 

40 

41 impl = types.UnicodeText 

42 cache_ok = True 

43 

44 def process_bind_param(self, value, dialect): 

45 if furl is not None and isinstance(value, furl): 

46 return str(value) 

47 

48 if isinstance(value, str): 

49 return value 

50 

51 def process_result_value(self, value, dialect): 

52 if furl is None: 

53 return value 

54 

55 if value is not None: 

56 return furl(value) 

57 

58 def _coerce(self, value): 

59 if furl is None: 

60 return value 

61 

62 if value is not None and not isinstance(value, furl): 

63 return furl(value) 

64 return value 

65 

66 @property 

67 def python_type(self): 

68 return self.impl.type.python_type