<commit_before>#!/usr/bin/env python3
# -*- coding: utf8 -*-

import datetime
import hashlib

from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, or_, and_, Boolean
from sqlalchemy.orm import relationship

from smutils import smpacket
import models.schema

class Room(models.schema.Base):
    __tablename__ = 'rooms'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    password = Column(String(255))
    description = Column(Text, default="")
    static = Column(Boolean, default=False)

    status = Column(Integer, default=0)
    type = Column(Integer, default=1)
    users = relationship("User", back_populates="room")

    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime, onupdate=datetime.datetime.now)

    def __repr__(self):
        return "<Room #%s (name='%s')>" % (self.id, self.name)

    def to_packet(self):
        return smpacket.SMOPacketServerRoomUpdate(
            type=0,
            room_title=self.name,
            room_description=self.description,
            room_type=self.type
        )

    @staticmethod
    def list_smopacket(rooms):
        packet = smpacket.SMOPacketServerRoomUpdate(
            type=1,
            nb_rooms=len(rooms),
            rooms=[],
            room_status=[],
            room_flags=[]
        )

        for room in rooms:
            packet["rooms"].append({
                "title": room.name,
                "description": room.description
            })
            packet['room_status'].append(room.status)
            packet['room_flags'].append(1 if room.password else 0)

        return smpacket.SMPacketServerNSSMONL(packet=packet)

    @classmethod
    def smo_list(cls, session):
        rooms = session.query(Room).all()
        return cls.list_smopacket(rooms)

    @classmethod
    def login(cls, name, password, session):
        return (
            session.query(cls)
            .filter_by(name=name)
            .filter(or_(
                cls.password.is_(None),
                and_(
                    cls.password.isnot(None),
                    cls.password == hashlib.sha256(password.encode('utf-8')).hexdigest()
                )))
            .first()
            )

    @classmethod
    def init_from_hashes(cls, hrooms, session):
        rooms = []

        for hroom in hrooms:
            room = session.query(cls).filter_by(name=hroom["name"]).first()

            if not room:
                room = cls(name=hroom["name"])
                session.add(room)

            room.description = hroom.get("description", ""),
            room.password = hashlib.sha256(hroom["password"].encode('utf-8')).hexdigest() if hroom.get("password") else None

            rooms.append(room)

        return rooms

<commit_msg>Room from configuration file are static<commit_after>#!/usr/bin/env python3
# -*- coding: utf8 -*-

import datetime
import hashlib

from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, or_, and_, Boolean
from sqlalchemy.orm import relationship

from smutils import smpacket
import models.schema

class Room(models.schema.Base):
    __tablename__ = 'rooms'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    password = Column(String(255))
    description = Column(Text, default="")
    static = Column(Boolean, default=False)

    status = Column(Integer, default=0)
    type = Column(Integer, default=1)
    users = relationship("User", back_populates="room")

    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime, onupdate=datetime.datetime.now)

    def __repr__(self):
        return "<Room #%s (name='%s')>" % (self.id, self.name)

    def to_packet(self):
        return smpacket.SMOPacketServerRoomUpdate(
            type=0,
            room_title=self.name,
            room_description=self.description,
            room_type=self.type
        )

    @staticmethod
    def list_smopacket(rooms):
        packet = smpacket.SMOPacketServerRoomUpdate(
            type=1,
            nb_rooms=len(rooms),
            rooms=[],
            room_status=[],
            room_flags=[]
        )

        for room in rooms:
            packet["rooms"].append({
                "title": room.name,
                "description": room.description
            })
            packet['room_status'].append(room.status)
            packet['room_flags'].append(1 if room.password else 0)

        return smpacket.SMPacketServerNSSMONL(packet=packet)

    @classmethod
    def smo_list(cls, session):
        rooms = session.query(Room).all()
        return cls.list_smopacket(rooms)

    @classmethod
    def login(cls, name, password, session):
        return (
            session.query(cls)
            .filter_by(name=name)
            .filter(or_(
                cls.password.is_(None),
                and_(
                    cls.password.isnot(None),
                    cls.password == <PASSWORD>(password.encode('utf-8')).hex<PASSWORD>()
                )))
            .first()
            )

    @classmethod
    def init_from_hashes(cls, hrooms, session):
        rooms = []

        for hroom in hrooms:
            room = session.query(cls).filter_by(name=hroom["name"]).first()

            if not room:
                room = cls(name=hroom["name"])
                session.add(room)

            room.description = hroom.get("description", ""),
            room.password = <PASSWORD>(hroom["password"].encode('utf-8')).hex<PASSWORD>() if hroom.get("password") else None
            room.static = True

            rooms.append(room)

        return rooms


{'fraction_non_alphanumeric': 0.11947136563876652, 'fraction_numerical': 0.006696035242290749, 'mean_word_length': 2.170949720670391, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, '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': '6763704', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1585, 'n_words': 323}