I am starting to learn python and unable to figure this out (spent hours googling it).
Here's what I have:
response = urllib.request.urlopen(url)
data = json.loads(response.read())
table_data = []
for i in data['items']:
attendeeid = i['attendee id']
if 'reg: how hear ' in i:
howhear = i['reg: how hear ']
else:
howhear = 'No Data'
orderid = i['order_id']
totalprice = i['total price']
ordernumber = i['order_number']
email = i['email']
state = i['state']
country = i['country']
companyname = i['company name']
packagename = i['package name']
orderdate = i['order date']
pipedurl = i['piped url']
revenue = i['revenue']
lineitemquantity = i['line item quantity']
timebaseitemprice = i['time base item price']
table_data.append([attendeeid, howhear, orderid, totalprice, ordernumber, email, state, country, companyname, packagename, orderdate, pipedurl, revenue, lineitemquantity, timebaseitemprice])
However, I am finding that not all records have a value in the state field. How do I create an exception to address null state values?
thanks.
Related
What I want to accomplish is to get a unique list of the names of customers with their lastest consultation date.
I have defined these models in my models.py file, using mySQL as my database:
class Customer(models.Model):
class ContactChoice(models.IntegerChoices):
DO_NOT_CONTACT = 0
EMAIL = 1
TXT_SMS_VIBER = 2
mobile_num = models.CharField('Mobile Number', max_length=10, unique=True,)
email_add = models.EmailField('Email', max_length=150, unique=True,)
last_name = models.CharField('Last Name', max_length=30,)
first_name = models.CharField('First Name', max_length=30,)
contact_for = models.CharField('Contact For', max_length=60,)
contact_on = models.IntegerField('Contact Using', choices=ContactChoice.choices, default=0,)
customer_consent = models.BooleanField('With Consent?', default=False,)
def __str__(self):
return self.last_name + ', ' + self.first_name
class Consultation(models.Model):
consultation_date = models.DateTimeField('Date of Consultation', default=now)
customer = models.ForeignKey(Customer, on_delete=models.SET_DEFAULT, default=1)
concern = models.ForeignKey(SkinConcern, on_delete=models.SET_DEFAULT, default=1)
consultation_concern = models.CharField('Other Concerns', max_length=120, null=True,)
product = models.ForeignKey(Product, on_delete=models.SET_DEFAULT, default=1)
user = models.ForeignKey(User, on_delete=models.SET_DEFAULT, default=1)
store = models.ForeignKey(Store, on_delete=models.SET_DEFAULT, default=1)
consultation_is_active = models.BooleanField('Is Active', default=True)
def __str__(self):
return self.customer.last_name + ", " + self.customer.first_name
In my views.py, I have this for the Consultations page:
distinct = Consultation.objects.values('customer').annotate(consultation_count=Count('customer')).filter(consultation_count=1)
consults = Consultation.objects.filter(customer__in=[item['customer'] for item in distinct])
As mentioned, I was expecting to get a unique list of customer names with their latest consultation dates. This code results in only 1 record being shown.
Can you point me in the right direction for this? Thank you in advance! :)
As I see it, what you're doing right now is gathering all the customers that only had one consultation. This won't return what you want.
I believe you can use the latest() method for this: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#latest
This is untested code, but you could do something like this:
# gather all the customers
customers = Customer.objects.all()
# for every customer, get their latest consultation date, using the .latest() function
for customer in customers:
try:
latest_consultation = Consultation.objects.filter(customer=customer).latest('consultation_date')
latest_consultation_date = latest_consultation.consultation_date
except Consultation.DoesNotExist:
latest_consultation_date = None
customer.latest_consultation_date = latest_consultation_date
you can then loop over it as so:
for customer in customers:
if customer.latest_consultation_date:
print(customer.latest_consultation_date)
def export_as_xls(self, request, queryset):
opts = self.model._meta
file_name = unidecode(opts.verbose_name)
sql_query = '''SELECT
COUNT(id) AS No_Of_Report,
vendor,
country_code,
SUM(new_code)*100/SUM(sent) AS 'failure_rate',
SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate'
FROM sms_statistics
WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
GROUP BY vendor, country_code
ORDER BY vendor DESC;'''
This is mysql query i used to call for the data in mysql schema
field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
wb = Workbook()
ws = wb.active
ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
with connection.cursor() as cursor:
cursor.execute(sql_query)
objects = list(cursor.fetchall())
for row in cursor.fetchall():
objects = list(row)
ws.append(objects)
print(ws.append(row))
ws = style_output_file(ws)
I think the issue is right here for not being able to export to excel. Im not be using the right method to export the file from action.py
response =
HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
wb.save(response)
return response
export_as_xls.short_description = "Reports of Past 30 days"
export_as_xls.acts_on_all = True
Blockquote
I believe this part is fine as i tried exporting an empty file and its working as expexted
def export_thirty_days(self, request, queryset):
opts = self.model._meta
file_name = "Sms Statistic Report"
sql_query = '''SELECT
COUNT(id) AS No_Of_Report,
vendor,
country_code,
SUM(new_code)*100/SUM(sent) AS 'failure_rate',
SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate'
FROM sms_statistics
WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
GROUP BY vendor, country_code
ORDER BY COUNT(id) DESC;'''
field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
wb = Workbook()
ws = wb.active
ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
with connection.cursor() as cursor:
cursor.execute(sql_query)
for row in cursor.fetchall():
l = list(row)
ws.append(l)
ws = style_output_file(ws)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
wb.save(response)
return response
export_thirty_days.short_description = "Reports of Past 30 days"
export_thirty_days.acts_on_all = True
This is the solutions i've found to make it work
I'm writing a code to generate n-grams for every record in the table by reading a specific column.
def extract_from_db(inp_cust_id):
sql_db = TatDBHelper()
t_sql = "select notes from raw_data where customer_id = {0}"
db_data = sql_db.execute_read(t_sql.format(inp_cust_id))
for row in db_data:
text = row.values()
bi_grams = generate_ngrams(text[0].encode("utf-8"), 2)
print bi_grams
def generate_ngrams(sentence, n):
sentence = sentence.lower()
sentence = re.sub(r'[^a-zA-Z0-9\s]', ' ', sentence)
tokens = [token for token in sentence.split(" ") if token != ""]
ngrams = zip(*[tokens[i:] for i in range(n)])
return [" ".join(ngram) for ngram in ngrams]
I'm getting the output like:
['i highly', 'highly recommend', 'recommend it']
['the penguin', 'penguin encounter', 'encounter was', 'was awesome']
I want the output to look like below, can anybody help me to get this.
['i highly',
'highly recommend',
'recommend it',
...
]
creat another list all_ngrams, and keep appending the values to it , using .extend(), and finally you will have all the ngrams in one list.
Try this :
def extract_from_db(inp_cust_id):
sql_db = TatDBHelper()
t_sql = "select notes from raw_data where customer_id = {0}"
db_data = sql_db.execute_read(t_sql.format(inp_cust_id))
all_ngrams = []
for row in db_data:
text = row.values()
bi_grams = generate_ngrams(text[0].encode("utf-8"), 2)
all_ngrams.extend(bi_grams)
print all_ngrams
$stmt = $db->prepare("UPDATE reservations SET name = :name, start = :start, end = :end, room_id = :room, status = :status, paid = :paid, customer = :customer, name_ship = :name_ship, equipment = :equipment, port = :port, ETA = :ETA, ETD = :ETD, service_id = :service_id, service_classification = :service_classification, job = :job WHERE id = :id");
Hi!
I have this query and I would like to transfer it into INSERT format.
When I made it like this, nothing happens. Thank you for your advice
"INSERT INTO reservations (name, start,...) VALUES (:name, :start,...) WHERE id = :id";
Since you are doing an INSERT you should drop the WHERE clause.
e.g.
INSERT INTO reservations (name, start,...) VALUES (:name, :start,...)
We have a 3-layer model for product qualities.
Model 1 is the main model which contains product quality info.
#-------------------------------------------------------------------------------
# product qualities
#-------------------------------------------------------------------------------
class qualities(models.Model):
_name = 'qualities'
_description = 'Sheet Qualities'
name = fields.Char('Quality', required=True, help='Quality name')
reseller_ids = fields.One2many('qualityinfo', 'product_qual_id', 'Quality Supplier')
Each quality-type has multiple suppliers connectect by:
reseller_ids = fields.One2many('qualityinfo', 'product_qual_id', 'Quality Supplier')
#-------------------------------------------------------------------------------
# Quality supplier
#-------------------------------------------------------------------------------
class qualityinfo(models.Model):
_name = 'qualityinfo'
_description = "Information about a quality supplier"
name = fields.Many2one('res.partner', 'Supplier', required=True,domain = [('supplier','=',True)], ondelete='cascade')
product_code = fields.Char('Supplier Product Code')
sequence = fields.Integer('Sequence')
min_qty = fields.Float('Minimal Quantity', required=True)
product_qual_id = fields.Many2one('qualities', 'Quality Template', ondelete='cascade', select=True)
pricelist_ids = fields.One2many('pricelist.partnerinfo', 'suppinfo_id', 'Supplier Pricelist', copy=True)
company_id = fields.Many2one('res.company','Company',select=1)
#-------------------------------------------------------------------------------
# Quality suppliers pricelist
#-------------------------------------------------------------------------------
class pricelist_partnerinfo(models.Model):
_name = 'pricelist.partnerinfo'
name = fields.Char('Description')
suppinfo_id = fields.Many2one('qualityinfo', 'Partner Information', required=True, ondelete='cascade')
min_quantity = fields.Float('Quantity', required=True)
price = fields.Float('Unit Price', required=True)
_order = 'min_quantity asc'
So far so good. But now we need a single price from the pricelist into a price field in qualities. We have tried the following, without any result:
def _get_price(self):
quality_id = self._context.get('product_qual_id' False)
if product_qual_id:
price = self.env['qualityinfo'].browse(product_qual_id).with_context(pricelist.partnerinfo=self.id).price self.price = price
I need the price of pricelist.partnerinfo in quality to calculate total cost.
You can use for this compute field. Here example for your models:
class qualityinfo(models.Model):
_name = 'qualityinfo'
# ... your fields here
# value not stored in db. (default flag 'store=False')
total_price = fieldsFloat('Total price', compute='_compute_total_price')
#api.multi
def _compute_total_price(self):
total_price = 0
for pricelist in self.pricelist_ids:
total_price += pricelist.price
self.total_price = total_price
Hope this help you.