Example #1
0
def test_filter():
    db.getdb().note.remove({})

    post('/note', {'type': 'diary', 'content': '1'}, cookies=cookies)
    post('/note', {'type': 'diary', 'content': '2'}, cookies=cookies)
    post('/note', {'type': 'book'}, cookies=cookies)
    post('/note', {'type': 'blog'}, cookies=cookies)

    post('/note', {'tags': ['.', 'foo']}, cookies=cookies)
    post('/note', {'tags': ['foo', 'bar']}, cookies=cookies)
    post('/note', {'tags': ['baz']}, cookies=cookies)

    describe('query book type')
    res = post('/query', {'type': 'book'}).json()
    assert len(res['notes']) == 1

    describe('query diary type')
    res = post('/query', {'type': 'diary'}).json()
    assert len(res['notes']) == 2

    describe('guest query not include hidden')
    res = post('/query', {'tags': 'foo'}).json()
    assert len(res['notes']) == 1

    describe('me query include hidden')
    res = post('/query', {'tags': 'foo'}, cookies=cookies).json()
    assert len(res['notes']) == 2
Example #2
0
def move(cover, localdisk, warcdisk):
    print "moving", cover.id, cover.filename
    data = localdisk.read(cover.filename)
    category = get_category(cover.category_id)
    params = {
        "subject_uri": "http://covers.openlibrary.org/%s/id/%d" % (category, cover.id),
        "olid": cover.olid,
        "ISBN": cover.isbn,
        "source": cover.source,
        "source_url": cover.source_url or "",
        "creation_date": cover.created.strftime("%Y%m%d%H%M%S"),
    }
    filename = warcdisk.write(data, params)
    print "filename", filename
    db.getdb().update("cover", where="id=$cover.id", archived=True, filename=filename, vars=locals())
Example #3
0
def dbfind(query):
    return db.getdb().note.find(query, {
        '_id': False
    }).sort([
        ('ctime', -1),
        ('id', -1),
    ])
Example #4
0
    def __init__(self, opts):

        #handlers = [
        #    (r'/point/test(.*)', TestHandler)
        #] + config.router + route.get_routes()

        self.ChatRouter = SockJSRouter(ChatConnection, '/chat')
        print 'self.ChatRouter=', dir(self.ChatRouter)
        print '_connection=', dir(self.ChatRouter._connection)
        handlers = [] + self.ChatRouter.urls

        # print ' Application:opts=', repr(opts)
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            # xsrf_cookies=True,
            login_url="/auth/login",
            autoescape=None  # ,
            #**{k: v.value() for k, v in opts.items()}
        )
        #settings.update(opts)
        for k, v in opts.items():
            settings[k] = v.value()
        #print 'settings=', repr(settings)
        web.Application.__init__(self, handlers, **settings)

        # Единое соединение с базой данных для всех обработчиков
        #self.db = base.DB.db(DB_URL, DB_REPLICASET)
        self.db = getdb(opts.mongodb_url, opts.mongodb_replicaset)

        # Синхронная работа через pymongo
        # self.syncdb = pymongo.Connection(DB_URL).navicc
        ioloop.PeriodicCallback(self.tick, 1000).start()
Example #5
0
def get_max_note_id():
    notes = db.getdb().note.find({}).sort('id', -1).limit(1)
    notes = list(notes)
    if not notes:
        return 0
    note = notes[0]
    return note['id']
Example #6
0
 def add_table(table, id_prefix="", type=""):
     for d in db.getdb().select(table):
         d.id = id_prefix + d.id
         d.type = type
         if table == "constituency":
             d.id = d.state + "/" + d.id
         add_to_index(d)
Example #7
0
def put_note(note):
    """
    insert or update note
    if 'id' is absent, create a new one
    'ctime' will be added to the note if not present
    return the added note

    Example:

        put_note({'content': 'test'})
        => {'content': 'test', 'id': 1, 'ctime': '2018-05-09 22:02:17 UTC'}
    """
    if 'id' not in note:
        note['id'] = new_note_id()
    if 'ctime' not in note:
        note['ctime'] = util.utcnow_as_str()
    db.getdb().note.update({'id': note['id']}, note, upsert=True)
    return note
Example #8
0
def get_note(note_id):
    """
    return note with id == <note_id>
    if not found, return None

    Example:

        get_note(328)
        => {'content': 'test', 'id': 328, 'ctime': '2018-05-09 22:25:34 UTC'}
    """
    return db.getdb().note.find_one({'id': note_id}, {'_id': False})
Example #9
0
def archive(localdisk, warcdisk):
    covers = db.getdb().select("cover", where="archived=false", order="id")
    for cover in covers:
        try:
            move(cover, localdisk, warcdisk)
            path = os.path.join(localdisk.root, cover.filename)
            os.remove(path)
        except:
            print "failed to move", cover.filename
            import traceback

            traceback.print_exc()
