class VerificationToken(koalacore.Resource): token_uid = koalacore.ResourceProperty(title=u'Token UID') token = koalacore.ResourceProperty(title=u'Token') expires = koalacore.ResourceProperty(title=u'Token Expires') redirect_uri = koalacore.ResourceProperty(title=u'Token Redirect URI') def __init__(self, **kwargs): if 'token' not in kwargs or kwargs['token'] is None: token = generate_client_id(length=32, chars=UNICODE_ASCII_CHARACTER_SET) kwargs['token'] = token kwargs['uid'] = token expires_in = 720 if 'expires_in' in kwargs: expires_in = int(kwargs['expires_in']) del kwargs['expires_in'] if 'expires' not in kwargs or kwargs['expires'] is None: kwargs['expires'] = datetime.datetime.utcnow() + datetime.timedelta(minutes=expires_in) super(VerificationToken, self).__init__(**kwargs) def is_expired(self): """ Check token expiration with timezone awareness """ return datetime.datetime.utcnow() >= self.expires
class TestModel(koalacore.Resource): example = koalacore.ResourceProperty(title='Example') random_property = koalacore.ResourceProperty(title='Random') computed = koalacore.ComputedResourceProperty( title='Computed', compute_function=lambda entity: u'{}{}'.format( (entity.example or ''), (entity.random_property or '')))
class User(BaseUser): groups = koalacore.ResourceProperty(title=u'Groups') session = koalacore.ResourceProperty(title=u'Session') permissions = koalacore.ResourceProperty(title=u'Permissions') def __init__(self, **kwargs): # Unfortunately, we can't set mutable defaults on a resource property if 'groups' not in kwargs or kwargs['groups'] is None: # Groups should be stored in the format group_uid: group_name kwargs['groups'] = {} if 'session' not in kwargs or kwargs['session'] is None: kwargs['session'] = SessionStorage() if 'permissions' not in kwargs or kwargs['permissions'] is None: if 'roles' in kwargs: roles = kwargs['roles'] del kwargs['roles'] else: roles = {'user'} if 'acl' in kwargs: acl = kwargs['acl'] del kwargs['acl'] else: acl = { 'self': {'read_profile', 'update_profile', 'change_password'} } kwargs['permissions'] = koalacore.PermissionsStorage(roles=roles, acl=acl) super(User, self).__init__(**kwargs) def to_search_doc(self): base_fields = super(User, self).to_search_doc() roles = [ koalacore.GAESearchInterface.atom_field(name='role', value=role) for role in self.permissions.roles ] groups = [ koalacore.GAESearchInterface.atom_field(name='group_uid', value=group_uid) for group_uid, details in self.groups.iteritems() ] group_key_ids = [ koalacore.GAESearchInterface.atom_field( name='group_key_id', value=str(ndb.Key(urlsafe=group_uid).id())) for group_uid, details in self.groups.iteritems() ] return base_fields + roles + groups + group_key_ids
class BaseUserGroup(koalacore.Resource): group_uid = koalacore.ResourceProperty(title=u'Group UID', immutable=True) user_uid = koalacore.ResourceProperty(title=u'User UID', immutable=True) group_name = koalacore.ResourceProperty(title=u'Group Name') def to_search_doc(self): return [ koalacore.GAESearchInterface.atom_field(name='group_uid', value=self.group_uid), koalacore.GAESearchInterface.atom_field(name='group_name', value=self.group_name), koalacore.GAESearchInterface.atom_field(name='user_uid', value=self.user_uid), ]
class User(koalacore.Resource): permissions = koalacore.ResourceProperty(title=u'Permissions') def __init__(self, **kwargs): if 'permissions' not in kwargs or kwargs['permissions'] is None: kwargs['permissions'] = koalacore.PermissionsStorage() super(User, self).__init__(**kwargs)
class Order(koalacore.Resource): # Order order_reference = koalacore.ResourceProperty(title=u'Order Reference') order_type = koalacore.ResourceProperty(title=u'Order Type', default='default') order_status = koalacore.ResourceProperty(title=u'Order Status', default='INVOICED') order_started = koalacore.ResourceProperty(title=u'Order Started') order_complete = koalacore.ResourceProperty(title=u'Order Complete') # Customer customer_uid = koalacore.ResourceProperty(title=u'Customer UID') user_uid = koalacore.ResourceProperty(title=u'User UID') customer_first_name = koalacore.ResourceProperty( title=u'Customer First Name') customer_last_name = koalacore.ResourceProperty( title=u'Customer Last Name') customer_company_name = koalacore.ResourceProperty( title=u'Customer Company Name') customer_email = koalacore.ResourceProperty(title=u'Customer Email') customer_phone = koalacore.ResourceProperty(title=u'Customer Phone') customer_turnover_ltm = koalacore.ResourceProperty( title=u'Customer Turnover < £1M') customer_tax_number = koalacore.ResourceProperty( title=u'Customer Tax Number') customer_membership_number = koalacore.ResourceProperty( title=u'Customer Membership Number') customer = koalacore.ComputedResourceProperty( title=u'Customer', compute_function=lambda resource: Customer( resource.customer_uid, resource.customer_first_name, resource. customer_last_name, resource.customer_email, resource.user_uid, resource.customer_company_name, resource.customer_phone, resource. customer_turnover_ltm, resource.customer_tax_number, resource. customer_membership_number)) # Delivery Address delivery_address_1 = koalacore.ResourceProperty( title=u'Delivery Address 1') delivery_address_2 = koalacore.ResourceProperty( title=u'Delivery Address 2') delivery_city = koalacore.ResourceProperty(title=u'Delivery City') delivery_country = koalacore.ResourceProperty(title=u'Delivery Country') delivery_state = koalacore.ResourceProperty(title=u'Delivery State') delivery_post_code = koalacore.ResourceProperty( title=u'Delivery Post Code') delivery_address = koalacore.ComputedResourceProperty( title=u'Delivery Address', compute_function=lambda resource: Address(resource.delivery_country, resource.delivery_city, resource. delivery_address_1, resource.delivery_address_2, resource. delivery_state, resource.delivery_post_code)) # Billing Address billing_address_1 = koalacore.ResourceProperty(title=u'Billing Address 1') billing_address_2 = koalacore.ResourceProperty(title=u'Billing Address 2') billing_city = koalacore.ResourceProperty(title=u'Billing City') billing_country = koalacore.ResourceProperty(title=u'Billing Country') billing_state = koalacore.ResourceProperty(title=u'Billing State') billing_post_code = koalacore.ResourceProperty(title=u'Billing Post Code') billing_address = koalacore.ComputedResourceProperty( title=u'Billing Address', compute_function=lambda resource: Address(resource.billing_country, resource.billing_city, resource. billing_address_1, resource.billing_address_2, resource. billing_state, resource.billing_post_code)) # Payment basket = koalacore.ResourceProperty(title=u'Basket') total = koalacore.ResourceProperty(title=u'Order Total') currency = koalacore.ResourceProperty(title=u'Currency') payment_details = koalacore.ResourceProperty(title=u'Payment Details') payment_completed_by = koalacore.ResourceProperty( title=u'Completed By UID') payment_completed_by_name = koalacore.ResourceProperty( title=u'Completed By') def __init__(self, **kwargs): # We only want to generate a new reference if one is missing or if order is not complete. if 'order_reference' not in kwargs or not kwargs['order_reference']: kwargs['order_reference'] = self.generate_order_reference( order_type=kwargs.get('order_type', 'default')) if 'basket' not in kwargs or not isinstance(kwargs['basket'], Basket): kwargs['basket'] = Basket() super(Order, self).__init__(**kwargs) @staticmethod def generate_order_reference(order_type): random_token = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(5) ]) return '{}{}-{}{}'.format( order_type, datetime.datetime.now().strftime("%y"), random_token, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) def update_order_reference(self): self.order_reference = self.generate_order_reference( order_type=self.order_type) def validate(self): valid = True if not self.uid: valid = False if not self.order_reference: valid = False if not self.customer_uid: valid = False if not self.customer_first_name: valid = False if not self.customer_last_name: valid = False if not self.customer_email: valid = False if not self.delivery_address_1: valid = False if not self.delivery_city: valid = False if not self.delivery_country: valid = False if not self.billing_address_1: valid = False if not self.billing_city: valid = False if not self.billing_country: valid = False if not self.basket: valid = False elif self.basket and not self.basket.count(): valid = False return valid def to_search_doc(self): payment_method = 'n/a' if self.payment_details is not None: payment_method = self.payment_details.method if self.payment_details is not None and (isinstance( self.payment_details.timestamp, datetime.date) or isinstance( self.payment_details.timestamp, datetime.datetime)): payment_date = [ koalacore.GAESearchInterface.date_field( name='payment_date', value=self.payment_details.timestamp), koalacore.GAESearchInterface.atom_field( name='payment_year', value=str(self.payment_details.timestamp.year)), koalacore.GAESearchInterface.atom_field( name='payment_month', value=str(self.payment_details.timestamp.month)), ] else: payment_date = [ koalacore.GAESearchInterface.atom_field(name='payment_date', value='NA') ] if isinstance(self.order_started, datetime.date) or isinstance( self.order_started, datetime.datetime): started_date = [ koalacore.GAESearchInterface.date_field( name='started_date', value=self.order_started), koalacore.GAESearchInterface.atom_field( name='started_year', value=str(self.order_started.year)), koalacore.GAESearchInterface.atom_field( name='started_month', value=str(self.order_started.month)), ] else: started_date = [ koalacore.GAESearchInterface.atom_field(name='started_date', value='NA'), koalacore.GAESearchInterface.atom_field(name='started_year', value='NA'), koalacore.GAESearchInterface.atom_field(name='started_month', value='NA'), ] total = 0 if self.basket is not None and self.basket.count(): basket_total = self.basket.get_total() total = float(basket_total.gross) return [ koalacore.GAESearchInterface.atom_field( name='order_reference', value=self.order_reference), koalacore.GAESearchInterface.atom_field(name='order_type', value=self.order_type), koalacore.GAESearchInterface.atom_field(name='order_status', value=self.order_status), koalacore.GAESearchInterface.atom_field( name='order_complete', value='Yes' if self.order_complete else 'No'), koalacore.GAESearchInterface.atom_field(name='customer_uid', value=self.customer_uid), koalacore.GAESearchInterface.atom_field(name='user_uid', value=self.user_uid), koalacore.GAESearchInterface.atom_field( name='customer_first_name', value=self.customer_first_name), koalacore.GAESearchInterface.atom_field( name='customer_last_name', value=self.customer_last_name), koalacore.GAESearchInterface.atom_field( name='customer_company_name', value=self.customer_company_name), koalacore.GAESearchInterface.atom_field(name='customer_email', value=self.customer_email), koalacore.GAESearchInterface.atom_field(name='customer_phone', value=self.customer_phone), koalacore.GAESearchInterface.atom_field( name='customer_turnover_ltm', value='Yes' if self.customer_turnover_ltm else 'No'), koalacore.GAESearchInterface.atom_field( name='customer_tax_number', value=self.customer_tax_number if self.customer_tax_number else 'No'), koalacore.GAESearchInterface.atom_field( name='customer_membership_number', value=self.customer_membership_number if self.customer_membership_number else 'No'), koalacore.GAESearchInterface.atom_field( name='delivery_address_1', value=self.delivery_address_1), koalacore.GAESearchInterface.atom_field( name='delivery_address_2', value=self.delivery_address_2), koalacore.GAESearchInterface.atom_field(name='delivery_city', value=self.delivery_city), koalacore.GAESearchInterface.atom_field( name='delivery_country', value=self.delivery_country), koalacore.GAESearchInterface.atom_field(name='delivery_state', value=self.delivery_state), koalacore.GAESearchInterface.atom_field( name='delivery_post_code', value=self.delivery_post_code), koalacore.GAESearchInterface.atom_field( name='billing_address_1', value=self.billing_address_1), koalacore.GAESearchInterface.atom_field( name='billing_address_2', value=self.billing_address_2), koalacore.GAESearchInterface.atom_field(name='billing_city', value=self.billing_city), koalacore.GAESearchInterface.atom_field( name='billing_country', value=self.billing_country), koalacore.GAESearchInterface.atom_field(name='billing_state', value=self.billing_state), koalacore.GAESearchInterface.atom_field( name='billing_post_code', value=self.billing_post_code), koalacore.GAESearchInterface.number_field( name='total', value=total if total else 0), koalacore.GAESearchInterface.atom_field(name='currency', value=self.currency), koalacore.GAESearchInterface.atom_field(name='payment_method', value=payment_method), koalacore.GAESearchInterface.atom_field( name='payment_completed_by', value=self.payment_completed_by), koalacore.GAESearchInterface.atom_field( name='payment_completed_by_name', value=self.payment_completed_by_name), koalacore.GAESearchInterface.text_field( name='fuzzy_order_reference', value=koalacore.generate_autocomplete_tokens( original_string=self.order_reference)), koalacore.GAESearchInterface.text_field( name='fuzzy_customer_first_name', value=koalacore.generate_autocomplete_tokens( original_string=self.customer_first_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_customer_last_name', value=koalacore.generate_autocomplete_tokens( original_string=self.customer_last_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_customer_company_name', value=koalacore.generate_autocomplete_tokens( original_string=self.customer_company_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_payment_completed_by_name', value=koalacore.generate_autocomplete_tokens( original_string=self.payment_completed_by_name)), ] + payment_date + started_date
class UserGroup(BaseUserGroup): action_set = koalacore.ResourceProperty(title=u'ACL')
class BaseUser(koalacore.Resource): username = koalacore.ResourceProperty(title=u'Username', unique=True, force_lowercase=True) email_address = koalacore.ResourceProperty(title=u'Email', unique=True, force_lowercase=True) first_name = koalacore.ResourceProperty(title=u'First Name') last_name = koalacore.ResourceProperty(title=u'Last Name') password = koalacore.ResourceProperty(title=u'Password', strip_whitespace=False) raw_password = koalacore.ResourceProperty( title=u'Raw Password', strip_whitespace=False) # Remove? language_preference = koalacore.ResourceProperty( title=u'Language Preference') recovery_email_address = koalacore.ResourceProperty( title=u'Recovery Email', unique=True, force_lowercase=True) email_address_verified = koalacore.ResourceProperty( title=u'Email Verified', default=False) recovery_email_address_verified = koalacore.ResourceProperty( title=u'Recovery Email Verified', default=False) def __init__(self, password=None, raw_password=None, **kwargs): assert password or raw_password, 'Must supply either hashed password or raw password to be hashed.' if raw_password: kwargs['password'] = self._hash_password(raw_password=raw_password) else: kwargs['password'] = password super(BaseUser, self).__init__(**kwargs) @staticmethod def _hash_password(raw_password): return koalacore.generate_password_hash(raw_password, salt_length=12) @property def auth_ids(self): return [self.username, self.email_address] def change_password(self, raw_password): """Sets the password for the current user :param raw_password: The raw password which will be hashed and stored """ self.password = self._hash_password(raw_password=raw_password) def to_search_doc(self): return [ koalacore.GAESearchInterface.text_field( name='fuzzy_username', value=koalacore.generate_autocomplete_tokens( original_string=self.username)), koalacore.GAESearchInterface.text_field( name='fuzzy_email_address', value=koalacore.generate_autocomplete_tokens( original_string=self.email_address)), koalacore.GAESearchInterface.text_field( name='fuzzy_recovery_email_address', value=koalacore.generate_autocomplete_tokens( original_string=self.recovery_email_address)), koalacore.GAESearchInterface.text_field( name='fuzzy_first_name', value=koalacore.generate_autocomplete_tokens( original_string=self.first_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_last_name', value=koalacore.generate_autocomplete_tokens( original_string=self.last_name)), koalacore.GAESearchInterface.date_field(name='created', value=self.created), koalacore.GAESearchInterface.date_field(name='updated', value=self.updated), koalacore.GAESearchInterface.atom_field(name='username', value=self.username), koalacore.GAESearchInterface.atom_field(name='email_address', value=self.email_address), koalacore.GAESearchInterface.atom_field( name='recovery_email_address', value=self.recovery_email_address), koalacore.GAESearchInterface.atom_field(name='first_name', value=self.first_name), koalacore.GAESearchInterface.atom_field(name='last_name', value=self.last_name), koalacore.GAESearchInterface.atom_field( name='language_preference', value=self.language_preference), koalacore.GAESearchInterface.atom_field( name='email_address_verified', value='Y' if self.email_address_verified else 'N'), koalacore.GAESearchInterface.atom_field( name='recovery_email_address_verified', value='Y' if self.recovery_email_address_verified else 'N'), ]
class Company(koalacore.Resource): company_name = koalacore.ResourceProperty(title=u'Company Name') contact_first_name = koalacore.ResourceProperty( title=u'Contact First Name') contact_last_name = koalacore.ResourceProperty(title=u'Contact Last Name') contact_email = koalacore.ResourceProperty(title=u'Contact Email') contact_phone = koalacore.ResourceProperty(title=u'Contact Phone') contact_mobile = koalacore.ResourceProperty(title=u'Contact Mobile') # Addresses delivery_address_1 = koalacore.ResourceProperty( title=u'Delivery Address 1') delivery_address_2 = koalacore.ResourceProperty( title=u'Delivery Address 2') delivery_address_3 = koalacore.ResourceProperty( title=u'Delivery Address 3') delivery_city = koalacore.ResourceProperty(title=u'Delivery City') delivery_county = koalacore.ResourceProperty(title=u'Delivery County') delivery_state = koalacore.ResourceProperty(title=u'Delivery State') delivery_post_code = koalacore.ResourceProperty( title=u'Delivery Post Code') delivery_country = koalacore.ResourceProperty(title=u'Delivery Country') billing_address_1 = koalacore.ResourceProperty(title=u'Billing Address 1') billing_address_2 = koalacore.ResourceProperty(title=u'Billing Address 2') billing_address_3 = koalacore.ResourceProperty(title=u'Billing Address 3') billing_city = koalacore.ResourceProperty(title=u'Billing City') billing_county = koalacore.ResourceProperty(title=u'Billing County') billing_state = koalacore.ResourceProperty(title=u'Billing State') billing_post_code = koalacore.ResourceProperty(title=u'Billing Post Code') billing_country = koalacore.ResourceProperty(title=u'Billing Country') def to_search_doc(self): return [ koalacore.GAESearchInterface.atom_field(name='company_name', value=self.company_name), koalacore.GAESearchInterface.atom_field( name='contact_first_name', value=self.contact_first_name), koalacore.GAESearchInterface.atom_field( name='contact_last_name', value=self.contact_last_name), koalacore.GAESearchInterface.atom_field(name='contact_email', value=self.contact_email), koalacore.GAESearchInterface.atom_field(name='contact_phone', value=self.contact_phone), koalacore.GAESearchInterface.atom_field(name='contact_mobile', value=self.contact_mobile), koalacore.GAESearchInterface.atom_field( name='delivery_address_1', value=self.delivery_address_1), koalacore.GAESearchInterface.atom_field( name='delivery_address_2', value=self.delivery_address_2), koalacore.GAESearchInterface.atom_field( name='delivery_address_3', value=self.delivery_address_3), koalacore.GAESearchInterface.atom_field(name='delivery_city', value=self.delivery_city), koalacore.GAESearchInterface.atom_field( name='delivery_county', value=self.delivery_county), koalacore.GAESearchInterface.atom_field(name='delivery_state', value=self.delivery_state), koalacore.GAESearchInterface.atom_field( name='delivery_post_code', value=self.delivery_post_code), koalacore.GAESearchInterface.atom_field( name='delivery_country', value=self.delivery_country), koalacore.GAESearchInterface.atom_field( name='billing_address_1', value=self.billing_address_1), koalacore.GAESearchInterface.atom_field( name='billing_address_2', value=self.billing_address_2), koalacore.GAESearchInterface.atom_field( name='billing_address_3', value=self.billing_address_3), koalacore.GAESearchInterface.atom_field(name='billing_city', value=self.billing_city), koalacore.GAESearchInterface.atom_field(name='billing_county', value=self.billing_county), koalacore.GAESearchInterface.atom_field(name='billing_state', value=self.billing_state), koalacore.GAESearchInterface.atom_field( name='billing_post_code', value=self.billing_post_code), koalacore.GAESearchInterface.atom_field( name='billing_country', value=self.billing_country), koalacore.GAESearchInterface.text_field( name='fuzzy_company_name', value=koalacore.generate_autocomplete_tokens( original_string=self.company_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_contact_first_name', value=koalacore.generate_autocomplete_tokens( original_string=self.contact_first_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_contact_last_name', value=koalacore.generate_autocomplete_tokens( original_string=self.contact_last_name)), koalacore.GAESearchInterface.text_field( name='fuzzy_contact_email', value=koalacore.generate_autocomplete_tokens( original_string=self.contact_email)), ]