I have written this code and it is fine, but the output of the dict is not what i want. Here is the code:
class EbExportCustomer(models.Model):
_inherit = 'res.partner'
#api.one
def get_pa_data(self):
aValues = defaultdict(dict)
aValues['partner_id'] = self.id
aValues['name'] = self.name
aValues['street'] = self.street
aValues['street2'] = self.street2
aValues['zip'] = self.zip
aValues['city'] = self.city
aValues['country'] = self.country_id.name
aValues['state'] = self.state_id.name
aValues['email'] = self.email
aValues['website'] = self.website
aValues['phone'] = self.phone
aValues['mobile'] = self.mobile
aValues['fax'] = self.fax
aValues['language'] = self.lang
aValues['child_ids']['name'] = []
aValues['child_ids']['function'] = []
aValues['child_ids']['email'] = []
if self.child_ids:
for child in self.child_ids:
aValues['child_ids']['name'].append(child.name)
aValues['child_ids']['function'].append(child.function)
aValues['child_ids']['email'].append(child.email)
return aValues
I am currently using dicttoxml and collections.defaultdict, The output is this:
<Partner><item>
<website>http://www.chinaexport.com/</website>
<city>Shanghai</city>
<fax>False</fax>
<name>China Export</name>
<zip>200000</zip>
<mobile>False</mobile>
<country>China</country>
<street2>False</street2>
<child_ids>
<function>
<item>Marketing Manager</item>
<item>Senior Consultant</item>
<item>Order Clerk</item>
<item>Director</item>
</function>
<name>
<item>Chao Wang</item>
<item>David Simpson</item>
<item>Jacob Taylor</item>
<item>John M. Brown</item>
</name>
<email><item>chao.wang#chinaexport.example.com</item> \ <item>david.simpson#epic.example.com</item><item>jacob.taylor#millennium.example.com</item><item>john.brown#epic.example.com</item></email></child_ids><phone>+86 21 6484 5671</phone><state>False</state><street>52 Chop Suey street</street><language>en_US</language><partner_id>9</partner_id><email>chinaexport#yourcompany.example.com</email>
But i would need the output for child_ids to be like:
<child_id>
< function >
Marketing
Manager
</function>
< name >Chao
Wang </ name >
< email > chao.wang # chinaexport.example.com </ email>
</child id>
And then another <child id> with the fields from all the other child ids.
Thanks in advance.
You want a single list (of dictionaries, probably), rather than three parallel lists. Something like this:
aValues['child_ids'] = []
for child in self.child_ids:
aValues['child_ids'].append({
'name': child.name,
'function': child.function,
'email': child.email
})
Related
For some reason, in my fruit scraper, i cannot access anything from listify function.
I'am getting an error, for exmaple: NameError: name 'family' is not defined.
And i cant figure out what is wrong with my code - is my function is bad, or i'am doing something wrong with class ?
import requests
import json
import random
import pickle
class FruitScraper():
def __init__(self):
self.name = []
self.id = []
self.family = []
self.genus = []
self.order = []
self.carbohydrates = []
self.protein = []
self.fat = []
self.calories = []
self.sugar = []
def scrape_all_fruits(self):
data_list = []
try:
for ID in range(1, 10):
url = f'https://www.fruityvice.com/api/fruit/{ID}'
response = requests.get(url)
data = response.json()
data_list.append(data)
except:
pass
return data_list
def listify(self, stats):
alist = json.dumps(self.scrape_all_fruits())
jsonSTr = json.loads(alist)
for i in jsonSTr:
try:
self.name.append(i['name'])
self.id.append(i['id'])
self.family.append(i['family'])
self.genus.append(i['genus'])
self.order.append(i['order'])
self.carbohydrates.append(i['nutritions']['carbohydrates'])
self.protein.append(i['nutritions']['protein'])
self.fat.append(i['nutritions']['fat'])
self.calories.append(i['nutritions']['calories'])
self.sugar.append(i['nutritions']['sugar'])
except:
pass
return stats
def get_summary(self):
for i in self.listify(zip(self.fat, self.protein, self.calories, self.sugar, self.carbohydrates, self.name)):
nutr_stats = f'\nNutrients maximum statistics:\nFat: {max(self.fat)}\nProtein: {max(self.protein)}\nCarbohydrates: {max(self.carbohydrates)}\nCalories: {max(self.calories)}\nSugar: {max(self.sugar)}' \
f'\nNutrients minimum statistics:\nFat: {min(self.fat)}\nProtein: {min(self.protein)}\nCarbohydrates: {min(self.carbohydrates)}\nCalories: {min(self.calories)}\nSugar: {min(self.sugar)}' \
f'\nTotal fruits scraped: {len(self.name)}'
return nutr_stats
Scraped_info = FruitScraper().scrape_all_fruits()
Listified_info = FruitScraper().listify(family)
Fruits_statistics = FruitScraper().get_summary()
It's my first time doing OOP.
Please consider changing this
Scraped_info = FruitScraper().scrape_all_fruits()
Listified_info = FruitScraper().listify(family)
Fruits_statistics = FruitScraper().get_summary()
to
myScraper = FruitScraper()
Scraped_info = myScraper.scrape_all_fruits()
myScraper.listify()
Fruits_statistics = myScraper.get_summary()
Otherwise you create three different objects of this class and discard them with all their attributes after running the individual method once.
This might also be critical to define family in this line of the code:
Listified_info = myScraper.listify(family)
But I can't see how you intended to use the parameter stats in your method listify(). It is just received and returned. I suggest that you change:
def listify(self, stats):
to
def listify(self):
and remove
return stats
If you want to get those lists inside the object of this class returned by listify(), you may do the following (but this is not OOP way of doing things):
import requests
import json
import copy
class FruitScraper():
def __init__(self):
self.name = []
self.id = []
self.family = []
self.genus = []
self.order = []
self.carbohydrates = []
self.protein = []
self.fat = []
self.calories = []
self.sugar = []
def collect_all_lists(self):
self.allLists = dict('name': self.name, 'id': self.id, 'family': self.family, 'genus': self.genus, 'order': self.order, 'carbohydrates': self.carbohydrates, 'protein': self.protein, 'fat': self.fat, 'calories': self.calories, 'sugar': self.sugar)
def scrape_all_fruits(self):
data_list = []
try:
for ID in range(1, 10):
url = f'https://www.fruityvice.com/api/fruit/{ID}'
response = requests.get(url)
data = response.json()
data_list.append(data)
except:
pass
return data_list
def listify(self):
alist = json.dumps(self.scrape_all_fruits())
jsonSTr = json.loads(alist)
for i in jsonSTr:
try:
self.name.append(i['name'])
self.id.append(i['id'])
self.family.append(i['family'])
self.genus.append(i['genus'])
self.order.append(i['order'])
self.carbohydrates.append(i['nutritions']['carbohydrates'])
self.protein.append(i['nutritions']['protein'])
self.fat.append(i['nutritions']['fat'])
self.calories.append(i['nutritions']['calories'])
self.sugar.append(i['nutritions']['sugar'])
except:
pass
self.collect_all_lists()
return copy.deepcopy(self.allLists)
def get_summary(self):
for i in self.listify(zip(self.fat, self.protein, self.calories, self.sugar, self.carbohydrates, self.name)):
nutr_stats = f'\nNutrients maximum statistics:\nFat: {max(self.fat)}\nProtein: {max(self.protein)}\nCarbohydrates: {max(self.carbohydrates)}\nCalories: {max(self.calories)}\nSugar: {max(self.sugar)}' \
f'\nNutrients minimum statistics:\nFat: {min(self.fat)}\nProtein: {min(self.protein)}\nCarbohydrates: {min(self.carbohydrates)}\nCalories: {min(self.calories)}\nSugar: {min(self.sugar)}' \
f'\nTotal fruits scraped: {len(self.name)}'
return nutr_stats
myScraper = FruitScraper()
Scraped_info = myScraper.scrape_all_fruits()
Listified_info = myScraper.listify()
Fruits_statistics = myScraper.get_summary()
This method to get the product price from the PO, and it works well if the PO have only one record otherwise I am getting this error.
raise ValueError("Expected singleton: %s" % self)
This is the method
#api.multi
def create_refund_invoice(self):
inv_obj = self.env['account.invoice']
for pick in self.filtered(lambda x:x.return_type):
type = 'in_refund' if pick.return_type == 'purchase' else 'out_refund'
inv_lines = {'type':type, 'partner_id':pick.partner_id.id, 'invoice_line_ids':[]}
account = pick.return_type == 'sale' and pick.partner_id.property_account_receivable_id.id or pick.partner_id.property_account_payable_id.id
inv_lines['account_id'] = account
inv_lines['origin'] = pick.name
inv_lines['name'] = pick.origin
for line in pick.move_lines:
name = line.product_id.partner_ref
for rec in self:
rec.order_id = line.env['purchase.order'].search([('name', '=', line.origin)]).order_line
rec.price = rec.order_id.price_unit
inv_lines['invoice_line_ids'] += [(0, None, {
'product_id':line.product_id.id,
'name':name,
'quantity':line.quantity_done,
'price_unit': rec.price,
'account_id':line.product_id.product_tmpl_id.get_product_accounts()['income'].id})]
if inv_lines['invoice_line_ids']:
inv_id = inv_obj.create(inv_lines)
pick.invoice_id = inv_id.id
It is necessary for odoo that when you are getting more than one record then you can not access it's field values directly.
In your code you are trying to get purchase_order_line of purchase_order It may possible that many lines are available in a single order.
def create_refund_invoice(self):
purchase_order_obj = self.env['purchase.order']
inv_obj = self.env['account.invoice']
for pick in self.filtered(lambda x:x.return_type):
type = 'in_refund' if pick.return_type == 'purchase' else 'out_refund'
inv_lines = {'type':type, 'partner_id':pick.partner_id.id, 'invoice_line_ids':[]}
account = pick.return_type == 'sale' and pick.partner_id.property_account_receivable_id.id or pick.partner_id.property_account_payable_id.id
inv_lines['account_id'] = account
inv_lines['origin'] = pick.name
inv_lines['name'] = pick.origin
for line in pick.move_lines:
name = line.product_id.partner_ref
for rec in self:
order_lines = purchase_order_obj.search([('name', '=', line.origin)]).order_line
for pol in order_lines:
price = pol.order_id.price_unit
inv_lines['invoice_line_ids'] += [(0, None, {
'product_id':line.product_id.id,
'name':name,
'quantity':line.quantity_done,
'price_unit': price,
'account_id':line.product_id.product_tmpl_id.get_product_accounts()['income'].id})
]
if inv_lines['invoice_line_ids']:
inv_id = inv_obj.create(inv_lines)
pick.invoice_id = inv_id.id
I have updated code test above code and update it as per your requirement.
I create new record with the method named create() in local database with data pulled away from remote database.As we all know,there are four normal fields in Odoo such as create_date,write_date,create_uid,write_uid.I want these data to be in a remote databaseļ¼but when I use method named create() to create the record,these data are the data at the time of local creation and not the remote data.
For example,in remote database,the creat_date is '2019-10-11',I can't change the value that is finally written to the local database even if I pass the value of the remote database into the dictionary.Finally,the value of field named create_date is '2019-12-03' rather than '2019-10-11'.The '2019-12-03' is the date now.The situation is similar for other fields such as write_date,create_uid,write_uid.
Please help me,thanks to everyone who thought about this question.
Following is my code.
The Class Model
class ReportRentalIncomeFromProperty(models.Model):
_name = 'report.rental.income.from.property'
_description = 'The report about the income from property rental'
_order = 'product_id, start_date'
# create_date = fields.Datetime('Created on')
create_uid = fields.Char('Created by')
# write_date = fields.Datetime('Last Modified on')
write_uid = fields.Char('Last Contributor')
product_id = fields.Many2one('product.product', 'Property House')
area_id = fields.Many2one('res.country.area', 'City')
district_id = fields.Many2one('res.country.district', 'District')
town_id = fields.Many2one('res.country.town', 'Town')
road_name = fields.Char('Road')
start_date = fields.Date('Start Date')
end_date = fields.Date('End Date')
should_pay = fields.Float('Should Pay')
real_pay = fields.Float('Real Pay')
balance_pay = fields.Float('Balance Pay')
rental_compliance_rate = fields.Float('Rental Compliance Rate(%)', group_operator="avg")
company_id = fields.Many2one('res.company', string='Company')
parent_company_id = fields.Many2one('res.company', related='company_id.parent_id', store=True,
string='Parent Company')
actual_business = fields.Many2many(
'rh.commercial.activities',
'house_rental_rent_income_business_db',
'actual_business_id',
'commercial_activities_id',
string='Actual business')
The function to pull away remote data to create new record in local database.
#api.multi
def synchronization_contract_performance_rate(self):
self.env['report.rental.income.from.property'].search([]).unlink()
product_dict = {}
A_product = self.env['product.product'].search([])
for a in A_product:
product_dict[a.name] = a.id
activities_dict = {}
D_activities = self.env['rh.commercial.activities'].search([])
for d in D_activities:
activities_dict[d.name] = d.id
address_dict = {}
i = 0
address_model_list = ['res.country.area', 'res.country.district', 'res.country.town']
address_field_list = ['area_id', 'district_id', 'town_id']
for addr in address_model_list:
C_address = self.env[addr].search([])
addr_dict = {}
for c in C_address:
addr_dict[c.name] = c.id
address_dict[i] = addr_dict
i += 1
record_list_1 = self.company_recursive_func()
for list_1 in record_list_1:
database = list_1[0]
link_url = list_1[1]
if link_url.startswith('http://'):
_uri = link_url.replace('http://', '')
my_odoo = odoorpc.ODOO(_uri, port=48080)
if link_url.startswith('https://'):
_uri = link_url.replace('https://', '')
my_odoo = odoorpc.ODOO(_uri, port=443, protocol='jsonrpc+ssl')
username = list_1[2]
password = list_1[3]
my_odoo.login(database, username, password)
company_id = list_1[4]
company_code = list_1[5]
product_actual_business_dict = {}
A_product_actual_business_ids = my_odoo.env['product.product'].search([])
A_product_actual_business = my_odoo.execute('product.product', 'read', A_product_actual_business_ids,
['actual_business'])
for a in A_product_actual_business:
name_list = []
for b in my_odoo.execute('rh.commercial.activities', 'read', a.get('actual_business'), ['name']):
name_list.append(b.get('name'))
product_actual_business_dict[a.get('id')] = name_list
remote_ids = my_odoo.env['report.rental.income.from.property'].search([])
remote_data_dict = my_odoo.execute('report.rental.income.from.property', 'read', remote_ids, ['product_id',
'start_date',
'create_date',
'create_uid',
'write_date',
'write_uid',
'end_date',
'should_pay',
'balance_pay',
'real_pay',
'rental_compliance_rate',
'area_id',
'road_name',
'district_id',
'town_id'])
for data in remote_data_dict:
remote_product_name = data.get('product_id')[1]
product_id = product_dict.get(remote_product_name + '(' + company_code + ')',
None)
if product_id:
i = 0
address_id_list = []
for address_field in address_field_list:
if data.get(address_field):
remote_address_name = data.get(address_field)[1]
local_address_id = address_dict[i].get(remote_address_name, None)
address_id_list.append(local_address_id)
else:
address_id_list.append(None)
i += 1
ids_list = []
find_names = product_actual_business_dict.get(data.get('product_id')[0])
for find_name in find_names:
id = activities_dict.get(find_name, None)
if id:
ids_list.append(id)
value = {
'product_id': product_id,
'area_id': address_id_list[0],
'district_id': address_id_list[1],
'town_id': address_id_list[2],
'road_name': data['road_name'],
'start_date': data['start_date'],
'end_date': data['end_date'],
'should_pay': data['should_pay'],
'real_pay': data['real_pay'],
'create_date': data['create_date'],
'create_uid': data['create_uid'][1],
'write_date': data['write_date'],
'write_uid': data['write_uid'][1],
'balance_pay':data['balance_pay'],
'rental_compliance_rate': data['rental_compliance_rate'],
'company_id': company_id,
'actual_business': [(6, 0, ids_list)]
}
self.env['report.rental.income.from.property'].create(value)
my_odoo.logout()
You can change standart odoo fields after you create your record with sql query
property_id = self.env['report.rental.income.from.property'].create(value)
self.env.cr.execute("UPDATE report_rental_income_from_property SET create_date='%s', create_uid=%s, write_date='%s', write_uid=%s WHERE id=%s" %
(value['create_date'], value['create_uid'], value['write_date'], value['write_uid'], property_id))
I have a method that tells the times that a worker makes a sale, but it does not work because it only marks me that he has made 1 but has actually done 5. Next I leave an image and the code used to guide me.
**class Worker (models.Model):**
_name = 'project_rc.worker'
sales_counter = fields.Integer (string = "Sales made", compute = "get_sales_made")
document_ids = fields.One2many (comodel_name = 'project_rc.document',
inverse_name = 'worker_id', string = 'Invoice')
def get_sales_made (self):
count = self.env ['project_rc.type_movement']. search_count ([('type_movement', '=', 'sale')])
self.counter_sale = count
**class Document (models.Model):**
type_movement_id = fields.Many2one (comodel_name = 'project_rc.type_movement', string = "Movement type")
worker_id = fields.Many2one (asdel_name = 'project_rc.worker', string = "Worker")
**class Type_Movement (models.Model):**
type_movement = fields.Selection ([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string = "Movement type")
document_ids = fields.One2many (comodel_name = 'project_rc.document', inverse_name = 'type_movimiento_id', string = 'Document')
Sample picture: https://ibb.co/vs0dw5K
The problem came from your function get_sales_made
class Worker(models.Model):
_name = 'project_rc.worker'
sales_counter = fields.Integer(string="Sales made", compute="get_sales_made")
document_ids = fields.One2many('project_rc.document', 'worker_id', string='Invoice')
#api.depends('document_ids')
def get_sales_made(self):
for rec in self:
document = rec.document_ids.filtered(lambda r: r.type_movement_id and r.type_movement_id.type_movement == 'sale')
rec.sales_counter = len(document)
class Document(models.Model):
_name = 'project_rc.document'
type_movement_id = fields.Many2one('project_rc.type_movement', string="Movement type")
worker_id = fields.Many2one('project_rc.worker', string="Worker")
class Type_Movement(models.Model):
_name = 'project_rc.type_movement'
type_movement = fields.Selection([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string="Movement type")
document_ids = fields.One2many('project_rc.document', 'type_movement_id', string='Document')
You are searching in the wrong table it should be project_rc.document
self.env['project_rc.document'].search_count([('type_movement_id.type_movement', '=', 'sale')
('worker_id', '=', rec.id)
])
Or you can simply filter document_ids to count sales.
I am trying to update a listfield of embedded documents in mongoengine. I have gone through almost all the related questions but somehow my code just won't work. I know there is something small that I'm missing but I can't figure it out.
Here is my code :
Documents:
class Address(EmbeddedDocument):
line1 = StringField(required=True, max_length=63)
line2 = StringField(max_length=127)
pin = StringField(max_length=6, min_length=6)
class Store(EmbeddedDocument):
code = StringField(max_length=50, required=True)
store_name = StringField(max_length=255, required=True)
address = EmbeddedDocumentField(Address)
class Vendor(Document):
name = StringField(max_length=255, required=True)
stores = ListField(EmbeddedDocumentField(Store))
View:
def stores(request, *args, **kwargs):
.......
elif request.method == 'POST':
if request.is_ajax():
if request.POST.get('oper') == 'edit':
post_dict = request.POST
# updated_vendor = Vendor.objects(pk=vendor_pk, stores__$__)
vendor.update_store(vendor_pk, post_dict)
return get_json_response({'msg': 'Store details updated successfully!!'})
else:
....
def update_store(self, vendor_pk, post_dict):
print post_dict
store = [attr for attr in self.stores if attr.code == post_dict.get('code') and attr.store_name == post_dict.get('store_name')]
vendor = self
new_address = Address(line1=post_dict.get('line1'), line2=post_dict.get('line2'),
city=post_dict.get('city'), state=post_dict.get('state'),
country=post_dict.get('country'))
if post_dict.get('pin') != 'None':
new_address.pin = post_dict.get('pin')
print "address new", new_address.line2, new_address.pin
new_store = Store(code=post_dict.get('code'), store_name=post_dict.get('store_name'), address=new_address)
print "new store", new_store.address.line2, new_store.address.pin
if store:
index = vendor.stores.index(store[0])
updated = Vendor.objects(pk=vendor_pk, stores__code=post_dict.get('code'), stores__name=post_dict.get('store_name')).update_one(set__stores__index=new_store)
print "updated", updated
#print index,vendor.stores[index].address.city
# del vendor.stores[index]
# vendor.stores.append(new_store)
# vendor.stores[index].code = post_dict.get('code')
# vendor.stores[index].store_name = post_dict.get('store_name')
# vendor.stores[index].address.line1 = post_dict.get('line1')
# vendor.stores[index].address.line2 = post_dict.get('line2')
# if post_dict['pin'] != 'None':
# vendor.stores[index].address.pin = post_dict.get('pin')
# vendor.save()
These are my print statements' output :
<QueryDict: {u'oper': [u'edit'], u'code': [u'BSK'], u'pin': [u'1'], u'line2': [u'near huda city center'], u'line1': [u'Shushant Lok'], u'store_name': [u'Baskin'], u'id': [u'2']}>
address new near huda city center 1
new store near huda city center 1
updated 0
My update_store method just won't work. Please Help.
Ok So I got my code to work now.
I modified my update_store method to this and it worked.
def update_store(self, post_dict):
vendor = self
new_address = Address(line1=post_dict.get('line1'), line2=post_dict.get('line2'))
if post_dict.get('pin') != 'None':
new_address.pin = post_dict.get('pin')
new_store = Store(code=post_dict.get('code'), store_name=post_dict.get('store_name'), address=new_address)
index = int(post_dict.get('id')) -1
stores = vendor.get_active_stores()
stores[index].is_active = False
vendor.stores.append(new_store)
vendor.save()