Пример #1
0
class SiteUser(ExtrovirtsDocument):
    groups = ListField(StringField())
    password = BinaryField()
    username = StringField(unique=True)
    salt = BinaryField()
    email = StringField()
    last_login = DateTimeField()
Пример #2
0
class User(UuidDocument, TimestampsDocument):
    """
    Represents a user.

    Note: A `User`'s password can be set and validated using `setPassword()` and
    `validatePassword()`. It may also be provided to the constructor.
    """

    def __init__(self, **kwargs):
        password = kwargs.pop("password", None)
        super(User, self).__init__(**kwargs)
        if password:
            self.setPassword(password)

    username = StringField(required=True, unique=True)
    email = StringField(required=True, unique=True)
    fullName = StringField(required=True)

    isAdmin = BooleanField(required=True)

    passwordSalt = BinaryField(
        max_bytes=32, required=True, custom_json=CustomJsonRules.HIDDEN
    )
    passwordHash = BinaryField(
        max_bytes=128, required=True, custom_json=CustomJsonRules.HIDDEN
    )

    def setPassword(self, password: str):
        self.passwordSalt = generateSalt()
        self.passwordHash = hashPassword(password, self.passwordSalt)

    def validatePassword(self, password: str) -> bool:
        return bytes(self.passwordHash) == hashPassword(password, self.passwordSalt)

    meta = {"allow_inheritance": True}
Пример #3
0
class Customer(Document):
    name = BinaryField(required=True)
    """
    Encrypted name based on location's public key.
    """

    phone_number = BinaryField(required=True)
    """
    Encrypted phone_number based on location's public key.
    """

    time_in = DateTimeField(default=datetime.utcnow, required=True)
    """
    The time that the customer registers their information.
    """

    location = ReferenceField(Location)

    # regulations state that we must destroy the information 30 days after
    # visiting.
    meta = {
        "indexes": [{
            "fields": ["time_in"],
            "expireAfterSeconds": 2592000
        }]
    }

    @classmethod
    def create(cls, name: str, phone_number: str,
               location: Location) -> Customer:
        public_key = serialization.load_pem_public_key(
            location.public_key.encode("utf-8"))
        secret_name = name + "|" + secrets.token_hex(48)
        encrypted_name = public_key.encrypt(
            secret_name.encode("utf-8"),
            padding.OAEP(
                mgf=padding.MGF1(algorithm=hashes.SHA256()),
                algorithm=hashes.SHA256(),
                label=None,
            ),
        )
        secret_phone_number = phone_number + "|" + secrets.token_hex(48)
        encrypted_phone = public_key.encrypt(
            secret_phone_number.encode("utf-8"),
            padding.OAEP(
                mgf=padding.MGF1(algorithm=hashes.SHA256()),
                algorithm=hashes.SHA256(),
                label=None,
            ),
        )
        customer = cls(
            name=encrypted_name,
            phone_number=encrypted_phone,
            time_in=datetime.utcnow().replace(tzinfo=timezone.utc),
        )

        customer.location = location
        customer.save()
        return customer
Пример #4
0
class ImageHandler(Document):
    md5 = StringField(required=True, max_length=200)
    original_image = BinaryField()
    grey_image = BinaryField()
    width = IntField(required=True, max_length=50)
    height = IntField(required=True, max_length=50)
    created_at = DateTimeField(default=datetime.utcnow())
    meta = {'indexes': [{'fields': ['md5'], 'unique': True}]}
Пример #5
0
class Comparison(Document):
    exp_name = StringField(required=True)
    wpsA = BinaryField(required=True)
    wpsB = BinaryField(required=True)
    # paths to the videos for each
    pathA = StringField(required=True)
    pathB = StringField(required=True)
    # 0 for A, 1 for B, -1 for undecided
    label = IntField(required=False)
Пример #6
0
class Users(Document):
    name = StringField(required=True, max_length=200)
    fbid = IntField(require=True, primary_key=True, unique=True)
    email = EmailField(required=True)
    token = StringField(required=True)
    signed = StringField(required=True)
    hasivector = BooleanField(default=False)
    processing = BooleanField(default=False)
    clf = BinaryField()
    ivectors = BinaryField()
Пример #7
0
class TxDetail(EmbeddedDocument):
    block_hash = BinaryField(unique=True, required=True)
    block_height = IntField(unique=True, required=True)
    timestamp = IntField(required=True)
    segwit = BooleanField(required=True)
    tx_hash = BinaryField(unique=True, required=True)
    version = IntField(required=True)
    tx_ins = ListField(DynamicField(required=True), required=True)
    tx_outs = EmbeddedDocumentListField(TxOut, required=True)
    locktime = IntField(required=True)
    size = IntField(required=True)