Example #10
0
def ensure_thumbnail_created(id, path):
    """Temporary hack during migration to make sure thumbnails are created."""
    if ':' in path or path.endswith('.jpg') or os.path.exists(path + '-S.jpg'):
        return

    # original file is not present. Can't create thumbnails.
    if not os.path.exists(path):
        return

    prefix = os.path.basename(path)
    assert not prefix.endswith('.jpg')

    write_image(open(path).read(), prefix)

    # write_image also writes the original image to a new file. Delete it as we already have the original image.
    os.remove(path + '.jpg')
    db.getdb().update('cover', where='id=$id', vars=locals(),
        filename_s=prefix + '-S.jpg', 
        filename_m=prefix + '-M.jpg', 
        filename_l=prefix + '-L.jpg', 
    )
Example #11
0
def test_basic():
    db.getdb().note.remove({})

    describe('guest post note')
    assert post('/note', {'foo': 'bar'}).status_code == 401

    describe('post note')
    r = post('/note', {'foo': 'bar'}, cookies=cookies)
    assert r.status_code == 200

    describe('verify note posted')
    r = get('/note/1')
    assert r.status_code == 200
    note = r.json()
    assert 'ctime' in note
    assert 'mtime' in note
    assert note['id'] == 1
    assert note['foo'] == 'bar'

    describe('guest put note')
    r = put('/note/1', {'foo': '!!!'})
    assert r.status_code == 401

    describe('put note')
    r = put('/note/1', {'foo': '!!!'}, cookies=cookies)
    assert r.status_code == 200

    describe('verify note changed')
    r = get('/note/1')
    assert r.json()['foo'] == '!!!'

    describe('guest delete')
    assert delete('/note/1').status_code == 401

    describe('delete')
    assert delete('/note/1', cookies=cookies).status_code == 200

    describe('verify deleted')
    assert get('/note/1').status_code == 404
Example #12
0
def test_hidden():
    db.getdb().note.remove({})

    describe('post a hidden note')
    r = post('/note', {'foo': 'bar', 'tags': ['.', 't']}, cookies=cookies)
    assert r.status_code == 200

    describe('guest should got 404')
    r = get('/note/1')
    assert r.status_code == 404

    describe('me should got fine')
    r = get('/note/1', cookies=cookies)
    assert r.status_code == 200

    describe('guest query should not include hidden')
    r = post('/query', {})
    assert r.json()['notes'] == []

    describe('me query should include hidden')
    r = post('/query', {}, cookies=cookies)
    assert len(r.json()['notes']) == 1
Example #13
0
def get_items(date=None, count=None):
    query = {}

    if date:
        query['date'] = date

    r = db.getdb().balance.find(query, {'_id': False})
    r.sort([('date', -1), ('ctime', 1)])

    if count:
        count = to_int(count)
        if count:
            r.limit(count)

    return list(r)
Example #14
0
def ensure_thumbnail_created(id, path):
    """Temporary hack during migration to make sure thumbnails are created."""
    if ':' in path or path.endswith('.jpg') or os.path.exists(path + '-S.jpg'):
        return

    # original file is not present. Can't create thumbnails.
    if not os.path.exists(path):
        return

    prefix = os.path.basename(path)
    assert not prefix.endswith('.jpg')

    write_image(open(path).read(), prefix)

    # write_image also writes the original image to a new file. Delete it as we already have the original image.
    os.remove(path + '.jpg')
    db.getdb().update(
        'cover',
        where='id=$id',
        vars=locals(),
        filename_s=prefix + '-S.jpg',
        filename_m=prefix + '-M.jpg',
        filename_l=prefix + '-L.jpg',
    )
Example #15
0
def query_notes(query, page=None, size=None):
    if not page:
        page = 1
    if not size:
        size = 20

    r = db.getdb().note.find(query, {'_id': False})
    total = r.count()
    r.sort('ctime', -1)
    r.skip((page - 1) * size).limit(size)
    return {
        'page': page,
        'size': size,
        'total': total,
        'notes': list(r),
    }
Example #16
0
    def __init__(self, opts):
        handlers = [
            (r'/point/test(.*)', TestHandler)
        ] + config.router + route.get_routes()
        # print ' Application:opts=', repr(opts)
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            # xsrf_cookies=True,
            login_url="/auth/login",
            autoescape=None  # ,
            #**{k: v.value() for k, v in opts.items()}
        )
        #settings.update(opts)
        for k, v in opts.items():
            settings[k] = v.value()
        print 'settings=', repr(settings)
        web.Application.__init__(self, handlers, **settings)

        # Единое соединение с базой данных для всех обработчиков
        #self.db = base.DB.db(DB_URL, DB_REPLICASET)
        self.db = getdb(opts.mongodb_url, opts.mongodb_replicaset)
