Exemple #1
0
 def get(request, id):
     user_id = auth.get_user(request).id
     time.sleep(1)
     try:
         rating = Rating.objects.get(user_id=user_id, book_id=id)
         return RestJsonResponse(rating.to_dict_without_user_and_book())
     except Rating.DoesNotExist:
         return RestJsonResponse(code='404', msg='no such rating')
Exemple #2
0
 def process_exception(request, exception):
     return
     if isinstance(exception, ObjectDoesNotExist):
         pass
         # return RestJsonResponse(msg='not found', code='404')
     elif isinstance(exception, KeyError):
         return RestJsonResponse(msg='property not found: ' + str(exception), code='404')
     elif isinstance(exception, AttributeError) and 'Request\' object has no attribute \'info\'' in str(exception):
         return RestJsonResponse(msg='request content type must be application/json', code='400', status=400)
Exemple #3
0
def login(request):
    username = request.info['username']
    password = request.info['password']
    user = auth.authenticate(username=username, password=password)
    if user is None:
        return RestJsonResponse(msg='wrong username or password',
                                code="403",
                                status=200)
    auth.login(request, user)
    return RestJsonResponse()
Exemple #4
0
def books_detail_ratings(request, id):
    page_number = int(request.GET.get('page_number', '1'))
    order_by = request.GET.get('order_by', 'id')
    if 'user' not in order_by and 'rating' not in order_by:
        desc = False
        if order_by[0] == '-':
            desc = True
            order_by = order_by[1:]
        order_by = 'user__' + order_by
        if desc:
            order_by = '-' + order_by

    ratings = Rating.objects.filter(book__id=id,
                                    deleted=False).order_by(order_by)
    if 'user_name' in request.GET:
        ratings = ratings.filter(user__name__contains=request.GET['user_name'])

    page_size = int(request.GET.get('page_size', str(ratings.count())))
    page = Paginator(ratings,
                     page_size if page_size > 0 else 1).page(page_number)
    return RestJsonResponse({
        'count':
        page.paginator.count,
        'content':
        list(map(lambda e: e.to_dict(), list(page))),
    })
Exemple #5
0
 def delete(request, id):
     book = Book.objects.get(id=id)
     book.deleted = True
     book.save()
     Rating.objects.filter(book_id=book.id).update(deleted=True)
     time.sleep(1)
     return RestJsonResponse()
Exemple #6
0
def recommendations_regenerate_similarity(request):
    recommendation.regenerate_similarity()
    return RestJsonResponse({
        'cost_seconds':
        str(recommendation.get_cost_seconds()),
        'size':
        sys.getsizeof(recommendation.get_similarity()),
    })
Exemple #7
0
 def get(request):
     recalls = Metrics.objects.filter(algorithm_name='item_cf',
                                      type='recall').order_by('k')
     ans = []
     for recall in recalls:
         ans.append({
             'k': recall.k,
             'value': recall.value,
         })
     return RestJsonResponse(ans)
Exemple #8
0
    def get(request):
        precisions = Metrics.objects.filter(algorithm_name='item_cf',
                                            type='precision').order_by('k')
        ans = []

        for precision in precisions:
            ans.append({
                'k': precision.k,
                'value': precision.value,
            })
        return RestJsonResponse(ans)
Exemple #9
0
 def get(request):
     coverages = Metrics.objects.filter(algorithm_name='item_cf',
                                        type='coverage').order_by('k')
     ans = []
     for coverage in coverages:
         ans.append({
             'k': coverage.k,
             'value': coverage.value,
         })
     return RestJsonResponse(ans)
     pass
Exemple #10
0
 def get(request):
     page_number = int(request.GET.get('page_number', '1'))
     order_by = request.GET.get('order_by', 'id')
     users = User.objects.order_by(order_by)
     if 'name' in request.GET:
         users = users.filter(name__contains=request.GET['name'])
     if 'location' in request.GET:
         users = users.filter(location__contains=request.GET['location'])
     page_size = int(request.GET.get('page_size', str(users.count())))
     page = Paginator(users,
                      page_size if page_size > 0 else 1).page(page_number)
     return RestJsonResponse(page)
Exemple #11
0
 def put(request, id):
     user = User.objects.filter(id=id)
     request.info.pop('id')
     request.info.pop('password')
     info = dict()
     info['username'] = request.info['username']
     info['email'] = request.info['email']
     info['location'] = request.info['location']
     info['age'] = request.info['age']
     info['avatar_url'] = request.info['avatar_url']
     user.update(**info)
     return RestJsonResponse(User.objects.get(id=id))
Exemple #12
0
 def get(request):
     page_number = int(request.GET.get('page_number', '1'))
     order_by = request.GET.get('order_by', 'id')
     books = Book.objects.filter(deleted=False).order_by(order_by)
     if 'title' in request.GET:
         books = books.filter(title__contains=request.GET['title'])
     if 'isbn' in request.GET:
         books = books.filter(isbn__contains=request.GET['isbn'])
     page_size = int(request.GET.get('page_size', str(books.count())))
     page = Paginator(books,
                      page_size if page_size > 0 else 1).page(page_number)
     return RestJsonResponse(page)
