<commit_before>from pylm.registry.config import configuration
from pylm.registry.db import DB
from pylm.registry.models import Admin, User, AdminLog, Cluster
from pylm.registry.manager import ConfigManager
from uuid import uuid4
import tornado.web
import tornado.template
import pylm.registry
import pickle
import datetime
import inspect
import json
import os

ROOT_PATH = os.path.abspath(os.path.join(
    inspect.getfile(pylm.registry),
    os.pardir)
    )

STATIC_PATH = os.path.abspath(os.path.join(
    inspect.getfile(pylm.registry),
    os.pardir,
    'static')
    )


def db_log(text):
    log_item = AdminLog()
    log_item.when = datetime.datetime.now()
    log_item.text = text

    DB.session.add(log_item)
    DB.session.commit()


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        template_dir = os.path.join(ROOT_PATH, 'templates')
        loader = tornado.template.Loader(template_dir)
        self.write(loader.load("index.html").generate(
            version=pylm.registry.__version__)
        )


class ClusterHandler(tornado.web.RequestHandler):
    def is_user(self):
        """
        Check if the request is from a user with admin privileges

        :return: True if has admin privileges, False otherwise
        """
        if 'Key' in self.request.headers:
            user_key = self.request.headers['Key']
            # Check if the admin key is one of the valid admins.
            is_user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            if is_user and is_user.active:
                return True

            elif is_user and not is_user.active:
                self.set_status(400)
                self.write(b'User not active')

            else:
                self.set_status(400)
                self.write(b'User key not valid')

        else:
            self.set_status(400)
            self.write(b'User key not present')

        return False

    def set_new_cluster(self):
        if self.is_user():
            # Get the user from the user key
            user_key = self.request.headers['Key']
            user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            cluster = Cluster()
            cluster.key = self.get_argument('key', default=str(uuid4()))
            cluster.description = self.get_argument('description')
            cluster.when = datetime.datetime.now()
            cluster.status = ''
            cluster.user = user

            DB.session.add(cluster)
            DB.session.commit()

            self.set_status(200)
            self.write(cluster.key.encode('utf-8'))

    def get_clusters_list(self):
        if self.is_user():
            # Get the user from the user key
            user_key = self.request.headers['Key']
            user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            clusters_dump = dict()
            for cluster in user.clusters:
                clusters_dump[cluster.key] = {
                    "description": cluster.description
                }

            self.set_status(200)
            self.write(json.dumps(clusters_dump).encode('utf-8'))

    def set_cluster_reset(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')
            cluster_data = DB.session.query(
                Cluster).filter(Cluster.key == cluster_key).one_or_none()
            cluster_data.status = ''
            DB.session.commit()

            self.set_status(200)
            self.write(cluster_key.encode('utf-8'))

    def set_cluster_delete(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')
            cluster = DB.session.query(
                Cluster
                ).filter(Cluster.key == cluster_key).one_or_none()

            DB.session.delete(cluster)
            DB.session.commit()

            self.set_status(200)
            self.write(cluster_key)

    def get_cluster_status(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')<commit_msg>Forgot to set the first assignment as binary<commit_after>from pylm.registry.config import configuration
from pylm.registry.db import DB
from pylm.registry.models import Admin, User, AdminLog, Cluster
from pylm.registry.manager import ConfigManager
from uuid import uuid4
import tornado.web
import tornado.template
import pylm.registry
import pickle
import datetime
import inspect
import json
import os

ROOT_PATH = os.path.abspath(os.path.join(
    inspect.getfile(pylm.registry),
    os.pardir)
    )

STATIC_PATH = os.path.abspath(os.path.join(
    inspect.getfile(pylm.registry),
    os.pardir,
    'static')
    )


def db_log(text):
    log_item = AdminLog()
    log_item.when = datetime.datetime.now()
    log_item.text = text

    DB.session.add(log_item)
    DB.session.commit()


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        template_dir = os.path.join(ROOT_PATH, 'templates')
        loader = tornado.template.Loader(template_dir)
        self.write(loader.load("index.html").generate(
            version=pylm.registry.__version__)
        )


class ClusterHandler(tornado.web.RequestHandler):
    def is_user(self):
        """
        Check if the request is from a user with admin privileges

        :return: True if has admin privileges, False otherwise
        """
        if 'Key' in self.request.headers:
            user_key = self.request.headers['Key']
            # Check if the admin key is one of the valid admins.
            is_user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            if is_user and is_user.active:
                return True

            elif is_user and not is_user.active:
                self.set_status(400)
                self.write(b'User not active')

            else:
                self.set_status(400)
                self.write(b'User key not valid')

        else:
            self.set_status(400)
            self.write(b'User key not present')

        return False

    def set_new_cluster(self):
        if self.is_user():
            # Get the user from the user key
            user_key = self.request.headers['Key']
            user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            cluster = Cluster()
            cluster.key = self.get_argument('key', default=str(uuid4()))
            cluster.description = self.get_argument('description')
            cluster.when = datetime.datetime.now()
            cluster.status = ''
            cluster.user = user

            DB.session.add(cluster)
            DB.session.commit()

            self.set_status(200)
            self.write(cluster.key.encode('utf-8'))

    def get_clusters_list(self):
        if self.is_user():
            # Get the user from the user key
            user_key = self.request.headers['Key']
            user = DB.session.query(
                User).filter(User.key == user_key).one_or_none()

            clusters_dump = dict()
            for cluster in user.clusters:
                clusters_dump[cluster.key] = {
                    "description": cluster.description
                }

            self.set_status(200)
            self.write(json.dumps(clusters_dump).encode('utf-8'))

    def set_cluster_reset(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')
            cluster_data = DB.session.query(
                Cluster).filter(Cluster.key == cluster_key).one_or_none()
            cluster_data.status = b''
            DB.session.commit()

            self.set_status(200)
            self.write(cluster_key.encode('utf-8'))

    def set_cluster_delete(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')
            cluster = DB.session.query(
                Cluster
                ).filter(Cluster.key == cluster_key).one_or_none()

            DB.session.delete(cluster)
            DB.session.commit()

            self.set_status(200)
            self.write(cluster_key)

    def get_cluster_status(self):
        if self.is_user():
            cluster_key = self.get_argument('cluster')
{'fraction_non_alphanumeric': 0.10328185328185328, 'fraction_numerical': 0.006274131274131274, 'mean_word_length': 1.9924187725631768, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3796522', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2329, 'n_words': 483}