convert queryset into pure string only - django

I have some code that checks if two string matches and they do in the database, but when I query them using this code
r = Usertasks.objects.all().filter(user=request.user).filter(randomURL=payment_id).values_list("TaskPostedToNetwork", flat=True)
e = Usertasks.objects.all().filter(user=request.user).filter(randomURL=payment_id).values_list("PaymentConfirmed", flat=True)
It returns
<QuerySet [False]>
<QuerySet ['yes']>
My if loop that checks for the string is never ran because it doesnt return as a string but with the queryset around it, I guess
if r == "False" and e == "yes":
print("Works")
How do I make it return just
False
yes

I'm assuming your query is intended to return just a single value. ie: user and randomURL are unique together. In that case:
task = Usertasks.objects.get(user=request.user, randomURL=payment_id)
if not task.TaskPostedToNetwork and task.PaymentConfirmed == 'yes':
print("Works")

Related

Django check if querysets are equals

I have this django code
q1 = MyModel.objects.all()
q2 = MyModel.objects.all()
When I try:
print(q1 == q2)
I get as a result:
False
So how can I check if two querysets results in django are equal?
You can convert the querysets to lists and check whether they are equal:
list(q1) == list(q2)
You can convert it to set, to check if 2 query sets have the same elements, without regard to ordering:
set(q1) == set(q2)
it will return:
True
Try this:
q1.intersection(q2).count() == q1.count() and q1.count() == q2.count()
Throwing in my two cents for a function that compares two QuerySets for equality while ignoring sort order. Note I'm not actually checking whether the QuerySets are empty or not; I'll leave that up to you.
def querysets_are_same(qs1, qs2, exclude_fields=[]):
'''
Check whether two queryset have the same content, sort order of querysets is ignored.
Params:
-------
qs1 (QuerySet) - first queryset to compare
qs2 (QuerySet) - second queryset to compare
exclude_fields (list) - fields to exclude from comparison; primary key field is automatically removed.
Yield:
------
True if both querysets contain the same data while ignoring sort order; False otherwise
'''
# lookup primary key field name
pk_qs1 = qs1[0]._meta.pk.name
pk_qs2 = qs2[0]._meta.pk.name
# update excluded fields
exclude_fields_qs1 = set(exclude_fields) | set([pk_qs1])
exclude_fields_qs2 = set(exclude_fields) | set([pk_qs2])
# convert queryset to list of dicts excluding fields
list_qs1 = [{k:v for k,v in d.items() if not k in exclude_fields_qs1} for d in qs1.values()]
list_qs2 = [{k:v for k,v in d.items() if not k in exclude_fields_qs2} for d in qs2.values()]
# sort lists
list_qs1_sorted = sorted(sorted(d.items()) for d in list_qs1)
list_qs2_sorted = sorted(sorted(d.items()) for d in list_qs2)
return list_qs1_sorted == list_qs2_sorted
You can see by .count() or:
q1 = Model.objects.all()
q2 = Model.objects.all()
equal = True
for idx, q in q1:
if q != q2[idx]:
equal = False
print(equal)

Django Validate Bitcoin Address

I'd like to have a Django form which accepts a bitcoin address from a user. What would be the best way to validate whether or not this address is legitimate?
I could try writing my own implementation, but in regards to these things I'm assuming it's always better to go with something tried and tested than create something with potential holes. Is there good python code I can use to make a custom field for my django forms, or any resources that have already done this?
Alternatively, might it be better to perhaps skip the whole custom form field process, for example, and validate the address in the view using a pycoin library instead? If I were to do it this way, however, how would I return the error in the form?
BCAddressField acomplishes exactly what I was looking for. Note however that you must replace from django.forms.util import ValidationError with from django.core.exceptions import ValidationError, as the former is deprecated.
Bech32:
def btc_validation(address):
a = str(address)
length = 0
valid = False
not_btc = False
for i in a:
length += 1
if length == 42:
if a[2] == '1' and a[0] == 'b' and a[1] == 'c':
for i in a:
if i == 'O' or i == 'I':
not_btc = True
break
if not_btc == True:
return valid
else:
valid = True
return valid
else:
return 'didn\'t start with bc1'
else:
return 'it\'s short'
P2PKH:
import base58
def bitcoin_address_validation(bitcoinAddress):
"""
Base58 (P2PKH)
"""
try:
base58Decoder = base58.b58decode(bitcoinAddress).hex()
prefixAndHash = base58Decoder[:len(base58Decoder) - 8]
checksum = base58Decoder[len(base58Decoder) - 8:]
hash = prefixAndHash
for x in range(1, 3):
hash = hashlib.sha256(binascii.unhexlify(hash)).hexdigest()
if (checksum == hash[:8]):
valid = True
else:
valid = False
except:
valid = False
pass
return valid
Reference