Exemple #13
0
def recommendations_status(request):
    recommendation.update_similarity()
    return RestJsonResponse({
        'precision_generating':
        recommendation.get_redis_server().get('books_recommend:precision_mark')
        is not None,
        'recall_generating':
        recommendation.get_redis_server().get('books_recommend:recall_mark')
        is not None,
        'coverage_generating':
        recommendation.get_redis_server().get('books_recommend:coverage_mark')
        is not None,
        'cost_seconds':
        recommendation.get_cost_seconds()
    })
Exemple #14
0
 def put(request, id):
     user_id = auth.get_user(request).id
     rating_id = None
     try:
         rating_id = Rating.objects.get(user_id=user_id, book_id=id).id
     except Rating.DoesNotExist:
         pass
     print(request.info)
     rating = Rating(book_id=id,
                     user_id=user_id,
                     rating=request.info['rating'])
     rating.id = rating_id
     rating.save()
     time.sleep(1)
     return RestJsonResponse()
Exemple #15
0
    def put(request):
        ks = request.info['ks']
        recommendation.get_redis_server().set('books_recommend:recall_mark',
                                              '')
        precisions = dict()
        for k in ks:
            precisions[k] = _recommendations_update_recall(k)

        Metrics.objects.filter(algorithm_name='item_cf',
                               type='recall').delete()
        for k, value in precisions.items():
            precision = Metrics(algorithm_name='item_cf',
                                type='recall',
                                k=k,
                                value=value,
                                description='none')
            precision.save()

        recommendation.get_redis_server().delete('books_recommend:recall_mark')
        return RestJsonResponse(None)
Exemple #16
0
def users_detail_recommendations(request, id):
    page_number = int(request.GET.get('page_number', '1'))
    page_size = int(request.GET.get('page_size', 100))
    k = int(request.GET.get('k', 5))
    ignore_rated = request.GET.get('ignore_rated', 'False').lower() == 'true'
    print(ignore_rated)
    book_ids_rated = set()

    if ignore_rated:
        books = Rating.objects.filter(user_id=id,
                                      deleted=False).values_list('book_id')
        for book in books:
            book_ids_rated.add(book[0])

    ratings = Rating.objects.filter(user_id=id, rating__gt=3)
    ranks = {}
    for item in ratings:
        if item.book_id not in recommendation.get_similarity_sorted():
            continue

        for book_id, p in recommendation.get_similarity_sorted()[
                item.book_id][0:k]:
            if book_id in book_ids_rated:
                continue
            ranks.setdefault(book_id, 0.0)
            ranks[book_id] += p * 1

    ranks = sorted(ranks.items(), key=operator.itemgetter(1), reverse=True)

    books = []
    for rank in ranks[page_size * (page_number -
                                   1):min(page_size *
                                          page_number, Book.objects.count())]:
        book = Book.objects.get(id=rank[0]).to_dict()
        book['rank'] = rank[1]
        books.append(book)

    return RestJsonResponse({'count': len(ranks), 'content': books})
Exemple #17
0
def recommendations_f_measure(request):
    recalls = Metrics.objects.filter(algorithm_name='item_cf',
                                     type='recall').order_by('k')
    precisions = Metrics.objects.filter(algorithm_name='item_cf',
                                        type='precision').order_by('k')
    recall_dict = {}
    for recall in recalls:
        recall_dict[recall.k] = recall.value
    precision_dict = {}
    for precision in precisions:
        precision_dict[precision.k] = precision.value
    ans = []
    for k, recall in recall_dict.items():
        if k not in precision_dict:
            continue
        precision = precision_dict[k]
        ans.append({
            'k':
            k,
            'value':
            round(2 * precision * recall / (precision + recall), 6)
        })
    return RestJsonResponse(ans)
Exemple #18
0
 def get(request):
     user = auth.get_user(request)
     # time.sleep(2)
     return RestJsonResponse(user)
Exemple #19
0
def logout(request):
    auth.logout(request)
    return RestJsonResponse()
Exemple #20
0
 def get(request, id):
     book = Book.objects.get(id=id)
     return RestJsonResponse(book)
Exemple #21
0
def users_list(request):
    users = auth.models.User.objects.all()
    return RestJsonResponse(users)
Exemple #22
0
 def post(request):
     user = User(**request.info)
     user.id = None
     user.save()
     return RestJsonResponse(user)
Exemple #23
0
 def get(request, id):
     user = User.objects.get(id=id)
     return RestJsonResponse(user)
Exemple #24
0
def recommendations_update_similarity(request):
    recommendation.update_similarity()
    return RestJsonResponse(
        {'cost_seconds': recommendation.get_cost_seconds()})
Exemple #25
0
 def delete(request, id):
     user = User.objects.filter(id=id)
     user.delete()
     return RestJsonResponse()
Exemple #26
0
 def post(request):
     book = Book(**request.info)
     book.id = None
     book.save()
     return RestJsonResponse(book)
Exemple #27
0
 def get(request, id):
     rating = Rating.objects.get(id=id)
     return RestJsonResponse(rating)
Exemple #28
0
 def post(request):
     User.objects.create_user(username=request.info['username'],
                              password=request.info['password'],
                              email='')
     return RestJsonResponse(None)
Exemple #29
0
 def put(request, id):
     user = User.objects.filter(id=id)
     request.info.pop('id')
     user.update(**request.info)
     return RestJsonResponse(User.objects.get(id=id))
Exemple #30
0
 def put(request, id):
     book = Book.objects.filter(id=id)
     request.info.pop('id')
     book.update(**request.info)
     time.sleep(1)
     return RestJsonResponse(Book.objects.get(id=id))