<s> from peewee import * <EOL> from . basemodel import BaseModel <EOL> from . user import User <EOL> NO_PERM = <NUM_LIT:0> <EOL> READ_PERM = <NUM_LIT:1> <EOL> WRITE_PERM = <NUM_LIT:2> <EOL> ADMIN_PERM = <NUM_LIT:3> <EOL> PERM_CHOICES = ( <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ) <EOL> PERM_MAP = { name : num for num , name in PERM_CHOICES } <EOL> class Permission ( BaseModel ) : <EOL> user = ForeignKeyField ( User , related_name = "<STR_LIT>" ) <EOL> perm_name = IntegerField ( choices = PERM_CHOICES ) <EOL> target_id = IntegerField ( ) <EOL> level = IntegerField ( <EOL> choices = ( <EOL> ( NO_PERM , '<STR_LIT>' ) , <EOL> ( READ_PERM , '<STR_LIT>' ) , <EOL> ( WRITE_PERM , '<STR_LIT>' ) , <EOL> ( ADMIN_PERM , '<STR_LIT>' ) , <EOL> ) ) <EOL> @ classmethod <EOL> def check_permission ( cls , target , user , level ) : <EOL> if not user : <EOL> return target . anonymous_perm >= level <EOL> try : <EOL> return cls . select ( ) . where ( <EOL> cls . user == user , <EOL> cls . perm_name == target . perm_name , <EOL> cls . target_id == target . id , <EOL> cls . level >= level ) . get ( ) <EOL> except cls . DoesNotExist : <EOL> return None <EOL> @ classmethod <EOL> def add_permission ( self , target , user , level ) : <EOL> return cls . create ( <EOL> user = user , <EOL> perm_name = target . perm_name , <EOL> target_id = target . id , <EOL> level = level , <EOL> ) </s>
