<s> from __future__ import unicode_literals <EOL> from billy . db import tables <EOL> from billy . models . base import BaseTableModel <EOL> from billy . models . base import decorate_offset_limit <EOL> from billy . utils . generic import make_guid <EOL> class PlanModel ( BaseTableModel ) : <EOL> TABLE = tables . Plan <EOL> types = tables . PlanType <EOL> frequencies = tables . PlanFrequency <EOL> @ decorate_offset_limit <EOL> def list_by_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> Company = tables . Company <EOL> Plan = tables . Plan <EOL> query = self . session . query ( Plan ) <EOL> if isinstance ( context , Company ) : <EOL> query = query . filter ( Plan . company == context ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( context ) ) <EOL> query = query . order_by ( Plan . created_at . desc ( ) ) <EOL> return query <EOL> def create ( <EOL> self , <EOL> company , <EOL> plan_type , <EOL> amount , <EOL> frequency , <EOL> interval = <NUM_LIT:1> , <EOL> external_id = None , <EOL> name = None , <EOL> description = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if interval < <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> now = tables . now_func ( ) <EOL> plan = tables . Plan ( <EOL> guid = '<STR_LIT>' + make_guid ( ) , <EOL> company = company , <EOL> plan_type = plan_type , <EOL> amount = amount , <EOL> frequency = frequency , <EOL> interval = interval , <EOL> external_id = external_id , <EOL> name = name , <EOL> description = description , <EOL> updated_at = now , <EOL> created_at = now , <EOL> ) <EOL> self . session . add ( plan ) <EOL> self . session . flush ( ) <EOL> return plan <EOL> def update ( self , plan , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> now = tables . now_func ( ) <EOL> plan . updated_at = now <EOL> for key in [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:description>' ] : <EOL> if key not in kwargs : <EOL> continue <EOL> value = kwargs . pop ( key ) <EOL> setattr ( plan , key , value ) <EOL> if kwargs : <EOL> raise TypeError ( '<STR_LIT>' . format ( tuple ( kwargs . keys ( ) ) ) ) <EOL> self . session . flush ( ) <EOL> def delete ( self , plan ) : <EOL> """<STR_LIT>""" <EOL> plan . deleted = True <EOL> self . session . flush ( ) </s>
