<s> import datetime <EOL> import hashlib <EOL> import time <EOL> from flask_login import UserMixin <EOL> from daimaduan . bootstrap import db <EOL> class BaseDocument ( db . Document ) : <EOL> meta = { '<STR_LIT>' : True , <EOL> '<STR_LIT:strict>' : False } <EOL> def increase_counter ( self , field , count = <NUM_LIT:1> ) : <EOL> counter_field = '<STR_LIT>' % field <EOL> counter_value = getattr ( self , counter_field , <NUM_LIT:0> ) + count <EOL> if counter_value <= <NUM_LIT:0> : <EOL> counter_value = <NUM_LIT:0> <EOL> attributes = dict ( [ ( counter_field , counter_value ) ] ) <EOL> self . update ( ** attributes ) <EOL> @ classmethod <EOL> def find_or_create_by ( cls , ** attributes ) : <EOL> record = cls . objects ( ** attributes ) . first ( ) <EOL> if record is None : <EOL> record = cls ( ** attributes ) . save ( ) <EOL> return record <EOL> @ classmethod <EOL> def find_and_delete_by ( cls , ** attributes ) : <EOL> record = cls . objects ( ** attributes ) . first ( ) <EOL> if record : <EOL> record . delete ( ) <EOL> def create_hash_id ( self , salt , string ) : <EOL> if not self . hash_id : <EOL> def generate_hash_id ( ) : <EOL> return hashlib . sha1 ( '<STR_LIT>' % ( salt , string , str ( time . time ( ) ) ) ) . hexdigest ( ) [ : <NUM_LIT:11> ] <EOL> self . hash_id = generate_hash_id ( ) <EOL> while ( self . __class__ . objects ( hash_id = self . hash_id ) . first ( ) is not None ) : <EOL> self . hash_id = generate_hash_id ( ) <EOL> created_at = db . DateTimeField ( default = datetime . datetime . now ) <EOL> updated_at = db . DateTimeField ( default = datetime . datetime . now ) <EOL> class LoginManagerUser ( UserMixin ) : <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> self . id = user . id <EOL> self . username = user . username <EOL> self . email = user . email </s>
