def __new__(cls, name, bases, attrs): # Returns the generated Configurator subclass. attrs['_config_name'] = name config_fields = {} for key,object in attrs.items(): if isinstance(object, forms.Field): config_fields[key] = object del attrs[key] attrs['_config_fields'] = config_fields config_cls = type(name + "Form", (forms.Form,), config_fields.copy()) attrs['_config_cls'] = config_cls new_class = super(ConfiguratorDeclarativeParser,cls).__new__(cls, name, bases, attrs) # For each field, set the default value if it hasn't already been set. initial = config_cls({}) for key,field in initial.fields.items(): full_key = 'CFG__' + name + '__' + key if not full_key in conf: try: conf[full_key] = field.clean(field.default) logger.log_debug("set initial value for %s.%s to '%s'" % (name, key, field.default)) except forms.ValidationError: e = ConfiguratorException("'%s' is not a valid value for %s.%s" % (field.default, name, key)) logger.log_warning("failed to set initial value: %s" % e) raise e return new_class
def list_playlists(request): if request.method == 'GET': pl_list = Playlist.objects.all().order_by('name') return render_to_response('templates/playlist-byname.t', {'pl_list': pl_list}) logger.log_debug("list_playlists: POST=%s" % request.POST) json='' if request.POST['action'] == 'list': playlists = [] for pl in Playlist.objects.all().order_by('name'): playlists.append({'id': pl.id, 'title': pl.name, 'len': len(pl)}) json = simplejson.dumps({'status': 200, 'playlists': playlists}) elif request.POST['action'] == 'create': playlist = Playlist(name=request.POST['title']) playlist.save() json = simplejson.dumps({'status': 200, 'title': playlist.name, 'id': playlist.id, 'len': len(playlist)}) elif request.POST['action'] == 'rename': playlist = Playlist.objects.get(id=request.POST['id']) playlist.name = request.POST['title'] playlist.save() json = simplejson.dumps({'status': 200, 'id': request.POST['id']}) elif request.POST['action'] == 'delete': playlist = Playlist.objects.get(id=request.POST['id']) playlist.delete() json = simplejson.dumps({'status': 200}) return HttpResponse(json, mimetype="application/json")
def __setattr__(cls, name, value): # if 'name' is a setting, then set its value in the configuration # store. Otherwise try to set the class attribute to 'value'. try: field = cls._config_fields[name] conf['CFG__' + cls._config_name + '__' + name] = field.clean(value) logger.log_debug("%s: %s => %s" % (cls._config_name, name, value)) except: type.__setattr__(cls, name, value)
def render(self, request): try: song = Song.objects.filter(id=self.songid)[0] f = open(song.file.path, 'rb') mimetype = str(song.file.mimetype) logger.log_debug("%s -> %s (%s)" % (request.path, song.file.path, mimetype)) mimetype = MimeType.fromString(mimetype) return Response(200, {'content-type': mimetype}, FileStream(f)) except IndexError: return Response(404) except: return Response(500)
def __call__(self, settings=None): # Return an instance of the Configurator form. current = QueryDict('').copy() for key in self._config_fields.keys(): current[key] = conf['CFG__' + self._config_name + '__' + key] if settings: for key,value in settings.items(): current[key] = value config = self._config_cls(current) if settings and config.is_valid(): for key in config.fields.keys(): value = config.cleaned_data[key] full_key = 'CFG__' + self._config_name + '__' + key if conf[full_key] != value: logger.log_debug("%s: %s => %s" % (self._config_name, key, value)) conf[full_key] = value return config
def playlist_show(request, playlist_id): if request.method == 'GET': playlist = get_object_or_404(Playlist, id=playlist_id) song_list = playlist.list_songs() return render_to_response('templates/playlist-songs.t', { 'playlist': playlist, 'song_list': song_list } ) try: logger.log_debug("playlist_show: POST=%s" % request.POST) try: playlist = Playlist.objects.get(id=playlist_id) except: raise ApiResponse(404, reason="No such playlist") if request.POST['action'] == 'edit': title = request.POST['title'] if title == '': raise ApiResponse(400, reason="Title is empty") if len(title) > 80: raise ApiResponse(400, reason="Title is longer than 80 characters") try: playlist.name = title playlist.save() except Exception, e: logger.log_debug("playlist_show: edit failed: %s" % str(e)) raise ApiResponse(400, reason=str(e)) raise ApiResponse(200) elif request.POST['action'] == 'add': for id in request.POST.getlist('ids'): song = Song.objects.get(id=int(id)) playlist.append_song(song) logger.log_debug("playlist_show: added song %s" % song.name) playlist.save() raise ApiResponse(200)
def music_byalbum(request, album_id): album = get_object_or_404(Album, id=album_id) song_list = album.song_set.all().order_by('track_number') playlists = Playlist.objects.all() if request.method == 'POST': if request.POST['is-playlist'] == 'false': editor = AlbumEditorForm(request.POST, instance=album) if editor.is_valid(): editor.save() else: playlist = Playlist.objects.get(id=request.POST['selection']) for id,value in request.POST.items(): if value == 'selected': song = Song.objects.get(id=int(id)) logger.log_debug("adding '%s' to playlist '%s'" % (song, playlist)) playlist.append_song(song) editor = AlbumEditorForm(instance=album) else: editor = AlbumEditorForm(instance=album) return render_to_response('templates/music-album.t', { 'album': album, 'song_list': song_list, 'editor': editor, 'playlists': playlists } )