Flask admin - complex sort on hybrid_property - python

I have a fairly complex hybrid_property. This is a vendor model, which has multiple skuchannels (products). What it does is: Based on the target_stock_duration (e.g. we want to keep items in stock for 4 months) calculate how many units have to be ordered and how much this would cost. This gives us the potential.
class Vendor(db.Model):
__tablename__ = "vendor"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
b2c_price_factor = db.Column(db.Float, nullable=False)
skuchannels = db.relationship("SKUChannel", back_populates="vendor")
#hybrid_property
def po_potential(self):
"""This is a "virtual" property that will that can be used in the admin view. It calculates
the potential value for a comming PO.
Returns:
_type_: _description_
"""
potential = 0
for item in self.skuchannels:
purchasing_price = item.purchasing_price if item.purchasing_price != None else 0
target_stock_duration = 4
try:
to_order = item.average_monthly_sales * target_stock_duration - item.stock_level #calculate how many units we have to order
if to_order < 0:
to_order = 0
except TypeError:
to_order = 0
potential = potential + purchasing_price * to_order #calculate how much everything costs
return potential
well this hybrid_property works just fine, but I would very much like to sort this property. with #po_potential.expression -> well I have no clue how to do this, because in my understanding it should return a select object. Is there any other way?

This should get you started:
class Vendor(Base):
...
...
#po_potential.expression
def po_potential(cls):
target_stock_duration = 4
return (
select(func.sum(
func.ISNULL(SKUChannel.purchasing_price, 0) *
func.GREATEST(0, SKUChannel.average_monthly_sales * target_stock_duration - SKUChannel.stock_level, 0)
))
.where(SKUChannel.vendor_id == cls.id)
.label('po_potential')
)

Related

Calculate a value from GraphQL and save it in Django models

I am trying to calculate a value from GraphQL. I am sending mutation to Django models but before save it I want to calculate this value with if statement (if the value is greater than 10 divide by 2, if is less than 10 multiply by 2).
I don't know where to add this function.
Here is my mutation in schema.py
class CreatePrice(graphene.Mutation):
price = graphene.Field(PriceType)
class Arguments:
price_data = PriceInput(required=True)
#staticmethod
def mutate(root, info, price_data):
price = Price.objects.create(**price_data)
return CreatePrice(price=price)
class Mutation(graphene.ObjectType):
create_product = CreateProduct.Field()
create_price = CreatePrice.Field()
schema = graphene.Schema(query = Query, mutation=Mutation)
And here is my Django model. Base price is calculated value and function name has two options(*2 or /2 it depends of initial value).
class Price(models.Model):
base_price = models.CharField(max_length = 20)
function_name = models.CharField(max_length = 20, choices = PROMO_FUNCTION)
def __str__(self):
return self.price_name
P.S. Sorry for bad English. Thanks!
I don't know why you are using CharField for base_price. So, I suggest you to do this:
#staticmethod
def mutate(root, info, price_data):
if int(price_data.base_price) >= 10:
price_data.base_price = str(int(price_data.base_price) / 2)
else:
price_data.base_price = str(int(price_data.base_price) * 2)
price = Price(base_price=price_data.base_price, function_name=price_data.function_name)
price.save()
return CreatePrice(price=price)
You can also create records in database by creating object and using save method on it.

SQLAlchemy order by function result

This is the code I have and it is working (returns all problems ordered by difficulty):
def get_noteworthy_problems(self):
ACategory = aliased(Category)
AProblem = aliased(Problem)
all_prob = DBSession.query(AProblem).filter(
AProblem.parent_id == ACategory.id,
ACategory.parent_id == self.id)
noteworthy_problems = \
sorted(all_prob, key=lambda x: x.difficulty(), reverse=True)
return noteworthy_problems
But I think I must optimize this code.
Is there a possibility to change the code having order_by and my function difficulty()? My function returns a number. I tried something like:
result = DBSession.query(AProblem).filter(
AProblem.parent_id == ACategory.id,
ACategory.parent_id == self.id).order_by(
AProblem.difficulty().desc())
but I receive the error TypeError: 'NoneType' object is not callable.
Hybrid attributes are special methods that act as both a Python property and a SQL expression. As long as your difficulty function can be expressed in SQL, it can be used to filter and order like a normal column.
For example, if you calculate difficulty as the number of parrots a problem has, times ten if the problem is older than 30 days, you would use:
from datetime import datetime, timedelta
from sqlalchemy import Column, Integer, DateTime, case
from sqlalchemy.ext.hybrid import hybrid_property
class Problem(Base):
parrots = Column(Integer, nullable=False, default=1)
created = Column(DateTime, nullable=False, default=datetime.utcnow)
#hybrid_property
def difficulty(self):
# this getter is used when accessing the property of an instance
if self.created <= (datetime.utcnow() - timedelta(30)):
return self.parrots * 10
return self.parrots
#difficulty.expression
def difficulty(cls):
# this expression is used when querying the model
return case(
[(cls.created <= (datetime.utcnow() - timedelta(30)), cls.parrots * 10)],
else_=cls.parrots
)
and query it with:
session.query(Problem).order_by(Problem.difficulty.desc())

Random match-up with Python and Google App Engine

