From 971cf1fa74963033c86260c80a455862ff425fa1 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Fri, 7 Aug 2009 14:41:17 -0500 Subject: [PATCH] Added model validation. --- tweepy/models.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tweepy/models.py b/tweepy/models.py index d3e90ef..b1b28e3 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -2,6 +2,8 @@ # Copyright 2009 Joshua Roesslein # See LICENSE +from error import TweepError + class Model(object): def __getstate__(self): @@ -12,13 +14,48 @@ class Model(object): pickle[k] = v return pickle + @staticmethod + def _validate(model, attributes): + missing = [] + for attr in attributes: + if not hasattr(model, attr): + missing.append(attr) + if len(missing) > 0: + raise TweepError('Missing required attribute(s) %s' % str(missing).strip('[]')) + class Status(Model): + @staticmethod + def _validate(status): + Model.validate(status, [ + 'created_at', 'id', 'text', 'source', 'truncated', 'in_reply_to_status_id', + 'in_reply_to_user_id', 'favorited', 'in_reply_to_screen_name' + ]) + if hasattr(status, 'user'): + User._validate(status.user) + def validate(self): + Status._validate(self) + def destroy(self): return self._api.destroy_status(id=self.id) class User(Model): + @staticmethod + def _validate(user): + Model.validate(user, [ + 'id', 'name', 'screen_name', 'location', 'description', 'profile_image_url', + 'url', 'protected', 'followers_count', 'profile_background_color', + 'profile_text_color', 'profile_sidebar_fill_color', 'profile_sidebar_border_color', + 'friends_count', 'created_at', 'favourites_count', 'utc_offset', 'time_zone', + 'profile_background_image_url', 'statuses_count', 'notifications', 'following', + 'verified' + ]) + if hasattr(user, 'status'): + Status._validate(user.status) + def validate(self): + User._validate(self) + def timeline(self, **kargs): return self._api.user_timeline(**kargs) def mentions(self, **kargs): -- 2.25.1