<commit_before># -*- coding: utf-8 -*-

from accounts.models import Student
from core.utils import JsonResponse
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseBadRequest, HttpResponse
from django.utils import simplejson
from lazysignup.decorators import allow_lazy_user
from questions.models import PlaceRelation, UsersPlace
from questions.utils import QuestionService

# Create your views here.


@allow_lazy_user
def question(request, map_code):
    try:
        map = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_ON_MAP)
    except PlaceRelation.DoesNotExist:
        raise Http404
    student = Student.objects.fromUser(request.user)
    qs = QuestionService(user=student, map=map)
    questionIndex = 0
    if (request.raw_post_data != ""):
        answer = simplejson.loads(request.raw_post_data)
        questionIndex = answer['index'] + 1
        qs.answer(answer)
    noOfQuestions = 5 if student.points < 10 else 10
    noOfQuestions -= questionIndex
    response = qs.get_questions(noOfQuestions)
    return JsonResponse(response)


def users_places(request, map_code, user=''):
    try:
        map = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_ON_MAP)
    except PlaceRelation.DoesNotExist:
        raise Http404("Unknown map name: {0}".format(map_code))
    if (user == ''):
        user = request.user
    else:
        try:
            user = User.objects.get(username=user)
        except User.DoesNotExist:
            raise HttpResponseBadRequest("Invalid username: {0}" % user)

    if request.user.is_authenticated():
        student = Student.objects.fromUser(user)
        ps = UsersPlace.objects.filter(
            user=student,
            place_id__in=map.related_places.all()
        ).select_related().order_by("place__name")
    else:
        ps = []
    try:
        cs = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_SUBMAP,
        ).related_places.all()
    except PlaceRelation.DoesNotExist:
        cs = []
    response = {
        'name': map.place.name,
        'placesTypes': [{
            'name': u'Kontinenty',
            'haveMaps': True,
            'places': [p.to_serializable() for p in cs]
        }, {
            'name': u'Státy',
            'places': [p.to_serializable() for p in ps]
        }]
    }
    return JsonResponse(response)
<commit_msg>skill map views logging<commit_after># -*- coding: utf-8 -*-

from accounts.models import Student
from core.utils import JsonResponse
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseBadRequest, HttpResponse
from django.utils import simplejson
from lazysignup.decorators import allow_lazy_user
from questions.models import PlaceRelation, UsersPlace
from questions.utils import QuestionService
from logging import getLogger


logger = getLogger(__name__)


@allow_lazy_user
def question(request, map_code):
    try:
        map = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_ON_MAP)
    except PlaceRelation.DoesNotExist:
        raise Http404
    student = Student.objects.fromUser(request.user)
    qs = QuestionService(user=student, map=map)
    questionIndex = 0
    if (request.raw_post_data != ""):
        answer = simplejson.loads(request.raw_post_data)
        questionIndex = answer['index'] + 1
        qs.answer(answer)
    noOfQuestions = 5 if student.points < 10 else 10
    noOfQuestions -= questionIndex
    response = qs.get_questions(noOfQuestions)
    return JsonResponse(response)


def users_places(request, map_code, user=''):
    try:
        map = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_ON_MAP)
    except PlaceRelation.DoesNotExist:
        raise Http404("Unknown map name: {0}".format(map_code))
    if (user == ''):
        user = request.user
    else:
        try:
            user = User.objects.get(username=user)
        except User.DoesNotExist:
            raise HttpResponseBadRequest("Invalid username: {0}" % user)

    if request.user.is_authenticated():
        student = Student.objects.fromUser(user)
        ps = UsersPlace.objects.filter(
            user=student,
            place_id__in=map.related_places.all()
        ).select_related().order_by("place__name")
    else:
        ps = []
    try:
        cs = PlaceRelation.objects.get(
            place__code=map_code,
            type=PlaceRelation.IS_SUBMAP,
        ).related_places.all()
    except PlaceRelation.DoesNotExist:
        cs = []
    response = {
        'name': map.place.name,
        'placesTypes': [{
            'name': u'Kontinenty',
            'haveMaps': True,
            'places': [p.to_serializable() for p in cs]
        }, {
            'name': u'Státy',
            'places': [p.to_serializable() for p in ps]
        }]
    }
    logger.info(
        u"users_places: previewed map '{0}' of user '{1}' with '{2}' places".
        format(map.place.name, user, len(cs)))
    return JsonResponse(response)

{'fraction_non_alphanumeric': 0.10363495746326373, 'fraction_numerical': 0.007927300850734726, 'mean_word_length': 2.622549019607843, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3726221', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1520, 'n_words': 335}