Пример #8
0
class Block(Document):
    block_hash = BinaryField(unique=True, required=True)
    version = IntField(required=True)
    prev_block = BinaryField(required=True)
    merkle_root = BinaryField(required=True)
    timestamp = IntField(required=True)
    bits = BinaryField(required=True)
    nonce = BinaryField(required=True)
    txs = EmbeddedDocumentListField(Tx, required=True)
    block_height = IntField(unique=True, required=True)
    size = IntField(required=True)
    block_reward = IntField(required=True)
    mined_btc = IntField(required=True)
    total_size = IntField(required=True)
Пример #9
0
class HTTP(Base):
    method = StringField(required=True)
    url = StringField(required=True)
    query_string = StringField()
    query = ListField(EmbeddedDocumentField(KeyValue))
    headers = ListField(EmbeddedDocumentField(KeyValue))
    data = BinaryField()
Пример #10
0
 class UserModel(Document):
     """Basic user database model with roles"""
     name = StringField(required=True, unique=True, max_length=32)
     password = BinaryField(required=True, max_length=60)
     created = DateTimeField(default=datetime.utcnow)
     last_login = DateTimeField(default=datetime.utcnow)
     role = ListField(StringField())
Пример #11
0
class BinaryAttachment(Document):
    """ pdf 或者 png 格式的各种附件内容
        这里只提供 binary 的基于 uuid 的 key-value query
    """

    uuid = StringField(primary_key=True)
    """ 暂定 uuid 为 bin_data 解压后的 MD5 hashcode
        ALERT: gzip 的 binary 文件可能存在有随机性,相同的文件多次压缩得到的 binary 并不完全相同,所以这里的 uuid 是原始 binary 的 md5 hash
    """

    bin_data = BinaryField()
    """文件内容"""

    file_ext = StringField()
    """文件的后缀,通常为 pdf / png 等"""

    ctime = DateTimeField()
    """文件的创建时间"""

    is_gzip = BooleanField()
    """是否经过了 gzip 压缩"""

    bin_length = IntField()
    """binary 的 length"""

    action_uuid = StringField()
    """由哪个 action 获得的"""

    meta = {
        "indexes": [
            "#action_uuid",
        ]
    }
Пример #12
0
class File(BaseFieldsMixin, AdditionalOperationsMixin, QueryMixin, Document):
    """ Represents File entity, that stores information about telegram file
    """
    telegram_id = BinaryField(required=True, null=False, unique=True)

    meta = {
        "db_alias": MONGO_ENGINE_ALIAS,
        "collections": "filesystem",
        "queryset_class": CustomQuerySet
    }

    def clean(self):
        """ Encrypt telegram_id field before saving
        """
        self.telegram_id = str(self.telegram_id)
        self.telegram_id = CRYPTO.encrypt(self.telegram_id.encode())

    @staticmethod
    def prepare_telegram_id(telegram_id: bytes) -> int:
        """ Convert encrypted string to appropriate telegram message id

        :param telegram_id: encrypted message id
        :return: decrypted message id
        """
        return int(CRYPTO.decrypt(telegram_id))
Пример #13
0
class User(Document):
    user_id = StringField(primary_key=True)
    full_name = StringField(min_length=2, max_length=50, required=True)
    country = StringField(min_length=2, max_length=50, required=True)
    email = EmailField(required=True)
    passwd = BinaryField(required=True)
    totp_secret = StringField(required=False)
Пример #14
0
class RTreePage(Document):
    data = BinaryField()
    page = IntField()
    name = StringField()

    @classmethod
    def indices(cls):
        cls.objects.ensure_index("name", "page")
Пример #15
0
class Credential(Document):
    client_id = StringField()
    user_id = StringField()
    response_type = response_type = StringField(max_length=5,
                                                choices=['code', 'token'])
    request = BinaryField()
    redirect_uri = StringField()
    state = StringField()
Пример #16
0
class Datagram_User(Document):
    business_email = StringField(required=True, unique=True)
    business_password = BinaryField(required=True)
    business_name = StringField(required=True)
    number = StringField(required=True)
    dg_token = StringField(required=True)
    date_created = DateTimeField(default=datetime.datetime.utcnow)
    date_accessed = DateTimeField(default=datetime.datetime.utcnow)
Пример #17
0
class Episode(Document):
    """everything per cycle"""
    episode = IntField()
    rewards = ListField(FloatField())
    activation = ListField(
        FloatField())  #stores the average activation of the input layer
    weights = BinaryField()
    weights_human = ListField(ListField(FloatField()))
    neuromodulator = ListField(FloatField())
