Ë
Yi Qing Sim By Yi Qing Sim • July 26, 2017

What I Made at the Hackathon: A Reusable Django App

Yi Qing Sim, DevOps Engineer at SYNQ

For the hackathon, I decided to create a reusable Django app. I used the example of a conference as a baseline for ideas, since many conferences these days record videos of the talks then go on to publish them and possibly also stream them. 

 

My synq_video pluggable app was an attempt to ease this process. In the `models.py` of the app itself, I defined a Project and Video model, mirroring the setup of the SYNQ.fm API.

One of Django’s killer features is its admin interface. It essentially provides a front end for administrators to enter data into the database, cleanly and with enforced constraints. This means that non-technical personnel can manage data without knowledge of how the database is structured. For instance, I define a foreign key constraint on the video table that references the project table. In the admin interface, this means that an administrator is only able to enter a new entry in the video table when it references an existing project.

Currently, the SYNQ video API does not allow for project creation. Thus, a project ID and api key must be created first in the SYNQ web interface and then added manually to the projects table in the synq_video Django admin interface. After this, rows to the video table can be added. Once a video is added, in the back end, a request is made to the SYNQ API to create the video object on SYNQ’s end, and the video ID in the response is saved to the synq_video database. This allows for synq_video to fetch, for example, video details.

With this scaffolding in place, I made it possible to upload a video file to SYNQ through the Django admin using a file upload widget. When the admin detects the upload of a file, it makes a /video/upload API call and uploads the video to SYNQ.

Finally, I made it possible to create a ManyToMany relationship to the Video model on an existing model in your Django app by way of a few helper functions. So all you have to do is include the following line in your model of choice:

videos = models.ManyToManyField(settings.SYNQ_VIDEO_M2M, blank=True)

 

You can also optionally set a `through` relationship that can add additional information to this ManyToMany relationship, for instance:

class Talk(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    speaker = models.ForeignKey(Speaker, on_delete=models.CASCADE)
    videos = models.ManyToManyField(settings.SYNQ_VIDEO_M2M, blank=True,
        through='TalkVideo')
 
 
    def __str__(self):
        return str(self.title)
 

 

An intermediary model is not required, but it has been included here to illustrate what you can do with it and what kinds of extra fields you may want to define on it. For example, the `position` field might be used to indicate which position this video should be in if it is one in an ordered sequence of videos that are associated with this particular talk. Similarly, the `type` field might be used to indicate what part of the # talk this video is of, e.g. the Q&A session

class TalkVideo(models.Model):
    talk = models.ForeignKey(Talk, on_delete=models.CASCADE)
    position = models.IntegerField()
    type = models.CharField(max_length=255, default='main', 
        choices=VIDEO_TYPES)
 

 

The front-end, I didn’t have time to complete too much, but I did manage to create a basic template for each video object with an embedded video.

That’s all I managed to complete this hackathon. If I had more time, I would have created html templates for each speaker with an array of their associated videos, and also created some example data for how the ‘through’ relationship might be properly taken advantage of - for example, an ‘order’ column, or a ‘type’ ChoiceField (e.g. it might be a ‘keynote’, ‘Q&A session’, or ‘lightning talk’).


Want to have your own hackathon? Try our API for free!

GET YOUR FREE API KEY


 

Related posts: