Esempio n. 1
0
def vote(request):
    results = {'result': 'Fail'}
    if request.method == 'GET':
        GET = request.GET
        if 'rate' in GET:
            #save request to database
            user = User.objects.get(id=int(GET['user_id']))
            movie = Movie.objects.get(id=int(GET['movie_id']))
            try:
                vote = Vote.objects.get(user=user, movie=movie)
                vote.rate = int(GET['rate'])
                vote.save()
            except:
                rate = int(GET['rate'])
                new_vote = Vote(user=user, movie=movie, rate=rate)
                new_vote.save()

            total = Vote.objects.filter(movie=movie).count()
            avg = Vote.objects.filter(movie=movie).aggregate(Avg('rate'))
            #prepare json response
            results = {'result': 'Successful', 'total': total, 'avg': avg['rate__avg']}

            #update feed
            f = Feed()
            f.update_feed(user, 6, None, movie)
    json = simplejson.dumps(results)
    return HttpResponse(json, mimetype='application/json')
Esempio n. 2
0
def review(request):
    results = {'result': 'Fail'}
    if request.method == 'GET':
        GET = request.GET
        if 'user_id' in GET:
            #save request to database
            user = User.objects.get(id=int(GET['user_id']))
            movie = Movie.objects.get(id=int(GET['movie_id']))

            #update feed
            f = Feed()
            f.update_feed(user, 5, None, movie)

            results = {'result': 'Successful'}
    json = simplejson.dumps(results)
    return HttpResponse(json, mimetype='application/json')
Esempio n. 3
0
def add_to_favlist(request):
    results = {'result': 'Fail'}
    if request.method == 'GET':
        GET = request.GET
        if GET.has_key('user_id'):
            user = User.objects.get(id=int(GET['user_id']))
            movie = Movie.objects.get(id=int(GET['movie_id']))
            favlist = FavList(user=user, movie=movie)
            favlist.save()
            results = {'result': 'Added successfully'}

            #update feed
            f = Feed()
            f.update_feed(user, 7, None, movie)

    json = simplejson.dumps(results)
    return HttpResponse(json, mimetype='application/json')
Esempio n. 4
0
def _save_movie(imdb_id, user):
    request_url = 'http://www.omdbapi.com/?i=%s' % imdb_id
    errors = []

    try:
        response = urllib2.urlopen(request_url)
    except urllib2.URLError as err:
        errors.append(err)
    if response:
        result = response.read()
        json_result = simplejson.loads(result)

        if json_result['Response'] != 'False':
            # process data
            title = json_result['Title']
            year = json_result['Year']

            # process release date
            release_str = json_result['Released']
            if len(release_str.split()) == 1:
                if release_str == 'N/A':
                    release_time = None
                else:
                    release_time = datetime(*time.strptime(release_str, "%Y")[:6])
            elif len(release_str.split()) == 2:
                release_time = datetime(*time.strptime(release_str, "%b %Y")[:6])
            elif len(release_str.split()) == 3:
                release_time = datetime(*time.strptime(release_str, "%d %b %Y")[:6])

            # fetch poster image to local disk
            poster_url = json_result['Poster']
            poster_file_name = ('/images/posters/%s_%s.jpg' %
                                (year, title.lower().replace(' ', '_')))
            poster_file_abs = settings.MEDIA_ROOT + poster_file_name
            poster_file_rel = '/media' + poster_file_name
            if poster_url == 'N/A':
                poster_file_rel = '/media/images/default_poster.png'
            else:
                try:
                    resp = urllib2.urlopen(poster_url)
                except urllib2.URLError as err:
                    print err
                if resp:
                    poster_file = open(poster_file_abs, 'w')
                    poster_file.write(resp.read())
                    poster_file.close()

            # check if movie exists in DB
            flag = Movie.objects.filter(title=title, year=year)
            if not flag:  # add movie info to DB
                new_movie = Movie(
                    title=title,
                    year=year,
                    genre=json_result['Genre'],
                    actors=json_result['Actors'],
                    plot=json_result['Plot'],
                    poster=poster_file_rel,
                    runtime=json_result['Runtime'],
                    rating=json_result['imdbRating'],
                    votes=json_result['imdbVotes'],
                    release_date=release_time,
                    added_by=user,
                    imdbid=imdb_id
                )
                new_movie.save()

                # update feed
                f = Feed()
                f.update_feed(user, 3, None, new_movie)
        else:
            errors.append('No data available')
    else:  # duplicate movie, returns error
        errors.append('This movie is already in our database. '
                      'Please try to add another one.')
    # return errors list
    return errors