def get_item_and_variant(item: dict) -> (StoreItem, StoreItemVariant): """ Return store item and variant (if any). """ store_item = StoreItem.items_available().get(pk=item['item_id']) store_variant = store_item.variants.get(pk=item['variant_id']) if item['variant_id'] else None return store_item, store_variant
class StoreItemViewSet(ReadOnlyModelViewSet): """ Exposes all available store items. This entrypoint does not require authentication/authorization. """ serializer_class = StoreItemSerializer queryset = StoreItem.items_available() permission_classes = [IsAuthenticatedOrReadOnly] authentication_classes = []
def get_item_and_variant(item: dict) -> (StoreItem, StoreItemVariant): """ Return store item and variant (if any). """ store_item = StoreItem.items_available().get(pk=item['item_id']) store_variant = store_item.variants.get( pk=item['variant_id']) if item['variant_id'] else None return store_item, store_variant
def test_secret_items(self): """Test hiding secret items unless the correct key is provided.""" self.assertLess(len(StoreItem.items_visible()), len(StoreItem.items_available())) self.assertLess(len(StoreItem.items_visible('cat')), len(StoreItem.items_available())) self.assertEqual(len(StoreItem.items_visible('kissa')), len(StoreItem.items_available()))
def validate_item(item): # First, make sure the item exists at all try: store_item = StoreItem.items_available().get(id=item['item_id']) except StoreItem.DoesNotExist: raise TransactionException("Tuotetta ei ole saatavilla") # Make sure the variant exists and belongs to the requested item if item['variant_id']: try: store_item.variants.get(id=item['variant_id']) except StoreItemVariant.DoesNotExist: raise TransactionException("Tuotetyyppiä ei ole saatavilla") # Make sure there are enough items in the stock to satisfy this request if store_item.num_available() < item['amount']: raise TransactionException("Tuotetta {} ei ole saatavilla riittävästi!".format(store_item.name))
def create_store_transaction(data): # Handle creation of the order in a transaction to avoid creating crap to db in errors try: with transaction.atomic(): ta = StoreTransaction() ta.firstname = data['first_name'] ta.lastname = data['last_name'] ta.company = data['company'] ta.email = data['email'] ta.telephone = data['telephone'] ta.mobile = data['mobile'] ta.street = data['street'] ta.postalcode = data['postal_code'] ta.city = data['city'] ta.country = data['country'] ta.information = data['information'] ta.time_created = datetime.now() ta.key = uuid.uuid4().hex ta.save() # Check items for item in data['items']: # First, make sure that the ordered item exists and is available store_item = StoreItem.items_available().get(pk=item['item_id']) store_variant = store_item.variants.get(pk=item['variant_id']) if item['variant_id'] else None # Find the price with discounts (if any) amount = item['amount'] purchase_price = store_item.get_discounted_unit_price(amount) # Form the transaction item(s) for m in range(amount): ta_item = TransactionItem() ta_item.transaction = ta ta_item.item = store_item ta_item.variant = store_variant ta_item.key = uuid.uuid4().hex ta_item.purchase_price = purchase_price ta_item.original_price = store_item.price ta_item.save() return ta except Exception as e: logger.error("Unable to save store transaction: %s", str(e)) raise
def validate_item(item: dict): # First, make sure the item exists at all try: store_item = StoreItem.items_available().get(id=item['item_id']) except StoreItem.DoesNotExist: raise TransactionException("Tuotetta ei ole saatavilla") # Make sure the variant exists and belongs to the requested item if item['variant_id']: try: store_item.variants.get(id=item['variant_id']) except StoreItemVariant.DoesNotExist: raise TransactionException("Tuotetyyppiä ei ole saatavilla") # Make sure there are enough items in the stock to satisfy this request if store_item.num_available() < item['amount']: raise TransactionException( "Tuotetta {} ei ole saatavilla riittävästi!".format( store_item.name))
def __init__(self, *args, **kwargs): super(StoreProductsForm, self).__init__(*args, **kwargs) for item in StoreItem.items_available(): name = 'item-%s' % item.id self.fields[name] = forms.IntegerField( initial='0', label=u'%s' % (item.name), help_text=item.description, required=False, widget=forms.TextInput() ) self.fields[name].widget.attrs['data-price'] = item.price self.fields[name].widget.attrs['data-min'] = 0 self.fields[name].widget.attrs['data-max'] = item.num_available() self.fields[name].widget.attrs['class'] = 'item-amount-field' self.fields[name].image_large = item.imagefile_original.url \ if item.imagefile_original else None self.fields[name].image_small = item.imagefile_thumbnail.url \ if item.imagefile_thumbnail else None self.fields[name].available = item.num_in_store() self.fields[name].price = item.price
def create_test_item(name, event, **kwargs): item = StoreItem() item.name = name item.event = event item.description = kwargs.get('description', fake.sentences(3)) item.price = kwargs.get('price', 20) item.max = kwargs.get('max', 50) item.available = kwargs.get('available', True) item.max_per_order = kwargs.get('max_per_order', 5) item.sort_index = kwargs.get('sort_index', 0) item.discount_amount = kwargs.get('discount_amount', -1) item.discount_percentage = kwargs.get('discount_percentage', 0) item.is_ticket = kwargs.get('is_ticket', False) item.save() return item
def __init__(self, *args, **kwargs): self.event_id = kwargs.pop('event_id', None) super(StoreOrderForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_class = 'store' self.fields['read_terms'].help_text = \ u'Olen lukenut <a href="%s" target="_blank">toimitusehdot</a> ' \ u'ja hyväksyn ne. (Luethan myös <a href="%s" target="_blank">rekisteriselosteen</a>)' % (reverse('store:terms'), reverse('store:privacy')) self.fields['information'].widget.attrs['placeholder'] = \ u'Mikäli tilauksessasi on T-paita, lisää tähän kenttään sen haluttu koko.' item_fields = Fieldset(u'', css_class='store-items') for item in StoreItem.items_for_event(self.event_id): name = 'item-%s' % item.id self.fields[name] = forms.IntegerField( initial=0, min_value=0, max_value=item.num_available(), label=u'%s' % (item.name), help_text=item.description, required=False ) # Set data-maxvalue attr for field, just for javascript stuff self.fields[name].widget.attrs['data-maxvalue'] = item.num_available() # Container mdiv = Div() # Print message if item is sold out if item.num_in_store() <= 0: self.fields[name].widget.attrs['disabled'] = True self.fields[name].label += u' <span class="item-soldout">(Lopussa)</span>' # Print img tag if item has image if item.imagefile_thumbnail: mdiv.fields.append( HTML('<img class="item-image" src="%s" width="64" height="64" alt="Tuotakuva" data-bigimg="%s" />' % (item.imagefile_thumbnail.url, item.imagefile_original.url)), ) mdiv.fields.append( HTML(u'<span class="item-price">%d €/kpl</span>' % item.price) ) mdiv.fields.append(name) item_fields.fields.append(mdiv) self.helper.layout = Layout( HTML(u'<h2>Tuotteet</h2>'), item_fields, HTML(u'<h2>Maksajan tiedot</h2>'), Fieldset( u'', 'firstname', 'lastname', 'email', 'email_confirm', 'telephone', 'mobile', 'company', 'street', 'postalcode', 'city', 'country', 'information', 'read_terms', ButtonHolder( Submit('Buy', u'Osta') ), css_class='store-details' ) )
def get_queryset(self): return StoreItem.items_visible(secret_key=self.request.query_params.get('secret_key'))
def get_queryset(self): return StoreItem.items_visible( secret_key=self.request.query_params.get('secret_key'))