Beispiel #1
0
def _entity_delete(args: dict):
    # Delete from DB
    mongodb.get_collection(args['collection_name']).delete_one(
        {'_id': args['_id']})

    # Update cache
    _ENTITIES_CACHE.rm('{}.{}'.format(args['model'], args['_id']))
Beispiel #2
0
def plugin_update(v_from: _Version):
    # Field 'uid' added to users and roles
    if v_from <= '2.3':
        from pytsite import console, mongodb
        from plugins import odm

        for c in ('users', 'roles'):
            col = mongodb.get_collection(c)
            for d in col.find():
                col.update_one({'_id': d['_id']}, {'$set': {'uid': str(d['_id'])}})
                console.print_info('Document updated: {}:{}'.format(c, d['_id']))

        odm.clear_cache('role')
        odm.clear_cache('user')
        odm.reindex('role')
        odm.reindex('user')

    if v_from <= '3.2':
        import re
        from pytsite import console, mongodb
        from plugins import odm

        for c in ('users', 'roles'):
            col = mongodb.get_collection(c)
            for d in col.find():
                col.update_one({'_id': d['_id']}, {'$set': {'uid': d['_ref']}})
                console.print_info('Document updated: {}:{}'.format(c, d['_id']))

        odm.clear_cache('role')
        odm.clear_cache('user')
        odm.reindex('role')
        odm.reindex('user')

        db_obj_id_re = re.compile('[a-z:]+([0-9a-f]{24})$')
        for m in odm.get_registered_models():
            mock = odm.dispense(m)
            for f_name, f in mock.fields.items():
                for d in mock.collection.find():
                    f_new_value = None

                    if isinstance(f, field.User) and d[f_name]:
                        f_new_value = '{}:{}'.format('user', db_obj_id_re.sub('\\1', d[f_name]))

                    if isinstance(f, (field.Users, field.Roles)) and d[f_name]:
                        auth_model = 'role' if isinstance(f, field.Roles) else 'user'
                        f_new_value = ['{}:{}'.format(auth_model, db_obj_id_re.sub('\\1', v)) for v in d[f_name]]

                    if f_new_value:
                        mock.collection.update_one({'_id': d['_id']}, {'$set': {f_name: f_new_value}})
                        console.print_info('Document updated: {}:{}'.format(m, d['_id']))

            odm.clear_cache(m)

    if v_from <= '3.4':
        from plugins import odm

        odm.reindex('role')
        odm.reindex('user')
Beispiel #3
0
def plugin_update(v_from: _Version):
    from pytsite import console

    if v_from < '1.4':
        from pytsite import mongodb

        for collection_name in mongodb.get_collection_names():
            collection = mongodb.get_collection(collection_name)
            for doc in collection.find():
                if '_ref' in doc:
                    continue

                doc['_ref'] = '{}:{}'.format(doc['_model'], doc['_id'])
                collection.replace_one({'_id': doc['_id']}, doc)
                console.print_info('Document {} updated'.format(doc['_ref']))

    if v_from < '1.6':
        console.run_command('odm:reindex')

    if v_from < '4.0':
        # Update all entities that have `Ref` and `RefsList` fields
        for m in get_registered_models():
            console.print_info("Processing model '{}'".format(m))
            fields_to_update = []
            for f in dispense(m).fields.values():
                if f.name != '_parent' and isinstance(
                        f, (field.Ref, field.RefsList)):
                    fields_to_update.append(f.name)

            if fields_to_update:
                for e in find(m).get():
                    for f_name in fields_to_update:
                        e.f_set(f_name, e.f_get(f_name))

                    e.save(update_timestamp=False)
                    console.print_info('Entity {} updated, fields {}'.format(
                        e, fields_to_update))

    if v_from < '6.0':
        console.run_command('odm:reindex')
Beispiel #4
0
def _entity_save(args: dict):
    """Save an entity
    """
    fields_data = args['fields_data']
    collection = mongodb.get_collection(args['collection_name'])

    # Save data to the database
    try:
        # New entity
        if args['is_new']:
            collection.insert_one(fields_data)

        # Existing entity
        else:
            collection.replace_one({'_id': fields_data['_id']}, fields_data)

        # Update cache
        c_key = '{}.{}'.format(fields_data['_model'], fields_data['_id'])
        _ENTITIES_CACHE.put_hash(c_key, fields_data, _CACHE_TTL)

    except (bson_errors.BSONError, PyMongoError) as e:
        logger.error(e)
        logger.error('Document dump: {}'.format(fields_data))
        raise e
Beispiel #5
0
def plugin_update(v_from: _Version):
    if v_from < '4.20':
        from pytsite import mongodb

        mongodb.get_collection('content_model_entities').drop()
Beispiel #6
0
 def collection(self) -> Collection:
     """Get entity's DB collection
     """
     return mongodb.get_collection(self._collection_name)