Example #17
0
def get_user(username):
    return db.getdb().user.find_one({
        'username': username,
    })
Example #18
0
def remove_user(username):
    r = db.getdb().user.remove({'username': username})
    return r['n'] == 1
Example #19
0
def create_user(user):
    user = dict(user)
    user.update({'_id': user['username']})
    r = db.getdb().user.insert_one(user)
    return r.acknowledged
Example #20
0
def test_pagination():
    db.getdb().note.remove({})

    now = datetime.datetime.now() - datetime.timedelta(days=365)
    for i in xrange(11):
        note = {
            'count': i + 1,
            'ctime': datetime.datetime.strftime(now, '%Y-%m-%d %H:%M:%S UTC'),
        }
        post('/note', note, cookies=cookies)
        now += datetime.timedelta(days=1)

    describe('default query')
    res = post('/query', {}).json()
    assert res['total'] == 11
    assert res['page'] == 1
    assert res['size'] == 20

    describe('query page 1')
    res = post('/query?page=1&size=5', {}).json()
    assert res['total'] == 11
    assert res['page'] == 1
    assert res['size'] == 5
    notes = res['notes']
    assert notes[0]['id'] == 11 and notes[-1]['id'] == 7

    describe('query page 2')
    res = post('/query?page=2&size=5', {}).json()
    assert res['total'] == 11
    assert res['page'] == 2
    assert res['size'] == 5
    notes = res['notes']
    assert notes[0]['id'] == 6 and notes[-1]['id'] == 2

    describe('query page 3')
    res = post('/query?page=3&size=5', {}).json()
    assert res['total'] == 11
    assert res['page'] == 3
    assert res['size'] == 5
    notes = res['notes']
    assert notes[0]['id'] == notes[-1]['id'] == 1

    describe('query page 4')
    res = post('/query?page=4&size=5', {}).json()
    assert res['total'] == 11
    assert res['page'] == 4
    assert res['size'] == 5
    assert not res['notes']

    describe('make some note hidden')
    note = get('/note/2').json()
    note.update({'tags': ['.']})
    put('/note/2', note, cookies=cookies)

    describe('guest query')
    res = post('/query', {}).json()
    assert res['total'] == 10
    assert len(res['notes']) == 10

    describe('me query')
    res = post('/query', {}, cookies=cookies).json()
    assert res['total'] == 11
    assert len(res['notes']) == 11
Example #21
0
def delete_note(note_id):
    r = db.getdb().note.remove({'id': note_id})
    return r['n'] == 1
Example #22
0
def save(note):
    data = {'_id': note['id']}
    data.update(note)
    r = db.getdb().note.update({'id': note['id']}, data, upsert=True)
    return r['n'] == 1
Example #23
0
def exists(note_id):
    return db.getdb().note.find_one({'id': note_id}) is not None
Example #24
0
def new_note_id():
    r = db.getdb().note.find().sort([('id', -1)]).limit(1)[0]
    if r:
        return r['id'] + 1
    else:
        return 1
Example #25
0
def clear_all():
    db.getdb().balance.remove({})
Example #26
0
def add_item(item):
    r = db.getdb().balance.insert_one(item)
    return r.acknowledged
Example #27
0
 def get(note_id):
     note = db.getdb().note.find_one({'id': note_id}, {'_id': False})
     if note:
         return Note(note)
Example #28
0
from db import getdb
db = getdb()
cursor = db.cursor()


class Pessoa:
    def __init__(self, nome=None, idade=None, cidade=None, id=None):
        self.nome = nome
        self.idade = idade
        self.cidade = cidade
        self.id = id

    def mostrarAtributos(self):
        print(
            f"\nID: {self.id}\nNome: {self.nome}\nIdade: {self.idade}\nCidade: {self.cidade}"
        )


class Cidade:
    def __init__(self, nome=None, cep=None, id=None):
        self.nome = nome
        self.cep = cep
        self.id = id

    def mostrarAtributos(self):
        print(f"\nID: {self.id}\nNome: {self.nome}\nCEP: {self.cep}")


class Sistema:
    def __init__(self):
        self.pessoas = list()
Example #29
0
def get_raw_txs():
    txs = db.getdb().txs.find({}).sort([('owner', 1), ('tx', 1)])
    txs = [u'{:8}  {}'.format(tx['owner'], tx['tx']) for tx in txs]
    return u'<pre>{}</pre>'.format('\n'.join(txs))
Example #30
0
def get_category(id):
    if id not in _cagegories:
        _cagegories[id] = db.getdb().select("category", where="id=$id", vars=locals())[0].name
    return _cagegories[id]