Some work on `models.Aspect.getUsers()` done
authorMarek Marecki <triviuss@gmail.com>
Sun, 30 Jun 2013 22:55:23 +0000 (00:55 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 30 Jun 2013 22:55:23 +0000 (00:55 +0200)
diaspy/connection.py
diaspy/models.py

index c2d185b0f344fbd3d1abd0b25fd8a4263ff48bd6..9ce40570e69f628b7fb3a8fc395ed76d40d79592 100644 (file)
@@ -38,7 +38,10 @@ class Connection():
         """
         self.pod = pod
         self.session = requests.Session()
-        self._setlogin(username, password)
+        try:
+            self._setlogin(username, password)
+        except Exception as e:
+            raise LoginError('cannot create login data (caused by: {0}'.format(e))
 
     def get(self, string, headers={}, params={}):
         """This method gets data from session.
index 72041a52416ae272ca02b228cd6323b5c975962a..555715b4e96b8c8c98e1549ebee741cb39092f03 100644 (file)
@@ -21,7 +21,27 @@ class Aspect():
     def getUsers(self):
         """Returns list of users who are listed in this aspect.
         """
-        return []
+        start_regexp = re.compile('<ul +class=["\']contacts["\'] *>')
+        userline_regexp = re.compile('<a href=["\']/people/[a-z0-9]{16,16}["\']>[A-Za-z0-9 _-]+</a>')
+        userline_regexp = re.compile('<a href=["\']/people/[a-z0-9]{16,16}["\']>[a-zA-Z0-9 _-]+</a>')
+        ajax = self._connection.get('aspects/{0}/edit'.format(self.id)).text
+        begin = ajax.find(start_regexp.search(ajax).group(0))
+        end = ajax.find('</ul>')
+        ajax = ajax[begin:end]
+        all_users = userline_regexp.findall(ajax)
+        usernames = [line[35:-4] for line in all_users]
+        personid_regexp = 'alt="{USERNAME}" class="avatar" data-person_id="[0-9]+"'
+        person_ids = [re.compile(personid_regexp.replace('{USERNAME}', name)).search(ajax).group(0) for name in usernames]
+        personids = []
+        for n, line in enumerate(person_ids):
+            i = -2
+            id = ''
+            while line[i].isdigit():
+                id += line[i]
+                i -= 1
+            personids.append((usernames[n], id))
+        users = personids
+        return users
 
     def addUser(self, user_id):
         """Add user to current aspect.