pfunk.contrib.ecommerce.collections
View Source
import stripe from envs import env from pfunk.collection import Collection from pfunk.contrib.auth.collections import User, Group from pfunk.exceptions import DocNotFound from pfunk.fields import EmailField, SlugField, ManyToManyField, ListField, ReferenceField, StringField, EnumField, FloatField from pfunk.contrib.auth.resources import GenericGroupBasedRole, GenericUserBasedRole, Public, UserRole from pfunk.contrib.ecommerce.resources import StripePublic from pfunk.contrib.ecommerce.views import ListStripePackage, DetailStripePackage from pfunk.web.views.json import CreateView, UpdateView, DeleteView stripe.api_key = env('STRIPE_API_KEY') class StripePackage(Collection): """ Collection that has the essential info about a stripe package This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system. Read and detail views are naturally public. Write operations requires authentication from admin group. """ use_crud_views = False collection_roles = [GenericGroupBasedRole] collection_views = [ListStripePackage, DetailStripePackage, CreateView, UpdateView, DeleteView] stripe_id = StringField(required=True) name = StringField(required=True) price = FloatField(required=True) description = StringField(required=True) group = ReferenceField(Group) def __unicode__(self): return self.name @property def stripe_price(self): return int(self.price*100) class StripeCustomer(Collection): """ Base Customer collection for Stripe This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system. """ collection_roles = [GenericUserBasedRole] user = ReferenceField(User) customer_id = StringField(required=True) package = ReferenceField(StripePackage) def __unicode__(self): return self.customer_id def get_or_create_customer(self, user): """ Acquires the Stripe Customer, if not found, create it. Returns: StripeCustomer (required): The instance of this class returned as object """ try: return self.get(user=user) except DocNotFound: sc = stripe.Customer.create( description=f"{user.username}", email=user.email, name=f'{user.first_name} {user.last_name}', metadata={'user_id': user.pk, 'username': user.username} ) return self.create( user=user, customer_id=sc.id )
View Source
class StripePackage(Collection): """ Collection that has the essential info about a stripe package This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system. Read and detail views are naturally public. Write operations requires authentication from admin group. """ use_crud_views = False collection_roles = [GenericGroupBasedRole] collection_views = [ListStripePackage, DetailStripePackage, CreateView, UpdateView, DeleteView] stripe_id = StringField(required=True) name = StringField(required=True) price = FloatField(required=True) description = StringField(required=True) group = ReferenceField(Group) def __unicode__(self): return self.name @property def stripe_price(self): return int(self.price*100)
Collection that has the essential info about a stripe package
This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system.
Read and detail views are naturally public. Write operations requires authentication from admin group.
Specifies whether to use the CRUD views.
Roles that are attached to this collection.
Events that are attached to this collection.
Inherited Members
- pfunk.collection.Collection
- Collection
- BUILTIN_DOC_ATTRS
- collection_functions
- collection_indexes
- all_index
- use_crud_functions
- crud_functions
- crud_views
- require_auth
- non_public_fields
- verbose_plural_name
- collection_name
- protected_vars
- get_fields
- get_collection_name
- get_enums
- get_verbose_plural_name
- all_index_name
- all_function_name
- call_function
- client
- create
- publish
- publish_functions
- publish_roles
- publish_indexes
- unpublish
- get_unique_together
- get_db_values
- get_foreign_fields_by_type
- call_signals
- get_data_dict
- save
- get
- all
- get_by
- get_index
- delete
- delete_from_id
- to_dict
- urls
- valley.schema.BaseSchema
- process_schema_kwargs
- validate
- get_class_name
- to_json
View Source
class StripeCustomer(Collection): """ Base Customer collection for Stripe This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system. """ collection_roles = [GenericUserBasedRole] user = ReferenceField(User) customer_id = StringField(required=True) package = ReferenceField(StripePackage) def __unicode__(self): return self.customer_id def get_or_create_customer(self, user): """ Acquires the Stripe Customer, if not found, create it. Returns: StripeCustomer (required): The instance of this class returned as object """ try: return self.get(user=user) except DocNotFound: sc = stripe.Customer.create( description=f"{user.username}", email=user.email, name=f'{user.first_name} {user.last_name}', metadata={'user_id': user.pk, 'username': user.username} ) return self.create( user=user, customer_id=sc.id )
Base Customer collection for Stripe
This is only a base model made to give an idea how can you structure your collections. Override the fields and functions to match your system.
Roles that are attached to this collection.
View Source
def get_or_create_customer(self, user): """ Acquires the Stripe Customer, if not found, create it. Returns: StripeCustomer (required): The instance of this class returned as object """ try: return self.get(user=user) except DocNotFound: sc = stripe.Customer.create( description=f"{user.username}", email=user.email, name=f'{user.first_name} {user.last_name}', metadata={'user_id': user.pk, 'username': user.username} ) return self.create( user=user, customer_id=sc.id )
Acquires the Stripe Customer, if not found, create it.
Returns
StripeCustomer (required): The instance of this class returned as object
Inherited Members
- pfunk.collection.Collection
- Collection
- BUILTIN_DOC_ATTRS
- collection_functions
- collection_indexes
- all_index
- use_crud_functions
- crud_functions
- use_crud_views
- crud_views
- require_auth
- non_public_fields
- verbose_plural_name
- collection_name
- collection_views
- protected_vars
- get_fields
- get_collection_name
- get_enums
- get_verbose_plural_name
- all_index_name
- all_function_name
- call_function
- client
- create
- publish
- publish_functions
- publish_roles
- publish_indexes
- unpublish
- get_unique_together
- get_db_values
- get_foreign_fields_by_type
- call_signals
- get_data_dict
- save
- get
- all
- get_by
- get_index
- delete
- delete_from_id
- to_dict
- urls
- valley.schema.BaseSchema
- process_schema_kwargs
- validate
- get_class_name
- to_json