Tags can be fetched from profile
authorMarek Marecki <marekjm@taistelu.com>
Sat, 7 Sep 2013 19:17:17 +0000 (21:17 +0200)
committerMarek Marecki <marekjm@taistelu.com>
Sat, 7 Sep 2013 19:17:17 +0000 (21:17 +0200)
diaspy/settings.py
testconf.markdown
tests.py

index d98bea5561d5a0446d0a5969ee1449175d8035db..27cd904663deab1464500911f70316a1fd2c72ba 100644 (file)
@@ -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('<a href="/tags/(.*?)" class="tag">#.*?</a>')
+        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
index b610612f51b580dd6c403768648c2840cd3377e8..5a51bd871d37a0cdbe2f78c60b07650c8c85b499 100644 (file)
@@ -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
index 9895aa6ac6645beb4602271decee97c90c73d080..b023bfc2060d5752c8c7e03aa263a4315bb92a35 100644 (file)
--- 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__':