I am building a website where two music videos are randomly chosen from a database and go head-to-head for voting. I need an algorithm that will continue picking unique match-ups for a user excluding match-ups they have had in the past, but with replacing videos for new match-ups. You can view a sample of the page here: http://10.showtownmvp.appspot.com/
I am running this on Google App Engine - Python, and have a voting table and videos table that stores the results. I would like to keep it as random as possible and avoid multiple queries, so if you have suggestions on how to model this in NDB or have a good algorithm, I would appreciate your help!
My solution to this problem was to query all videos from the datastore and randomly select one. I also ran a query for past votes / matchups for the user and converted this to a list so I could manipulate it without running several queries. Using the random video, I used a while loop to find a second video that was not in the previous matchup list. If no video was found, the program would remove the random choice from the video list, then select a new sample and run the search again. The code is below:
class MainHandler(views.Template):
def post(self):
# NOTE: we are posting genre and state.
user = self.user_check()
self.videos = models.videos.Videos.fetch_featured()
try:
random.sample(self.videos,2)
if user:
self.user_votes = models.voting.Voting.query_by_user(user.key)
if self.user_votes != None:
self.user_votes = [[x.video_one,x.video_two] for x in self.user_votes]
page_vids = False
while page_vids == False and len(self.videos)>1:
rand_vid = random.choice(self.videos)
page_vids = self.find_match(rand_vid)
self.videos.remove(rand_vid)
else:
page_vids = random.sample(self.videos,2)
else:
page_vids = random.sample(self.videos,2)
except:
page_vids = None
def find_match(self, rand_vid):
i =0
while i < len(self.videos):
if rand_vid.key != self.videos[i].key and ([rand_vid.key,self.videos[i].key] not in self.user_votes and [self.videos[i].key, rand_vid.key] not in self.user_votes):
return [rand_vid,self.videos[i]]
i+=1
return False
class Videos(ndb.Model):
acc_key = ndb.KeyProperty()
musician_key = ndb.KeyProperty()
musician_name = ndb.StringProperty()
embed_link = ndb.StringProperty()
genre_tag = ndb.StringProperty()
video_title = ndb.StringProperty()
featured = ndb.BooleanProperty(default = False)
likes_count = ndb.IntegerProperty()
video_added = ndb.DateTimeProperty(auto_now_add = True)
#classmethod
def query_by_account(cls, acc_key):
return cls.query(cls.acc_key == acc_key).fetch()
#classmethod
def fetch_featured(cls):
return cls.query(cls.featured == True).fetch(100)
class Voting(ndb.Model):
voter_acc_key = ndb.KeyProperty()
voter_type = ndb.StringProperty()
video_one = ndb.KeyProperty()
video_one_artist_key = ndb.KeyProperty()
video_two = ndb.KeyProperty()
video_two_artist_key = ndb.KeyProperty()
voter_choice = ndb.KeyProperty()
video_set_check = ndb.KeyProperty(repeated = True)
voter_ip = ndb.StringProperty()
vote_time = ndb.DateTimeProperty(auto_now_add = True)
#classmethod
def query_by_user(cls, acc_key):
return cls.query(cls.voter_acc_key == acc_key).fetch(2000)

Django random value for Model field

So I am able to generate a random id using uuid
So far so good
But when I try to database i get same value
def f():
d = uuid4()
str = d.hex
return str[0:16]
class Q(models.Model):
a = models.CharField(max_length=150)
b = models.IntegerField(max_length=25)
c = models.IntegerField(max_length=32 , default=0)
d = models.ManyToManyField(Ans , related_name='aa')
e = models.CharField(max_length=18 , default = f() ,unique=True )
class Ans(models.Model):
sub = models.CharField(max_length=150)
-----------------------------------------------------------------
And I'm inserting like this
def ins(request):
t =random.randint(0, 1000)
p = Q(a = t , b=0 , c=0)
p.save()
return HttpResponse('Saved')
Just curious what the hell is happening here
Side note: If I set e.unique = False I get 2-3 with the same e values before I get a new
UUID values
You should not call the function that you are passing to default:
e = models.CharField(max_length=18, default=f, unique=True)
FYI, according to docs, you should pass a value or a callable:
The default value for the field. This can be a value or a callable
object. If callable it will be called every time a new object is
created.

Matching 3 out 5 fields - Django

I'm finding this a bit tricky! Maybe someone can help me on this one
I have the following model:
class Unicorn(models.Model):
horn_length = models.IntegerField()
skin_color = models.CharField()
average_speed = models.IntegerField()
magical = models.BooleanField()
affinity = models.CharField()
I would like to search for all similar unicorns having at least 3 fields in common.
Is it too tricky? Or is it doable?
You should use Q objects. The rough example is:
from django.db.models import Q
from itertools import combinations
# this -- the unicorn to be matched with
attr = ['horn_length', 'skin_color', 'average_speed', 'magical', 'affinity']
q = None
for c in combinations(attrs, 3):
q_ = Q(**{c[0]: getattr(this, c[0])}) & Q(**{c[1]: getattr(this, c[1])}) & Q(**{c[2]: getattr(this, c[2])})
if q is None:
q = q_
else:
q = q | q_
Unicorn.objects.get(q)
not tested, though
It has to be done in the HAVING clause:
SELECT ... HAVING (IF(a.horn_length=b.horn_length, 1, 0) + ...) >= 3
There's no way to express HAVING in the Django ORM so you'll need to drop to raw SQL in order to perform it.
This should cover your question, if I understood it right:
from django.db import models
Unicorn.objects.filter(models.Q(skin_color = 'white') | models.Q(magical = True))
This would filter all unicorns that have skin color white or have some magical stuff in common. More about the Q objects here http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
I have never used Django and i'm rather novice in Python but perhaps you can do something like this:
make a method that compares two instances of the class Unicorn.
def similarity(self, another)
sim = 0
if (self.horn_length==another.horn_length):
sim+=1
if (self.skin_color==another.skin_color):
sim+=1
if (self.average_speed==another.average_speed):
sim+=1
if (self.magical==another.magical):
sim+=1
if (self.affinity==another.affinity):
sim+=1
return sim
Then you can test with something like:
myUnicorn
for x in unicornsList:
if myUnicorn.similarity(x) >=3:
...

Categories

Resources