I am currently building a Django application where visitors can buy an online course. I now want to implement the possibility to provide discount codes. As these discount codes should be limited by quantity I now have the following implementation idea:
Guest visits www.page.com?discount=TEST
The model discount contains the fields discount_codes & max qty. I will check here, if the code exists. Also, I have to count all entries in my order model that used the discount code TEST. My order model contains the foreign_key field 'redeemed_discounts').
As soon the user clicks on Pay (via Stripe) I'll once again count all the orders in my order model which contain 'TEST' to make sure, the 'max_qty' is not reached meanwhile.
Now I can charge the visitor.
Would you consider this as good implemented or do you see any problems with the way I am planning to do it?
instead of using max_qty why don't you use something like use_left and max_use
so whenever someone uses that code you can reduce the count accordingly and when count hits zero you can stop using that with this approach you don't have to scan order table every time to see if the coupon code is still available.
Related
I'm building a Django web application, part of it involves an online ordering system for food. I want to make a "receipt" object to save transactions.
My concern, however, is this - let's say I have an object Receipt that relates to Orders which relate to Items, if the items get edited or change over time, it will make the receipts look different down the line. Is there a way to save these at the moment of a transaction?
I am implementing a "soft deletion" to my models to avoid deletion issues however I don't think this would protect against edits.
The only way I can think of to deal with is to 'materialize' the Receipt. In other words when a receipt is generated use the Order and Items information current at the time and then write the actual values, not the Order/Items id to a receipt table. So for a Items item write out the attributes(description, price, qty.etc) you are interested in recording to the table, instead of just an Items.id that points to a possibly changed value in future.
After a basic introduction to Python thanks to an edX course and a chat with a friend who told me about Django, I thought I could implement a solution for my laboratory. My goal is to keep track of every reagent order made by everyone of us researchers to the suppliers.
After one month I have a pretty decent version of it which I'm very proud of (I also have to thank a lot of StackOverFlow questions that helped me). Nonetheless, there's one requirement of the ordering flow that I haven't been able to translate to the Django app. Let me explain:
Users have a form to anotate the reagent (one per form) they need, and then it is passed to the corresponding manufacturer for them to send us an invoice. It's convenient that each invoice has several products, but they all have to: a) be sold by the same manufacturer, b) be sent to the same location and c) be charged to the same bank account (it's actually more complicated, but this will suffice for the explanation).
According to that, administrators of the app could process different orders by different users and merge them together as long as they meet the three requirements.
How would you implement this into the app regarding tables and relationships?
What I have now is an Order Model and an Order Form which has different CharFields regarding information of the product (name, reference, etc.), and then the sending direction and the bank account (which are ForeingKeys).
When the administrators (administratives) process the orders, they asign several of them that meet the requirements to an invoice, and then the problem comes: all the data has to be filled repeatedly for each of the orders.
The inmediate solution for this would be to create a Products Model and then each Order instance could have various products as long as they meet the three requirements, but this presents two problems:
1) The products table is gonna be very difficult to populate properly. Users are not gonna be concise about references and important data.
2) We would still have different Orders that could be merged into the same invoice.
I thought maybe I could let the users add fields dynamically to the Order model (adding product 1, product 2, product 3). I read about formsets, but they repeat whole Forms as far as I understood, and I would just need to repeat fields. Anyway, that would solve the first point, but not the second.
Any suggestions?
Thanks, and sorry for the long block!
I want to make a site where people can make listings for things to sell. I want it to have a front page where the most popular (hot) items are always displayed.
Popularity decreases with time and increases with activity (bidding, commenting, clicking). Every item starts with a popularity of 100.
That way uninteresting items dissapear quickly and interesting ones stay on longer.
So everytime a user interacts with the objects its popularity should increase (for example, everytime a get request from a unique user is made for the details of the object, its popularity value goes up by 1, every bid increases it by 10).
On the opposite, everytime a minute or so passes, the popularity of all currently active items decreases. Once it hits 0, it will be "deactivated" it will still be tradable, but it will never hit the frontpage again.
The problem is, how do I decrease the popularity of a queryset of all active items?
I realize that everytime the user request the front page. I could just fetch all active objects, calculate the popularity within python code and sort them by hand, but that seams rather wastefull.
I know I can easily set a property of an entire queryset, by using the update function, but that only takes one absolute value for the entire set. Is there a built in way to just decrease the property by one?
Or do I just have to loop through the queryset and decrease every value manually?
class Item(models.Model):
popularity = models.IntegerField()
Item.objects.update(popularity=models.F('popularity')-1)
This is how you update a queryset based on the values it has instead of giving it an absolute value, you can tweak this around to fit your needs.
because of MAGE price calculation complexity + EPR incompatibility with MAGE calculation, I was thinking about overriding getPrice() and getFinalPrice() methods in a way, that they will call external webservice for a requested price.
Does anybody tried to solve price calculation in a suggested way and if, does this work in a real environment?
Second option is to reverse engineer price engine from ERP in MAGE database (additinal tables + logic inside MAGE).
What do you tink? Any advice would be welcome.
I had a similar problem with complex pricing issues.
I ended up with adding some custom attribute fields that where dynamically chosen on the
customer number.
Could you give me some background on your problem?
we have very complex price calculation. Every customer has its own pricing rules(per product,per product group, special discount, etc..).
There is 15.000 customers and 60.000 items.
It is possible to add all rules into MAGE, but price indexing takes very, very long and MAGE is almost unusable. pricing engine on the other hand is not so hard to implement, but a question remain if it is possible to use price engine from ERP(ERP has API for getting a price) or is it better to implement it inside MAGE (on mysql) with extending original logic.
There is no need to have administrator GUI for checking prices, because sync is done automatically and never entered by the user. (A goal is to completely replace pricing logic from MAGE).
Thank for your answer.
After some work and testing, I did new models in Magento and reimplement price calculation logic based od ERP rules. Webservice would work, but it would be very very slow(call to getPrice or getFinalPrice is executed at least twice for every item displayed).
I am trying to implement a search engine for a new app.
The app allows people to rate items (+1 or -1) - Giving the items a +ve or -ve score.
When people search for items, I'd like to take into account their rating and to order the results accordingly. If the item is a match, it should show up. But if it's a match with a high score it should be boosted up the results a bit.
A really good match should win over a fairly good match with a high score, so it needs to be weighted along with the rest of it (i.e. I boosted my titles a bit).
Not stuck on Solr by any means, only just started playing today.
With Solr, you can maintain a field with the document which holds the difference.
The difference can be between the total +1ve's and the -1ve's.
Solr allows you to boost on field values using function queries.
So you can query with the boost on the difference field, with documents with better difference scoring over others.
From indexing front, as this difference would change quite often, the respective document needs to be updated everytime.
Solr does not allow the updation of the single field, so you need to handle the incremental updates of the difference field.
If that would be a concern to you, can try using ExternalFileField.
This allows mapping of certain fields of documents such as ranking, popularity external to the index in a separate file.
The file can be updated and index committed to reflect the changes.
The field can also be used with function queries to boost the results as needed, however have lot of limitations.
You can order your results by a field that stores the ranking.
sqs.filter(content='blah').order_by('rating')