Beispiel #1
0
 def test_mongo_id_blank(self):
     c = MongoId(allow_blank=True)
     assert c.check("5583f69d690b2d70a4afdfae") == ObjectId(
         '5583f69d690b2d70a4afdfae')
     res = extract_error(c, 'just_id')
     assert res == "'just_id' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string"
     assert isinstance(c.check(None), ObjectId)
Beispiel #2
0
    def test_mongo_id_blank(self):
        c = MongoId(allow_blank=True)
        self.assertEqual(c.check("5583f69d690b2d70a4afdfae"),
                         ObjectId('5583f69d690b2d70a4afdfae'))
        res = extract_error(c, 'just_id')
        self.assertEqual(res, "'just_id' is not a valid ObjectId, it must be"
                              " a 12-byte input or a 24-character hex string")

        self.assertIsInstance(c.check(None), ObjectId)
Beispiel #3
0
 def test_mongo_id(self):
     c = MongoId()
     assert isinstance(repr(c), str)
     assert c.check("5583f69d690b2d70a4afdfae") == ObjectId(
         '5583f69d690b2d70a4afdfae')
     res = extract_error(c, 'just_id')
     assert res == "'just_id' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string"
     res = extract_error(c, None)
     assert res == "blank value is not allowed"
Beispiel #4
0
class ImageFinger(
        namedtuple('ImageFinger', 'id,vectors,message,file_id,chat_id'),
        StorableMix):
    collection = 'images'
    trafaret = t.Dict({
        'id': t.Or(t.String | MongoId(allow_blank=True)),
        'vectors': t.Any,
        'message': t.Dict().allow_extra('*'),
        'file_id': t.String,
        'chat_id': t.Int
    })
Beispiel #5
0
class Reaction(
        namedtuple(
            'BaseReaction',
            'id,patterns,image_url,image_id,text,created_at,created_by,last_used'
        ), StorableMix):
    collection = 'reactions'

    trafaret = t.Dict({
        'id': t.Or(t.String | MongoId(allow_blank=True)),
        'patterns': t.List(t.String, min_length=1),
        'image_url': t.URL(allow_blank=True),
        'image_id': t.String(allow_blank=True),
        'text': t.String(allow_blank=True),
        'created_at': t.Int,
        'created_by': User.trafaret,
        t.Key('last_used', default=0): t.Int,
    }).make_optional('image_id', 'image_url', 'text', 'last_used')

    @classmethod
    @inject.params(db=AsyncIOMotorDatabase)
    def find_by_pattern(cls, patterns, db=None):
        return db[cls.collection].find({'patterns': {'$in': patterns}})

    @inject.params(db=AsyncIOMotorDatabase)
    def update_usage(self, db=None):
        epoch_now = int(time.time())
        return db[self.collection].update({'_id': self.id},
                                          {'$set': {
                                              'last_used': epoch_now
                                          }})

    @property
    @inject.params(config=Config)
    def on_hold(self, config=None):
        epoch_now = int(time.time())
        return self.last_used >= (epoch_now - config.reaction_threshold * 60)
Beispiel #6
0
import trafaret as t
from trafaret.contrib.object_id import MongoId
from trafaret.contrib.rfc_3339 import DateTime

user = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('username'): t.String(max_length=50),
    t.Key('email'): t.Email(),
    t.Key('pw_hash'): t.String(),
    # t.Key('first_name'): t.String(max_length=50),
    # t.Key('last_name'): t.String(max_length=50),
    # t.Key('created'): DateTime,
    # t.Key('active'): t.Buol,
})

message = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('author_id'): MongoId(),
    t.Key('username'): t.String(max_length=50),
    t.Key('text'): t.String(),
    t.Key('pub_date'): DateTime(),
    # t.Key('likes'): t.Int,
})

follower = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('who_id'): MongoId(),
    t.Key('whom_id'): t.List(MongoId()),
})

Beispiel #7
0
import trafaret as t
from trafaret.contrib.object_id import MongoId

user = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('username'): t.String(max_length=50),
    t.Key('pw_hash'): t.String(),
})

link = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('user_id'): MongoId(),
    t.Key('url'): t.String(),
    t.Key('filename'): t.String(),
    t.Key('file_id'): t.String(),
})


async def get_user_id(user_collection, username):
    rv = await (user_collection.find_one({'username': username}, {'_id': 1}))
    return rv['_id'] if rv else None
Beispiel #8
0
import trafaret as t
from trafaret.contrib.object_id import MongoId

car = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('vin_code'): t.String(),
    t.Key('manufacturer'): t.String(),
    t.Key('model'): t.String(),
    t.Key('year_created'): t.Date(format='%Y'),
    t.Key('colour'): t.String(),
})


async def get_car_by_vin_code(car_collection, vin_code):
    car_detail = await car_collection.find_one({'vin_code': vin_code})
    return car_detail


async def delete_car_by_vin_code(car_collection, vin_code):
    car_detail = await car_collection.delete_one({'vin_code': vin_code})
    return car_detail
Beispiel #9
0
from datetime import datetime as dt
from app.security import generate_password_hash
import asyncpgsa
import aiomysql
from sqlalchemy import (
    MetaData, Table, Column, ForeignKey,
    Integer, String, DateTime
)
from sqlalchemy.sql import select, and_, or_

import trafaret as t
from trafaret.contrib.object_id import MongoId

sufferers = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('username'): t.String(max_length=50),
    t.Key('email'): t.Email
})

reports = t.Dict({
    t.Key('_id'): MongoId(),
    t.Key('sufferer_id'): MongoId(),
    t.Key('username'): t.String(max_length=50),
    t.Key('encrypted_text'): t.Bytes(),
    t.Key('private_key'): t.Bytes()

})

metadata = MetaData()

users = Table(
Beispiel #10
0
 def test_bad_id(self):
     c = MongoId(allow_blank=True)
     res = extract_error(c, 123)
     assert res == "value is not ObjectId"
     assert isinstance(c.check(None), ObjectId)
Beispiel #11
0
from functools import partial

import trafaret as t
from trafaret.contrib.object_id import MongoId

NotEmptyString = partial(t.String, min=1)

GetTagsTrafaret = t.Dict({t.Key("text"): t.String})

TagsOutputTrafaret = t.Dict({
    t.Key("tags"):
    t.List(t.Dict({
        t.Key("_id"): MongoId() >> str,
        t.Key("tag"): t.String(),
    }),
           min_length=1),
})