def update_from_json(cls, data): PROPNAME_MAPPING = EntityProp.map_name_id() eid = None PROP_MAPPING = { 'image': lambda s, _eid, _id, _val: [PropMedia.delete(_eid, _id, False), PropMedia(_eid, _id, cls.convert_media_value_to_media_item('image', _eid, _val)). add_or_update(session=s, no_commit=True)], 'priority': lambda s, _eid, _id, _val: PropInt(eid, _id, _val).add_or_update(session=s, no_commit=True) } if 'id' in data: with DBConnection() as session: eid = data['id'] entity = session.db.query(EntityNews).filter_by(eid=eid).all() fields = ['title', 'desc', 'text'] if len(entity): for _ in entity: for l in cls.locales: for f in fields: if f in data and l in data[f]: setattr(_, '%s_%s' % (f, l), data[f][l]) session.db.commit() if 'prop' in data: for prop_name, prop_val in data['prop'].items(): if prop_name in PROPNAME_MAPPING and prop_name in PROP_MAPPING: PROP_MAPPING[prop_name](session, eid, PROPNAME_MAPPING[prop_name], prop_val) session.db.commit() return eid
def delete_wide_object(cls, eid): PROPNAME_MAPPING = EntityProp.map_name_id() PROP_MAPPING = { 'image': lambda _eid, _id: PropMedia.delete(_eid, _id, False), 'priority': lambda _eid, _id: PropInt.delete(_eid, _id, False) } for key, propid in PROPNAME_MAPPING.items(): if key in PROP_MAPPING: PROP_MAPPING[key](eid, propid)
def delete_wide_object(cls, eid): from each.Prop.PropGame import PropGame PROPNAME_MAPPING = EntityProp.map_name_id() PROP_MAPPING = { 'game': lambda _eid, _id: PropGame.delete_value(_eid, False), 'image': lambda _eid, _id: PropMedia.delete(_eid, _id, False), 'scenario': lambda _eid, _id: PropScenario.delete(_eid, _id, False), 'rating': lambda _eid, _id: PropLike.delete(_eid, _id, False), 'comment': lambda _eid, _id: PropComment.delete(_eid, _id, False) } for key, propid in PROPNAME_MAPPING.items(): if key in PROP_MAPPING: PROP_MAPPING[key](eid, propid)
def get_wide_object(cls, eid, items=[]): def rating(_eid, propid): with DBConnection() as session: likes = session.db.query(PropLike, EntityLike). \ filter(PropLike.eid == _eid). \ filter(PropLike.propid == propid).filter(PropLike.value == EntityLike.eid).all() if len(likes): rate = 0 for _ in likes: rate += _[1].weight rate /= len(likes) return rate return 0 def comment(_eid, propid): from each.Entities.EntityUser import EntityUser with DBConnection() as session: comments = session.db.query(EntityComment, EntityUser). \ join(EntityUser, EntityUser.eid == EntityComment.userid). \ filter(PropComment.eid == _eid). \ filter(PropComment.propid == propid). \ filter(EntityComment.eid == PropComment.value).order_by(EntityComment.created.desc()).all() if len(comments): return [{'eid': _[0].eid, 'user': _[1].to_dict(['name', 'email']), 'comment': _[0].to_dict(['text', 'created'])} for _ in comments] return [] PROPNAME_MAPPING = EntityProp.map_name_id() PROP_MAPPING = { 'image': lambda _eid, _id: PropMedia.get_object_property(_eid, _id, ['eid', 'url']), 'scenario': lambda _eid, _id: PropScenario.get_object_property(_eid, _id, ['eid']), 'rating': rating, 'comment': comment } result = { 'eid': eid } for key, propid in PROPNAME_MAPPING.items(): if key in PROP_MAPPING and (not len(items) or key in items): result.update({key: PROP_MAPPING[key](eid, propid)}) return result
def get_wide_object(cls, eid, items=[]): PROPNAME_MAPPING = EntityProp.map_name_id() PROP_MAPPING = { 'image': lambda _eid, _id: PropMedia.get_object_property(_eid, _id, ['eid', 'url']), 'priority': lambda _eid, _id: [_.value for _ in PropInt.get().filter_by(eid=_eid, propid=_id).all() or [PropInt(0, 0, 0)]] } result = { 'eid': eid } for key, propid in PROPNAME_MAPPING.items(): if key in PROP_MAPPING and (not len(items) or key in items): result.update({key: PROP_MAPPING[key](eid, propid)}) return result
def mediaPropMapping(_eid, _id): PropMedia.delete(_eid, _id, False)
def mediaPropMapping(_eid, _id): return PropMedia.get_object_property(_eid, _id, ['eid', 'url'])
def mediaPropMapping(s, _eid, _id, _val): PropMedia.delete(_eid, _id, False) PropMedia(eid, _id, cls.convert_media_value_to_media_item('image', _eid, _val))\ .add_or_update(session=s, no_commit=True)