Пример #18
0
class User(Document):
    name = StringField(required=True)
    email = EmailField(required=True, unique=True)
    company_name = StringField(required=True)
    company_url = URLField(required=True, unique=True)
    password = BinaryField(required=True)
    github_creds = EmbeddedDocumentField(GithubCredentials)
    confluence_url = StringField(required=True, default="")
    travis_creds = StringField(required=True, default="")
    trello_creds = EmbeddedDocumentField(TrelloCredentials)
Пример #19
0
class Users(Document):
    username = StringField(max_length=256, required=True, unique=True)
    password = StringField(max_length=256, required=True)
    userpic = BinaryField()

    atime = DateTimeField(default=datetime.now)
    is_active = BooleanField(default=True)

    def save(self, *args, **kwargs):
        self.atime = datetime.now()
        super().save(*args, **kwargs)
Пример #20
0
class Item(Document):
    '''
    Holds the data related to an item.
    '''
    meta = {'collection': 'grocery-items'}
    upc = StringField(max_length=12, min_length=12, required=True)
    upc.validate = validation.validate_upc
    name = StringField(min_length=1, max_length=64, required=True)
    image_url = StringField(min_length=0, max_length=512)
    image = BinaryField()
    stores = EmbeddedDocumentListField(Store, required=True)
Пример #21
0
class User(Document):

    first_name = StringField(max_length=20)
    last_name = BinaryField(max_length=500)
    contact = StringField(max_length=40)
    email = StringField(max_length=50)

    meta = {
        'indexes': ['first_name', 'last_name'],
        'ordering': ['-date_created']
    }
Пример #22
0
class Template(Document):
    """
    Default context:

    datetime_now - datetime.datetime.now()
    query("...") - yields events based on the query
    plot("line", query("..."), "field") - draws a plot of type "line" with the queries yielded by the query using the
        "field" field.

    """
    name = StringField()
    description = StringField()
    body = BinaryField()
Пример #23
0
class Chat(Core):
    name = StringField(unique=True)
    avatar = BinaryField()
    members = ListField(ReferenceField('User'))
    owner = ReferenceField('User')
    is_personal = BooleanField(default=True)

    @property
    def messages(self):
        return Messages.objects(chat=self).all()

    @classmethod
    def chat_hiltory(cls, chatname, limit=100):
        """Получение истории чата.

        Args:
            chatname: имя чата для персональных чатов совпадает с именем контакта
            limit: ограничение по количеству сообщений (default: {100})

        Returns:
            лист сообщений

        """

        history = []
        chat = cls.objects(name=chatname).first()
        if chat:
            history = chat.messages[:limit]
        return history

    @classmethod
    def create_msg(cls, msg, received=False):
        dest_user = getattr(msg, settings.DESTINATION, None)
        src_user = getattr(msg, settings.SENDER, None)
        text = getattr(msg, settings.MESSAGE_TEXT, '')
        chat_name = msg.chat
        sender = User.by_name(src_user)
        receiver = User.by_name(dest_user)
        if not chat_name:
            chat_name = '__'.join(sorted((sender.username, receiver.username)))
        chat = cls.objects(name=chat_name).first()
        if not chat:
            chat = cls.objects.create(name=chat_name,
                                      owner=sender,
                                      members=[sender, receiver])

        Messages.objects.create(chat=chat,
                                text=text,
                                sender=sender,
                                receiver=receiver,
                                received=received)
Пример #24
0
class History(BaseDocument):
    ref_id = StringField()
    value = BinaryField()
    time = DateTimeField(default=datetime.datetime.now)
    # operator id
    o_id = ObjectIdField()
    # position id:
    #   0: head of history chain, mostly this means create an instance
    #   1: body of history chain, mostly this means update an instance
    #   2: tail of history chain, mostly this means delete an instance
    p_id = IntField()
    ip = StringField()

    meta = {'db_alias': settings.DB_NAME_HIS}
Пример #25
0
class Image(EmbeddedDocument):
    '''
    Image model class.
    '''
    img = BinaryField()

    @classmethod
    def get_image(cls, img_id):
        image = cls.objects.get(id=img_id)
        if not image:
            raise errors.DoesNotExist()
        return image

    def to_json(self):
        return {'img_id': self.pk, 'img': self.img}
Пример #26
0
class Function(Document):
    sha256 = StringField(max_length=64)
    opcodes = BinaryField()
    apis = ListField(StringField(max_length=128), default=list)
    metadata = EmbeddedDocumentListField(Metadata, default=list)
    architecture = StringField(max_length=64, required=True)

    meta = {
        'indexes' : []
    }

    def dump(self):
        return {'opcodes' : Binary(self.opcodes),
                'architecture' : self.architecture,
                'sha256' : self.sha256}
