Over the weekend, I got to attend Triangle Code and Coffee! It was pretty exciting and I’m glad that I went. You always hear about how great it is to attend events that have a variety of people with a diversity of backgrounds and ideas. And it’s like, yeah, of course it would be pretty fantastic. However, actually being there for it certainly strengthens that perspective. It’s kind of annoying how often cliches are actually true.
If I were to give advice for anyone attending a networking event, go early. You’ll meet other fellow go-early-ers and by the time the event starts you’ll have people you can reliably approach. It makes meshing with groups a lot easier, especially if your go-early-er group is a decent size. That’s all I have for advice though; I’m still a programmer and you know how cliches go.
“That’s great and all,” you say while pointing at your watch, “but where’s that Python Django progress you were talking about last week? Don’t leave us hanging.” And to that I would say, oh yeah, you’re right just a sec.
shuffles metaphorical papers
The Django stuff
So this week I managed to make some considerable progress on my Python Django application. Admittedly, the size of the things I want to do increased in size faster than I could make the things. I understand if that comes as a surprise.
Last time I mentioned that I had 7 things I accomplished and 5 things I wanted to do. Now, I have…
- 11 things I accomplished
- 5 things I want to do as part of completing this project
- An extra thing I decided to do in the middle of a programming frenzy
- 5 things I’d like to do but I won’t strictly consider acceptance criteria for the purposes of finishing this within the current decade
To keep it simple, I will talk about the new and most interesting things I accomplished.
Following users and only seeing their posts
Given that the concept of following people is a many-to-many relationship, there was some database-related logic that needed to happen. Simply put, it was this:
Anybody with any amount of database experience will recognize this pattern, which is called a join table. These “following” entries can be created and deleted on the fly without affecting the users.
For those who are interested in the Python/Django representation of this, it is the following:
class User(AbstractUser):
date_joined = models.DateField(auto_now_add=True)
class UserFollowing(models.Model):
follower = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='following',
on_delete=models.CASCADE)
following = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='followers',
on_delete=models.CASCADE)
For those who may have noticed, User
extends from Django’s AbstractUser
class which has all the attributes you’d expect from a user. The attributes that you do not see (but definitely exist) are username, password, and email. For my particular use case, I added a date_joined
attribute that allows you to see when the User
was created.
The ForeignKey
parts in UserFollowing
essentially say that we are going to be pointing to a user for each of them. There are 2 entries for it, one for a follower and one for is being followed. Due to how Django allows you to access database info, any User
object is capable of accessing their followers and who they follow by using the names described with the related_name
parameters. A good example of this is from a function to generate posts based on a user’s following. This also includes the user’s own posts because it’s nice to be able to see your own posts too:
def aggregrate_tailored_posts(request):
follow_relationships = request.user.following.all()
posts = request.user.post_set.all()
for relationship in follow_relationships:
posts = posts.union(relationship.following.post_set.all())
return posts.order_by('-date')
The posts returned from this function will then be passed to the HTML template represented by the feed. Then, the user will see their own stuff they posted, as well as the posts from people they’re following.
Improving site look with bootstrap
I put this off for a while because I knew that my creative eye can vary from “vaguely resembling insightful” to “trash can incarnate.” For a better idea, take a look at my pixel art section!
Self-deprecation aside, I was able to format the site in such a way that it looked like something a human could use. This not only applied to desktop, but also mobile.
In order to do this, I leveraged the different formatting classes that bootstrap has which allow for adjustments based on different screen sizes. As advice for those that are curious about Bootstrap, read into how containers, rows, columns, and how the 12-column system works. This will provide a solid foundation for picking up the rest of the things bootstrap has to offer.
Seeing a user’s previous posts in their profile (the extra thing I decided to do)
This next feature is very similar to the feed. The main difference is that the posts listed are all posts made by the user. There is currently a restriction where you can’t comment on posts when you’re looking at them from the user page. I may implement the ability to comment while on a user page but it’s not entirely high on my list of things to do.
Much like the feed, this is a mobile-compatible interface. If it sees that the user is on a mobile device, it will actually move the user’s card to the top of the page instead of having it on the left side like the image above.
Testing
Since my focus has been more on picking up the framework, I haven’t written as many tests as I wanted to. That said, I have a working test in Selenium which will login as a user and check that the main page is available.
I am currently running into an issue where I want to have a consistent starting database that would reset back to its original configuration after selenium tests are done. A little bit of work will need to be done there in order to not have to manually reset stuff before running the Selenium tests.
I currently have a Gitlab server set up on my local network, so I will be working towards trying to dockerize this project and set up ways to run the selenium tests in a pipeline. That will be the best time to address the aforementioned problem, as I can have everything running in neat little containers with their own scripts and things. I also need to look into the Gunicorn utility for hosting the site that will be tested against.
And that’s about it
I didn’t cover literally everything that I made in the past week since that’d be a pretty nasty-sized post, but I did want to get the important parts out at the very least. It’s been a pretty neat project so far so I’m hoping I can try and get at least the core parts completed.
‘Till next week!