def _add_all(objs, data): #if present, updating objects with some fields that are not loaded #by default by schemas id_field = '{}_id'.format(objs[0].__class__.__name__.lower()) for obj, dct in zip(objs, data): if 'created_at' in dct: obj.created_at = get_datetime(dct['created_at']) if 'updated_at' in dct: obj.updated_at = get_datetime(dct['updated_at']) if id_field in dct: setattr(obj, id_field, dct[id_field]) db.session.flush() db.session.add_all(objs) db.session.commit()
def check_post_time_interval(user, model_cls): if BypassAntiFlood.is_granted(user): return last_post = order_query(model_cls.query.filter_by(user_id=user.user_id), by='newest').first() if last_post is None: return diff_secs = (get_now() - get_datetime(last_post.created_at)).total_seconds() if int(diff_secs) < current_app.config['MIN_POST_TIME_INTERVAL']: raise TooManyRequests('not outside minimum time interval for posting')
def deserialize(data, model_cls=None): new_dct = {} for k, v in data.items(): if k.endswith('_id') or k.startswith('n_'): new_dct[k] = int(v) elif k.endswith('_at'): new_dct[k] = get_datetime(v) elif k == 'password': new_dct[k] = generate_hash(v) else: new_dct[k] = str(v) obj = new_dct if model_cls is None else model_cls(**new_dct) return obj
def _deserialize(self, value, attr, obj, **kwargs): if not value: return super()._deserialize(value, attr, obj, **kwargs) return get_datetime(value)
def _serialize(self, value, attr, obj, **kwargs): if value is None: return super()._serialize(value, attr, obj, **kwargs) return get_datetime(value).isoformat()