blob: a780d0e6c5a2275672d7be22e34bbdf942eff154 [file] [log] [blame]
Joe Gregorio845a5452010-09-08 13:50:34 -04001# Copyright (C) 2010 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Utilities for Google App Engine
16
17Utilities for making it easier to use the
18Google API Client for Python on Google App Engine.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23import pickle
24
25from google.appengine.ext import db
26from apiclient.oauth import OAuthCredentials
27from apiclient.oauth import FlowThreeLegged
28
29
30class FlowThreeLeggedProperty(db.Property):
31 """Utility property that allows easy
32 storage and retreival of an
33 apiclient.oauth.FlowThreeLegged"""
34
35 # Tell what the user type is.
36 data_type = FlowThreeLegged
37
38 # For writing to datastore.
39 def get_value_for_datastore(self, model_instance):
40 flow = super(FlowThreeLeggedProperty,
41 self).get_value_for_datastore(model_instance)
42 return db.Blob(pickle.dumps(flow))
43
44 # For reading from datastore.
45 def make_value_from_datastore(self, value):
46 if value is None:
47 return None
48 return pickle.loads(value)
49
50 def validate(self, value):
51 if value is not None and not isinstance(value, FlowThreeLegged):
52 raise BadValueError('Property %s must be convertible '
53 'to a FlowThreeLegged instance (%s)' %
54 (self.name, value))
55 return super(FlowThreeLeggedProperty, self).validate(value)
56
57 def empty(self, value):
58 return not value
59
60
61class OAuthCredentialsProperty(db.Property):
62 """Utility property that allows easy
63 storage and retrieval of
64 apiclient.oath.OAuthCredentials
65 """
66
67 # Tell what the user type is.
68 data_type = OAuthCredentials
69
70 # For writing to datastore.
71 def get_value_for_datastore(self, model_instance):
72 cred = super(OAuthCredentialsProperty,
73 self).get_value_for_datastore(model_instance)
74 return db.Blob(pickle.dumps(cred))
75
76 # For reading from datastore.
77 def make_value_from_datastore(self, value):
78 if value is None:
79 return None
80 return pickle.loads(value)
81
82 def validate(self, value):
83 if value is not None and not isinstance(value, OAuthCredentials):
84 raise BadValueError('Property %s must be convertible '
85 'to an OAuthCredentials instance (%s)' %
86 (self.name, value))
87 return super(OAuthCredentialsProperty, self).validate(value)
88
89 def empty(self, value):
90 return not value