Source

ytmanager / mcyoutube / views / entry.py

Full commit
from django import http
from django.forms.models import model_to_dict
from django.shortcuts import get_object_or_404

from annoying.decorators import ajax_request

import gdata.service

from mcyoutube.youtube import models
from mcyoutube.decorators import is_auth

@ajax_request
@is_auth
def update(request):
    try:
        id = request.GET['id']
        title = request.GET['title']
        playlist_id = request.GET['playlist_id']
    except KeyError:
        return http.HttpResponseBadRequest()

    entry = get_object_or_404(models.PlaylistEntry, id=id)
    if not entry.is_mine():
        return http.HttpResponseForbidden()

    if entry.playlist_id != playlist_id:
        playlist = get_object_or_404(models.Playlist, id=playlist_id)

        entry.remote_delete_from_playlist()
        entry.delete()

        entry.playlist = playlist
        try:
            entry.remote_add_to_playlist()
        except gdata.service.RequestError, exc:
            if exc.args[0]['body'] == 'Favorite already exists.':
                entry.delete()
                return {'result': False, 'reason': 'Favorite already exists.'}

    entry.title = title
    entry.remote_update()
    entry.save()

    return dict(model_to_dict(entry), result=True)

@ajax_request
@is_auth
def delete(request):
    try:
        id = request.GET['id']
    except KeyError:
        return http.HttpResponseBadRequest()

    entry = get_object_or_404(models.PlaylistEntry, id=id)
    if not entry.is_mine():
        return http.HttpResponseForbidden()

    entry_dict = model_to_dict(entry)

    entry.remote_delete_from_playlist()
    entry.delete()

    return entry_dict