Hey there reader !
Its been quite some time since I updated about brainstormings going on for the new widgets for the hubs pages.
So, I was working on an issue for the Subscriber/Subscribe widget, where I had to list the number of subscribers that were subscribed to this user(his/her hub page), and also the number of users he/she was subscribed to, basically the subscription stats.
pretty simple, eh? That’s what I thought too 😛
Well PR #111 fixes it !
I learned a lot while trying to figure out ways to solve this issue, jotting them down :
- how easily one can access the attributes if we know the relation between two objects/tables.
- using dir() function to see beyond an alphanumeric object !
- write test-cases.
Structure of HUBS :
User : A user table with attributes like openid, fullname, and created_on.
Hub: This is the individual user’s hub page(in simple terms-profile page) with attributes like name, created_on, widgets, etc
So, we wanted to implement a many-to-many relationship between users and hubs, ‘coz many users can subscribe themselves to many hubs.
So, I spent quite some time with the flask-sqlalchemy tutorial to learn the pythonic implementation of the many-to-many relationship. 😛 my bad !!
Its really amazing to read code written by awesome developers. Ralph‘s code already had the ‘Association’ table to connect the users and hubs.
>>> for assoc in widget.hub.associations:
........sub_list = [u.name for u in assoc.user.subscriptions]
>>> subscribers = [u.username for u in widget.hub.subscribers]
‘widget.hub.associations’ returned a list of associated user objects.
This is where dir() function came to my rescue 😛
So I tried to print the attributes of the user objects that were being returned by simply printing the output of the same.
>>> print dir(widget.hub.associations)
Thus, displaying a list of the attributes of the object.
Hub names were extracted, which helped extract the subscriptions of the respective hub_user’s page.
Rest, was a simple edit in the jinja template, that made the Subscribe/Unsubscribe button do its magic 😉
But, that was not the end !
I had tests failing ! 😦
I didn’t have any experience with writing tests.
Why were tests failing ?
‘coz the default values of the subscribers was still assigned ‘0’, when I had changed it into a list. Changing its default value to ‘’ and voila !
It works !
I came across an awesome quote when bugs overpower you 😛
Your Goals don’t care if its the weekend !