<commit_before>#coding:utf-8

from __future__ import division
from random import sample, randrange, choice
import string
import sys
import shutil
import os
import time
import datetime


from django.views.generic import *
from django.http import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.db.models import F
from django.views.decorators.csrf import csrf_exempt


# Imaginary function to handle an uploaded file.
from crike_django.models import *
from crike_django.forms import *
from crike_django.settings import MEDIA_ROOT,STATIC_ROOT
from word_utils import download_word, handle_uploaded_file
from image_download import download_images_single, is_path_full
from multiprocessing import Process


# Utils
def save_file(srcfile, dst):
    try:
        file = open(dst, 'wb')
        file.write(srcfile.read())
        file.close()
        srcfile.close()
    except Exception as e:
        print(e)

def get_or_none(model, **kwargs):
    try:
        return model.objects.filter(**kwargs)
    except model.DoesNotExist:
        return None

def get_bookobj(book):
    bookobjs = Book.objects.filter(name=book)
    if len(bookobjs) > 0:
        return bookobjs[0]
    return None

def get_lessonemb(book, lesson):
    bookobj = get_bookobj(book)
    if bookobj == None:
        return None
    lessonobjs = filter(lambda x: x.name == lesson, bookobj.lessons)
    if len(lessonobjs) > 0:
        return lessonobjs[0]
    return None

def get_lessonobj(lessonemb):
    return lessonemb.book.lesson_set.filter(name=lessonemb.name)[0]

def get_words_from_lesson(book, lesson):
    lessonobj = get_lessonemb(book, lesson)
    if lessonobj:
        # https://bitbucket.org/wkornewald/djangotoolbox/pull-request/3/allow-setting-an-actual-object-in-a/diff
        return Word.objects.filter(id__in=lessonobj.words)
    return []

def get_words_from_paginator(paginator, page):
    try:
        if page is -1:
            return paginator.page(paginator.num_pages)
        words = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        words = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        words = paginator.page(paginator.num_pages)

    return words


def today():
    return datetime.datetime.now().date()


def now():
    return datetime.datetime.now()


class IndexView(TemplateView):
    template_name = 'index.html'

    def get(self, request, *args, **kwargs):
        if request.user:
            p = get_profile(request.user)
            if p.last_visit is None or p.last_visit.date() != today():
                p.point_add(10)
                p.save()

        books = Book.objects.all()
        return render(request, self.template_name, {'books':books})

    def post(self, request, *args, **kwargs):
        return HttpResponse("Not implement yet")
<commit_msg>view: prevent None profile of admin;<commit_after>#coding:utf-8

from __future__ import division
from random import sample, randrange, choice
import string
import sys
import shutil
import os
import time
import datetime


from django.views.generic import *
from django.http import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.db.models import F
from django.views.decorators.csrf import csrf_exempt


# Imaginary function to handle an uploaded file.
from crike_django.models import *
from crike_django.forms import *
from crike_django.settings import MEDIA_ROOT,STATIC_ROOT
from word_utils import download_word, handle_uploaded_file
from image_download import download_images_single, is_path_full
from multiprocessing import Process


# Utils
def save_file(srcfile, dst):
    try:
        file = open(dst, 'wb')
        file.write(srcfile.read())
        file.close()
        srcfile.close()
    except Exception as e:
        print(e)

def get_or_none(model, **kwargs):
    try:
        return model.objects.filter(**kwargs)
    except model.DoesNotExist:
        return None

def get_bookobj(book):
    bookobjs = Book.objects.filter(name=book)
    if len(bookobjs) > 0:
        return bookobjs[0]
    return None

def get_lessonemb(book, lesson):
    bookobj = get_bookobj(book)
    if bookobj == None:
        return None
    lessonobjs = filter(lambda x: x.name == lesson, bookobj.lessons)
    if len(lessonobjs) > 0:
        return lessonobjs[0]
    return None

def get_lessonobj(lessonemb):
    return lessonemb.book.lesson_set.filter(name=lessonemb.name)[0]

def get_words_from_lesson(book, lesson):
    lessonobj = get_lessonemb(book, lesson)
    if lessonobj:
        # https://bitbucket.org/wkornewald/djangotoolbox/pull-request/3/allow-setting-an-actual-object-in-a/diff
        return Word.objects.filter(id__in=lessonobj.words)
    return []

def get_words_from_paginator(paginator, page):
    try:
        if page is -1:
            return paginator.page(paginator.num_pages)
        words = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        words = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        words = paginator.page(paginator.num_pages)

    return words


def today():
    return datetime.datetime.now().date()


def now():
    return datetime.datetime.now()


class IndexView(TemplateView):
    template_name = 'index.html'

    def get(self, request, *args, **kwargs):
        if request.user:
            p = get_profile(request.user)
            if p is None:
                pass
            elif p.last_visit is None or p.last_visit.date() != today():
                p.point_add(10)
                p.save()

        books = Book.objects.all()
        return render(request, self.template_name, {'books':books})

    def post(self, request, *args, **kwargs):
        return HttpResponse("Not implement yet")

{'fraction_non_alphanumeric': 0.09255042290175666, 'fraction_numerical': 0.004879635653871178, 'mean_word_length': 3.163168584969533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '396491', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1870, 'n_words': 506}