views:

32

answers:

1

Hey, I'm making a popup radio player with an attached shoutbox with django and i'm having trouble getting the admin functionality I want.

I have two models:

1) A Stream (which represents a certain radio stream that the admin can publish to play on the frontpage - i.e. there are multiple saved streams, but only one is playing on the frontpage at a time, at the discretion of the admin)

2) A Shout (a shout that has been entered into the shoutbox, and is associated to a certain stream, i.e. every stream has multiple shouts that are entered by users of the site.)

I want the admin to be able to log into the back end, create multiple streams, but only select one to be published at any one time. Presumabley each stream should have an attribute (i.e is_published) and I should create an admin action to perform a check of every stream, and publish only the correct one? Is this the correct way to go about it or am I missing something

+1  A: 

The only potential problem I forsee is, what if someone has already connected and is listening to a stream before the admin changes it. Should that person hear the new stream or continue to hear the stream they were listening to?

Other than that, the way you've described it, I could see it working. You could make a url/view which always returns the current stream, like /stream/current/. The view for that URL will always get the most current Stream model...

def current_stream(request, *args, **kwargs):
   # Get first stream marked as published
   s = Stream.objects.filter(is_published=True)[1][0]
   return do_streaming_stuff(s)

Because you're going to probably use the "set this stream as active stream" elsewhere in your app, you could make it a part of your Stream model...

class Stream(models.Model):
    is_published = models.BooleanField()

    def set_as_active_stream(self, do_save=True):
        enabled_streams = Stream.objects.filter(is_published=True)
        for s in enabled_streams:
            s.is_published=False
            s.save()    
        if do_save:
            self.is_published=True
            self.save()

    def save(self, *args, **kwargs):
        if self.is_published:
            # No need to double save, since we're already saving it
            self.set_as_active_stream(do_save=False)
        super(Stream, self).save(*args, **kwargs)
T. Stone
That sounds exactly like what I want to do. Brilliant! The website will only ever have one stream live at any one time. Also, each stream is going to have a bunch of mp3 recordings that the user can switch to. I may add functionality to manually switch between streams.I want the admins to be able to publish a certain stream to be published to the site from the admin area. Would I be correct in saying I need to use admin actions to achieve this?
pastylegs
I'm still getting my head around the MVC pattern and the separation of database actions from logic. I notice that the model has functions that operate on itself. Should these not be in views instead of models?
pastylegs
@pasty Django is actually MVT, slightly different than MVC. It's probably somewhat preferential, but I lean towards thick models, thin views. I find there's less code duplication this way. I'm sure others can make a good argument for thin models, thick views.
T. Stone