<commit_before>from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.forms.models import model_to_dict
from django.utils import timezone
import json, collections, datetime

from api.models import *

# internal implementations
class JsonResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        kwargs.setdefault('content_type', 'application/json')
        data = json.dumps(data, ensure_ascii=False)
        super(JsonResponse, self).__init__(content=data, **kwargs)


def _response_with_header(data):
    ret = JsonResponse(data)
    ret['Access-Control-Allow-Origin'] = '*'
    ret['Access-Control-Allow-Methods'] = 'POST, GET, OPTION'
    ret['Access-Control-Allow-Headers'] = 'Content-Type, Accept, Origin'
    ret['Access-Control-Max-Age'] = '86400'
    return ret


def _is_hex(str):
    if len(str) != 6: return False
    allowed = ['0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f']
    for i in range(6):
        if str[i].lower() not in allowed: return False
    return True


def _valid_content(str):
    return len(str) <= 140


# get un-retrieved bullets
def new_bullets(request):
    if request.method == 'OPTIONS':
        return _response_with_header({ 'ok': True })

    if request.method != 'GET':
        return HttpResponseBadRequest('bad request type')

    time_now = timezone.now()
    objs = Bullet.objects.filter(post_time=None).exclude(ret_time=None)
    for obj in objs:
        if time_now - obj.ret_time > datetime.timedelta(minutes=10):
            obj.ret_time = None; obj.save()

    objs = Bullet.objects.filter(ret_time=None, post_time=None)
    resp = { 'ok': True, 'bullets': [] }
    for obj in objs:
        obj.ret_time = time_now; obj.save()
        cur = { 'content': obj.content, 'color': obj.color, 'id': obj.id,
            'display_mode': obj.display_mode,'font_size': obj.font_size }
        resp['bullets'].append(cur)
        if len(resp['bullets']) >= 20: break

    return _response_with_header(resp)


# update posted bullets
def success_last_retrieve(request):
    if request.method == 'OPTIONS':
        return _response_with_header({ 'ok': True })
<commit_msg>fix bug<commit_after>from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.forms.models import model_to_dict
from django.utils import timezone
import json, collections, datetime

from api.models import *

# internal implementations
class JsonResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        kwargs.setdefault('content_type', 'application/json')
        data = json.dumps(data, ensure_ascii=False)
        super(JsonResponse, self).__init__(content=data, **kwargs)


def _response_with_header(data):
    ret = JsonResponse(data)
    ret['Access-Control-Allow-Origin'] = '*'
    ret['Access-Control-Allow-Methods'] = 'POST, GET, OPTION'
    ret['Access-Control-Allow-Headers'] = 'Content-Type, Accept, Origin'
    ret['Access-Control-Max-Age'] = '86400'
    return ret


def _is_hex(str):
    if len(str) != 6: return False
    allowed = ['0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f']
    for i in range(6):
        if str[i].lower() not in allowed: return False
    return True


def _valid_content(str):
    return len(str) <= 140


# get un-retrieved bullets
def new_bullets(request):
    if request.method == 'OPTIONS':
        return _response_with_header({ 'ok': True })

    if request.method != 'GET':
        return HttpResponseBadRequest('bad request type')

    time_now = timezone.now()
    objs = Bullet.objects.filter(post_time=None).exclude(ret_time=None)
    for obj in objs:
        if time_now - obj.ret_time > datetime.timedelta(minutes=10):
            obj.ret_time = None; obj.save()

    objs = Bullet.objects.filter(ret_time=None, post_time=None)
    resp = { 'ok': True, 'bullets': [] }
    for obj in objs:
        obj.ret_time = time_now; obj.save()
        cur = {
            'content': obj.content,
            'color': obj.color,
            'id': obj.id,
            'display_mode': obj.display_mode,
            'font_size': obj.font_size,
            'num_repeat': obj.num_repeat,
        }
        resp['bullets'].append(cur)
        if len(resp['bullets']) >= 20: break

    return _response_with_header(resp)


# update posted bullets
def success_last_retrieve(request):
    if request.method == 'OPTIONS':
        return _response_with_header({ 'ok': True })

{'fraction_non_alphanumeric': 0.14419642857142856, 'fraction_numerical': 0.010714285714285714, 'mean_word_length': 3.38454011741683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5961255', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1404, 'n_words': 326}