Beispiel #1
0
 def __importRating(self, user, key, rating, status, date):
     # Import ratings to database, check if previous ratings exists
     # and maintain a proper event history of everything
     
     # First, resolve the media
     url = self.getUrl(key)
     self.last_url = url #for debug
     try:
         sourceMedia = mediamanager.addSourceMedia(url)
     except MediaTypeNotSupportedError:
         #print "Unsupported media type for url: "+url
         return # Nothing to do yet
     
     #print "Source media " + key + " - " + sourceMedia.media.name
     
     # Then find existing ratings for our media type
     # (IMDB only supports the simple kind of rating)
     ratings = RatingEvent.objects.filter(user=user, 
                                          source=sourceMedia.source,
                                          media=sourceMedia.media,
                                          complex=False).order_by('-date','-id')
     
     addNewRating = True
     if len(ratings) > 0 and ratings[0].rating == rating:
         addNewRating = False # don't add new rating if previous rating already exists
     
     # Special rules for imdb: we ignore statuses (since IMDb doesn't have them)
     # and we only check if the rating changed. duration is ignored completely
     if addNewRating:
         re = RatingEvent()
         re.user = user
         re.media = sourceMedia.media
         re.source = sourceMedia.source
         re.rating = rating
         re.date = mediamanager.nowDateString()
         re.complex = False
         # Always use 'watched' for the status, as IMDB does not offer any choice
         re.status = Status.objects.get(type=sourceMedia.media.type, name="Watched")
         re.save()
         # Get mediamanager to update duration:
         mediamanager.updateStatus(re, re.status.id)
Beispiel #2
0
def ajaxUpdateRatingEvent(request, re_id, update_what, new_value, type_id=-1):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse("auth_login"))
    
    re = None
    media = None
    try:
        re = RatingEvent.objects.get(pk=re_id)
        media = re.media
    except RatingEvent.DoesNotExist:
        raise Http404
    
    if not re.user.id == request.user.id:
        print "Invalid user! re user: %s request user: %s" % (str(re.user.id), str(request.user.id))
        return HttpResponseForbidden("You are not allowed to access this page")

    wasDeleted = False
    error = None
    if update_what == 'status':
        if int(new_value) <= 0: #delete
            error = mediamanager.deleteRating(re)
            wasDeleted = True
        else:
            error = mediamanager.updateStatus(re, new_value)
    elif update_what == 'rating':
        error = mediamanager.updateRating(re, new_value)
    elif update_what == 'progress': # check that status allows updates!
        if not re.status.durationEditable():
            error = "Cannot edit duration"
        else:
            error = mediamanager.updateProgress(re, new_value, type_id)
    elif update_what == 'date':
        error = mediamanager.updateDate(re, new_value)
    else:
        error = "Invalid update item"
    
    # Prepare the response
    if wasDeleted:
        myData = mediamanager.toAjaxDict(None, media)
    elif update_what == 'date': #don't need all the heavy stuff
        myData = {'id':re.id, 'date':re.date.strftime("%Y-%m-%d")}
    else:
        myData = mediamanager.toAjaxDict(re, re.media, dict={}, type_id=type_id)
        
    if error:
        myData["error"] = error
    else:
        myData["message"] = update_what + " updated!"
    
    return HttpResponse(simplejson.dumps(myData), mimetype='application/json')