cleaning up my SQLAlchemy operations (reducing repetition)

I have some server side processing of some data (client-side library = jQuery DataTables)
I am using POST as my ajax method. In my Flask webapp, I can access the POST data with request.values
The data type / structure of request.values is werkzeug.datastructures.CombinedMultiDict
If the user wants to sort a column, the request contains a key called action with a value of filter (note the below printouts are obtained with for v in request.values: print v, request.values[v])
...
columns[7][data] role
columns[8][search][regex] false
action filter
columns[10][name]
columns[3][search][value]
...
all the column names are also contained in the request as keys. The columns that have search terms will have the search string as a value for the column name key (as opposed to empty for columns with no search term entered. So, If I want to search for firstname containing bill, I would see the following in my request
columns[7][searchable] true
...
columns[6][name]
firstname bill
columns[0][search][value]
columns[2][searchable] true
...
columns[5][data] phone
role
columns[10][data] registered_on
...
columns[0][searchable] true
email
columns[7][orderable] true
...
columns[2][search][value]
Notice how role and email are empty. So my code below is very non-DRY
rv = request.values
if rv.get('action') == 'filter':
if len(rv.get('firstname')):
q = q.filter(User.firstname.ilike('%{0}%'.format(rv.get('firstname'))))
if len(rv.get('lastname')):
q = q.filter(User.lastname.ilike('%{0}%'.format(rv.get('lastname'))))
if len(rv.get('username')):
q = q.filter(User.username.ilike('%{0}%'.format(rv.get('username'))))
if len(rv.get('email')):
q = q.filter(User.email.ilike('%{0}%'.format(rv.get('email'))))
if len(rv.get('phone')):
q = q.filter(User.phone.ilike('%{0}%'.format(rv.get('phone'))))
if len(rv.get('region')):
q = q.filter(User.region.name.ilike('%{0}%'.format(rv.get('region'))))
if len(rv.get('role')):
q = q.filter(User.role.name.ilike('%{0}%'.format(rv.get('role'))))
if len(rv.get('is_active')):
q = q.filter(User.is_active_ == '{0}'.format(rv.get('is_active')))
if len(rv.get('is_confirmed')):
q = q.filter(User.is_confirmed == '{0}'.format(rv.get('is_confirmed')))
if len(rv.get('registered_on_from')):
fdate = datetime.strptime(rv.get('registered_on_from'), '%Y-%m-%d')
q = q.filter(User.registered_on > fdate)
if len(rv.get('registered_on_to')):
tdate = datetime.strptime(rv.get('registered_on_to'), '%Y-%m-%d')
q = q.filter(User.registered_on < tdate)
I was building the sorting functionality, and I found the following statement that greatly simplified my life (see this answer)
q = q.order_by('{name} {dir}'.format(name=sort_col_name, dir=sort_dir))
I was wondering if there was a way to simplify this set of filtering queries like the above sorting code since I will have to do this for many other models.
This should help:
from sqlalchemy import inspect
from sqlalchemy.sql.sqltypes import String,Boolean
def filter_model_by_request(qry,model,rv):
if rv.get('action') == 'filter':
mapper = inspect(model).attrs # model mapper
col_names = list(set([c.key for c in mapper]) & set(rv.keys()))
# col_names is a list generated by intersecting the request values and model column names
for col_name in col_names:
col = mapper[col_name].columns[0]
col_type = type(col.type)
if col_type == String: # filter for String
qry = qry.filter(col.ilike('%{0}%'.format(rv.get(col_name))))
elif col_type == Boolean: # filter for Boolean
qry = qry.filter(col == '{0}'.format(rv.get(col_name)))
return qry
Example call (I used it with a #app.before_request and a cURL call to verify):
qry = db.session.query(User)
print filter_model_by_request(qry,User,request.values).count()
The date range filtering is not included in the function, add this feature if you wish, your code is fine for that purpose.
side note: be careful with the bigger/smaller operators for the dates. You're excluding the actual requested dates. Use <= or >= to include dates in filtering action. It's always a pitfall for me..

How to include "None" in lte/gte comparisons?

I've got this complex filtering mechanism...
d = copy(request.GET)
d.setdefault('sort_by', 'created')
d.setdefault('sort_dir', 'desc')
form = FilterShipmentForm(d)
filter = {
'status': ShipmentStatuses.ACTIVE
}
exclude = {}
if not request.user.is_staff:
filter['user__is_staff'] = False
if request.user.is_authenticated():
exclude['user__blocked_by__blocked'] = request.user
if form.is_valid():
d = form.cleaned_data
if d.get('pickup_city'): filter['pickup_address__city__icontains'] = d['pickup_city']
if d.get('dropoff_city'): filter['dropoff_address__city__icontains'] = d['dropoff_city']
if d.get('pickup_province'): filter['pickup_address__province__exact'] = d['pickup_province']
if d.get('dropoff_province'): filter['dropoff_address__province__exact'] = d['dropoff_province']
if d.get('pickup_country'): filter['pickup_address__country__exact'] = d['pickup_country']
if d.get('dropoff_country'): filter['dropoff_address__country__exact'] = d['dropoff_country']
if d.get('min_price'): filter['target_price__gte'] = d['min_price']
if d.get('max_price'): filter['target_price__lte'] = d['max_price']
if d.get('min_distance'): filter['distance__gte'] = d['min_distance'] * 1000
if d.get('max_distance'): filter['distance__lte'] = d['max_distance'] * 1000
if d.get('available_on'): # <--- RELEVANT BIT HERE ---
filter['pickup_earliest__lte'] = d['available_on'] # basically I want "lte OR none"
filter['pickup_latest__gte'] = d['available_on']
if d.get('shipper'): filter['user__username__iexact'] = d['shipper']
order = ife(d['sort_dir'] == 'desc', '-') + d['sort_by']
shipments = Shipment.objects.filter(**filter).exclude(**exclude).order_by(order) \
.annotate(num_bids=Count('bids'), min_bid=Min('bids__amount'), max_bid=Max('bids__amount'))
And now my client tells me he wants pickup/drop-off dates to be 'flexible' as an option. So I've updated the DB to allow dates to be NULL for this purpose, but now the "available for pickup on" filter won't work as expected. It should include NULL/None dates. Is there an easy fix for this?
Flip the logic and use exclude(). What you really want to do is exclude any data that specifies a date that doesn't fit. If pickup_latest and pickup_earliest are NULL it shouldn't match the exclude query and wont be removed. Eg
exclude['pickup_latest__lt'] = d['available_on']
exclude['pickup_earliest__gt'] = d['available_on']
Most database engines don't like relational comparisons with NULL values. Use <field>__isnull to explicitly check if a value is NULL in the database, but you'll need to use Q objects to OR the conditions together.
Don't think that's actually a django-specific question. Variable 'd' is a python dictionary, no? If so, you can use this:
filter['pickup_latest__gte'] = d.get('available_on', None)

In django forms custom widget return list as value instead of string

I am writting a custom widget which I want to return a list as the value. From what I can find to set the value that is returned you create a custom value_from_datadict function. I have done this
def value_from_datadict(self, data, files, name):
value = data.get(name, None)
if value:
# split the sting up so that we have a list of nodes
tag_list = value.strip(',').split(',')
retVal = []
# loop through nodes
for node in tag_list:
# node string should be in the form: node_id-uuid
strVal = str(node).split("-")
uuid = strVal[-1]
node_id = strVal[0]
# create a tuple of node_id and uuid for node
retVal.append({'id': node_id, 'uuid': uuid})
if retVal:
# if retVal is not empty. i.e. we have a list of nodes
# return this. if it is empty then just return whatever is in data
return retVal
return value
I expect this to return a list but when I print out the value it is returned as a string rather than a list. The string itself contains the right text but as i said it is a string and not a list. An example of what is returned could be
[{'id': '1625', 'uuid': None}]
but if I did str[0] it would print out [ instead of {'id': '1625', 'uuid': None}
How can I stop it from converting my list into a string?
Thanks
Well, it's simple: if you have a CharField, then you will get a string as a result, because CharField uses method to_python, that coerces the result to string. You need to create your own Field for this one and return a list.
OLD
Could you post the result of:
x = value_from_datadict(..)
print type(x)
so we can see, what exactly is returned?
And could you post the whole test case you are using to deliver the example?