Пример #27
0
class Model(Document):
    timestamp = DateTimeField()
    runtime = FloatField()
    device_type = StringField()
    learning_rate = FloatField()
    epochs = IntField()
    network_width = IntField()
    precision_no_delay = FloatField()
    precision_delay = FloatField()
    accuracy = FloatField()
    macro_avg = FloatField()
    weighted_avg = FloatField()
    model_file = BinaryField()
    tag = StringField(default="")

    @staticmethod
    def save_model(runtime,
                   device_type,
                   learning_rate,
                   epochs,
                   network_width,
                   path,
                   classification_report,
                   tag=""):
        precision_no_delay = classification_report["0"]["precision"]
        precision_delay = classification_report["1"]["precision"]
        accuracy = classification_report["accuracy"]
        macro_avg = classification_report["macro avg"]["precision"]
        weighted_avg = classification_report["weighted avg"]["precision"]
        with open(path, "rb") as f:
            model_bin = f.read()
        return Model(timestamp=datetime.datetime.now(),
                     runtime=runtime,
                     device_type=device_type,
                     learning_rate=learning_rate,
                     epochs=epochs,
                     network_width=network_width,
                     precision_delay=precision_delay,
                     precision_no_delay=precision_no_delay,
                     accuracy=accuracy,
                     macro_avg=macro_avg,
                     weighted_avg=weighted_avg,
                     model_file=model_bin,
                     tag=tag)
Пример #28
0
class Function(Document):
    sha256 = StringField(max_length=64)
    opcodes = BinaryField()
    apis = ListField(StringField(max_length=64), default=list)
    metadata = EmbeddedDocumentListField(Metadata, default=list)
    #  Return value from idaapi.get_file_type_name()
    architecture = StringField(max_length=64, required=True)

    meta = {
        'indexes' : []
    }

    def dump(self):
        return {'id' : self.id,
                'opcodes' : self.opcodes,
                'apis'  : self.apis,
                'metadata' : [str(x.id) for x in self.metadata],
                'architecture' : self.architecture,
                'sha256' : self.sha256}
Пример #29
0
class CodedStep(ExtrovirtsDocument):
    name = StringField()
    display_name = StringField()
    coded_name = StringField()
    parameters = ListField(StringField())
    score = IntField()
    add = EmbeddedDocumentListField(Term)
    requirement_terms = EmbeddedDocumentListField(Term)
    requirement_comparisons = EmbeddedDocumentListField(Comparison)
    remove = EmbeddedDocumentListField(Term)
    deterministic = BooleanField()
    significant_parameters = ListField(IntField())
    bindings = DictField()
    source_hash = BinaryField()
    summary = StringField()
    mapping = EmbeddedDocumentListField('TechniqueMapping', default=list)
    default_mapping = EmbeddedDocumentListField('TechniqueMapping',
                                                default=list)
    cddl = StringField()
Пример #30
0
class Record(Document):
    title = StringField(max_length=100)
    comment = StringField()
    created_time = ComplexDateTimeField(default=now)
    finished_time = ComplexDateTimeField(default=now)
    modified_time = ComplexDateTimeField(default=now)
    hidden = BooleanField(default=False)
    children = ListField(ReferenceField('Record'))
    config=DictField()
    setting=DictField()
    tags = ListField(StringField(max_length=50))
    datafield = FileField(collection_name='data')
    imagefield = FileField(collection_name='images')
    imagefields = ListField(BinaryField())
    work = ReferenceField('CodeSnippet')
    notebook = ReferenceField('Notebook')
    notebook_index = IntField(min_value=0)

    def __repr__(self):
        return 'Record(title=%s, finished_time=%s, tags=%s)' % (
            self.title, self.finished_time, self.tags)

    @property
    def data(self):
        return from_pickle(self.datafield)

    @property
    @functools.lru_cache(maxsize=1)
    def image(self):
        return from_pickle(self.imagefield)

    def set_data(self, obj, content_type='application/octet-stream'):
        if self.datafield is None:
            self.datafield.put(to_pickle(obj), content_type=content_type)
        else:
            self.datafield.replace(to_pickle(obj), content_type=content_type)

    def set_image(self, img, content_type='image/png'):
        if self.imagefield is None:
            self.imagefield.put(to_pickle(img), content_type=content_type)
        else:
            self.imagefield.replace(to_pickle(img), content_type=content_type)