<commit_before>from django.http import HttpResponse
from django.db.models import get_model
from django.utils import simplejson
from django.contrib.contenttypes.models import ContentType

from .models import Favorite


def add_or_remove(request):
    if request.is_ajax():
        user = request.user
        target_model = get_model(*request.POST['target_model'].split('.') or None)
        target_content_type = ContentType.objects.get_for_model(target_model)
        target_object_id = request.POST['target_object_id']

        # delete it if it's already a faorite
        if user.favorite_set.filter(target_content_type=target_content_type,
                                 target_object_id=target_object_id):
            user.favorite_set.get(target_content_type=target_content_type,
                                     target_object_id=target_object_id).delete()
            status = 'deleted'

        # otherwise, create it
        else:
            user.favorite_set.create(target_content_type=target_content_type,
                                     target_object_id=target_object_id)
            status = 'added'

        response = {'status': status,
                    'fav_count': Favorite.objects.filter(target_content_type=target_content_type,
                                                         target_object_id=target_object_id).count()}

        return HttpResponse(simplejson.dumps(response, ensure_ascii=False),
                            mimetype='application/json')

    return HttpResponse(status=405)<commit_msg>Refactoring add_or_remove view to use manager querysets.<commit_after># -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseNotAllowed, HttpResponseBadRequest
from django.utils import simplejson

from .models import Favorite


@login_required
def add_or_remove(request):

    if not request.is_ajax():
        return HttpResponseNotAllowed()

    user = request.user

    try:
        app_model = request.POST["target_model"]
        obj_id = request.POST["target_object_id"]
    except KeyError:
        return HttpResponseBadRequest()

    fav = Favorite.objects.get_favorite(user, obj_id, model=app_model)

    if fav is None:
        Favorite.objects.create(user, obj_id, app_model)
        status = 'added'
    else:
        fav.delete()
        status = 'deleted'

    response = {
        'status': status,
        'fav_count': Favorite.objects.for_object(obj_id, app_model).count()
    }

    return HttpResponse(
        simplejson.dumps(response, ensure_ascii=False),
        mimetype='application/json'
    )
{'fraction_non_alphanumeric': 0.08361710177828226, 'fraction_numerical': 0.001513431706394249, 'mean_word_length': 2.662049861495845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460101', 'n_tokens_mistral': 736, 'n_tokens_neox': 718, 'n_words': 141}