From 79a0fc1952dbeceb3ca32557cfc3f6f1ebbc896f Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sat, 7 Sep 2013 21:17:17 +0200 Subject: [PATCH] Tags can be fetched from profile --- diaspy/settings.py | 74 +++++++++++++++++++++++++++++----------------- testconf.markdown | 24 ++++++++------- tests.py | 7 +++-- 3 files changed, 65 insertions(+), 40 deletions(-) diff --git a/diaspy/settings.py b/diaspy/settings.py index d98bea5..27cd904 100644 --- a/diaspy/settings.py +++ b/diaspy/settings.py @@ -41,34 +41,67 @@ class Profile(): 'profile[date][month]': '', 'profile[date][day]': '', } + self._html = self._fetchhtml() + self._loaded = False + + def _fetchhtml(self): + """Fetches html that will be used to extract data. + """ + return self._connection.get('profile/edit').text + + def load(self): + """Loads profile data into self.data dictionary. + **Notice:** Not all keys are loaded yet. + """ + self.data['profile[first_name]'], self.data['profile[last_name]'] = self.getName() + self.data['profile[bio]'] = self.getBio() + self.data['profile[location]'] = self.getLocation() + self.data['profile[gender]'] = self.getGender() + year, month, day = self.getBirthDate(named_month=False) + self.data['profile[date][]'] = year + self.data['profile[date][]'] = month + self.data['profile[date][]'] = day + self._loaded = True + + def update(self): + """Updates profile information. + """ + if not self._loaded: raise errors.DiaspyError('profile was not loaded') + data['authenticity_token'] = repr(self._connection) + request = self._connection.post('profile', data=json.dumps(data), allow_redirects=False) + return request.status_code def getName(self): """Returns two-tuple: (first, last) name. """ - html = self._connection.get('profile/edit').text - first = self.firstname_regexp.search(html).group(1) - last = self.lastname_regexp.search(html).group(1) + first = self.firstname_regexp.search(self._html).group(1) + last = self.lastname_regexp.search(self._html).group(1) return (first, last) + def getTags(self): + """Returns tags user had selected when describing him/her-self. + """ + guid = self._connection.getUserInfo()['guid'] + html = self._connection.get('people/{0}'.format(guid)).text + description_regexp = re.compile('#.*?') + return [tag.lower() for tag in re.findall(description_regexp, html)] + def getBio(self): """Returns user bio. """ - html = self._connection.get('profile/edit').text - bio = self.bio_regexp.search(html).group(1) + bio = self.bio_regexp.search(self._html).group(1) return bio def getLocation(self): """Returns location string. """ - html = self._connection.get('profile/edit').text - location = self.location_regexp.search(html).group(1) + location = self.location_regexp.search(self._html).group(1) return location def getGender(self): """Returns location string. """ - html = self._connection.get('profile/edit').text - gender = self.gender_regexp.search(html).group(1) + gender = self.gender_regexp.search(self._html).group(1) return gender def getBirthDate(self, named_month=False): @@ -77,11 +110,10 @@ class Profile(): :param named_month: if True, return name of the month instead of integer :type named_month: bool """ - html = self._connection.get('profile/edit').text - year = self.birth_year_regexp.search(html) + year = self.birth_year_regexp.search(self._html) if year is None: year = -1 else: year = int(year.group(1)) - month = self.birth_month_regexp.search(html) + month = self.birth_month_regexp.search(self._html) if month is None: if named_month: month = '' else: month = -1 @@ -90,7 +122,7 @@ class Profile(): month = month.group(2) else: month = int(month.group(1)) - day = self.birth_day_regexp.search(html) + day = self.birth_day_regexp.search(self._html) if day is None: day = -1 else: day = int(day.group(1)) return (year, month, day) @@ -98,8 +130,7 @@ class Profile(): def isSearchable(self): """Returns True if profile is searchable. """ - html = self._connection.get('profile/edit').text - searchable = self.is_searchable_regexp.search(html) + searchable = self.is_searchable_regexp.search(self._html) # this is because value="true" in every case so we just # check if the field is "checked" if searchable is None: searchable = False # if it isn't - the regexp just won't match @@ -109,22 +140,11 @@ class Profile(): def isNSFW(self): """Returns True if profile is marked as NSFW. """ - html = self._connection.get('profile/edit').text - nsfw = self.is_nsfw_regexp.search(html) + nsfw = self.is_nsfw_regexp.search(self._html) if nsfw is None: nsfw = False else: nsfw = True return nsfw - def setName(self, first='', last=''): - """Set first name. - """ - data = self.data - data['profile[first_name]'] = first - data['profile[last_name]'] = last - data['authenticity_token'] = repr(self._connection) - request = self._connection.post('profile', data=data, allow_redirects=False) - return request.status_code - class Account(): """This object is used to get access to user's settings on diff --git a/testconf.markdown b/testconf.markdown index b610612..5a51bd8 100644 --- a/testconf.markdown +++ b/testconf.markdown @@ -17,26 +17,28 @@ Template file: # Your login details - __pod__ = 'https://pod.orkz.net' - __username__ = 'marekjm' - __passwd__ = 'mAreKJmonDiASporA' + __pod__ = 'https://pod.example.com' + __username__ = 'user' + __passwd__ = 'password' # D* identifiers - diaspora_id = 'marekjm@pod.orkz.net' - guid = 'fd4ac447f2d267fa' + diaspora_id = 'user@pod.example.com' + guid = 'abcdef01234678' # Details of your account - diaspora_name = 'Marek Marecki' - user_names_tuple = ('Marek', 'Marecki') - user_location_string = 'Poland' - user_gender_string = 'Dude' - user_date_of_birth = (1995, 3, 22) + diaspora_name = 'Foo Bar' + user_names_tuple = ('Foo', 'Bar') + user_location_string = 'Nowhere' + user_gender_string = 'Gender' + user_date_of_birth = (2013, 9, 7) # remember about language you've set! - user_date_of_birth_named = (1995, 'March', 22) + user_date_of_birth_named = (2013, 'September', 7) user_is_searchable = True user_is_nsfw = False + # five tags you use to describe yourself (all in lowercase) + user_tags = ['movies', 'kittens', 'travel', 'teacher', 'newyork'] # both names are created diff --git a/tests.py b/tests.py index 9895aa6..b023bfc 100644 --- a/tests.py +++ b/tests.py @@ -224,7 +224,7 @@ class NotificationsTests(unittest.TestCase): class SettingsTests(unittest.TestCase): profile = diaspy.settings.Profile(test_connection) - settings = diaspy.settings.Account(test_connection) + account = diaspy.settings.Account(test_connection) def testGettingName(self): self.assertEqual(testconf.user_names_tuple, self.profile.getName()) @@ -245,8 +245,11 @@ class SettingsTests(unittest.TestCase): def testGettingInfoIfProfileIsNSFW(self): self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW()) + def testGettingTags(self): + self.assertEqual(sorted(testconf.user_tags), sorted(profile.getTags())) + def testGettingLanguages(self): - self.assertIn(('English', 'en'), self.settings.getLanguages()) + self.assertIn(('English', 'en'), self.account.getLanguages()) if __name__ == '__main__': -- 2.25.1