Skip to content

Commit

Permalink
Merge pull request #75 from prakashpp/feature/task-7223
Browse files Browse the repository at this point in the history
Refactor for AR pattern and code cleanup #7223
  • Loading branch information
tarunbhardwaj committed Mar 10, 2015
2 parents de2cf56 + 9967f81 commit d19ded5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 96 deletions.
42 changes: 19 additions & 23 deletions magento_.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ def test_connection(cls, instances):
:param instances: Active record list of magento instance
"""
if len(instances) != 1:
try:
instance, = instances
except ValueError:
cls.raise_user_error('multiple_instances')

instance = instances[0]
try:
with magento.API(
instance.url, instance.api_user, instance.api_key
Expand All @@ -182,11 +183,11 @@ def import_websites(cls, instances):
StoreView = Pool().get('magento.store.store_view')
MagentoOrderState = Pool().get('magento.order_state')

if len(instances) != 1:
try:
instance, = instances
except ValueError:
cls.raise_user_error('multiple_instances')

instance = instances[0]

with Transaction().set_context(magento_instance=instance.id):

# Import order states
Expand Down Expand Up @@ -361,7 +362,7 @@ def export_inventory(self, websites):

def export_inventory_to_magento(self):
"""
Exports stock data of products from openerp to magento for this
Exports stock data of products from tryton to magento for this
website
:return: List of product templates
Expand Down Expand Up @@ -743,7 +744,7 @@ def export_order_status(self, store_views=None):
:param store_views: List of active record of store view
"""
if not store_views:
if store_views is None:
store_views = self.search([])

for store_view in store_views:
Expand All @@ -767,9 +768,9 @@ def export_order_status_for_store_view(self):

sales = Sale.search(domain)

self.write([self], {
'last_order_export_time': datetime.utcnow()
})
self.last_order_export_time = datetime.utcnow()
self.save()

for sale in sales:
exported_sales.append(sale.export_order_status_to_magento())

Expand All @@ -782,7 +783,7 @@ def import_orders(cls, store_views=None):
:param store_views: Active record list of store views
"""
if not store_views:
if store_views is None:
store_views = cls.search([])

for store_view in store_views:
Expand All @@ -796,7 +797,7 @@ def export_shipment_status(cls, store_views=None):
:param store_views: List of active records of store_view
"""
if not store_views:
if store_views is None:
store_views = cls.search([])

for store_view in store_views:
Expand All @@ -821,6 +822,7 @@ def export_shipment_status_to_magento(self):
('magento_store_view', '=', self.id),
('shipment_state', '=', 'sent'),
('magento_id', '!=', None),
('shipments', '!=', None),
]

if self.last_shipment_export_time:
Expand All @@ -830,17 +832,14 @@ def export_shipment_status_to_magento(self):

sales = Sale.search(sale_domain)

self.last_shipment_export_time = datetime.utcnow()
self.save()

for sale in sales:
if not sale.shipments:
sales.pop(sale)
continue
# Get the increment id from the sale reference
increment_id = sale.reference[
len(instance.order_prefix): len(sale.reference)
]
self.write([self], {
'last_shipment_export_time': datetime.utcnow()
})

for shipment in sale.shipments:
try:
Expand Down Expand Up @@ -907,12 +906,9 @@ class StorePriceTier(ModelSQL, ModelView):
__name__ = 'magento.store.price_tier'

store = fields.Many2One(
'magento.website.store', 'Magento Store', required=True,
readonly=True,
)
quantity = fields.Float(
'Quantity', required=True
'magento.website.store', 'Magento Store', required=True, readonly=True,
)
quantity = fields.Float('Quantity', required=True)

@classmethod
def __setup__(cls):
Expand Down
35 changes: 21 additions & 14 deletions party.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ def find_using_magento_id(cls, magento_id):
"""
MagentoParty = Pool().get('magento.website.party')

magento_parties = MagentoParty.search([
('magento_id', '=', magento_id),
('website', '=', Transaction().context.get('magento_website'))
])

return magento_parties and magento_parties[0].party or None
try:
magento_party, = MagentoParty.search([
('magento_id', '=', magento_id),
('website', '=', Transaction().context.get('magento_website'))
])
except ValueError:
return None
else:
return magento_party.party

@classmethod
def find_or_create_using_magento_data(cls, magento_data):
Expand All @@ -88,7 +91,7 @@ def find_or_create_using_magento_data(cls, magento_data):
:param magento_data: Dictionary of values for customer sent by magento
:return: Active record of record created/found
"""
if not Transaction().context['magento_website']:
if Transaction().context.get('magento_website') is None:
cls.raise_user_error('website_not_found')

party = cls.find_using_magento_data(magento_data)
Expand Down Expand Up @@ -137,11 +140,15 @@ def find_using_magento_data(cls, magento_data):
"""
MagentoParty = Pool().get('magento.website.party')

magento_parties = MagentoParty.search([
('magento_id', '=', magento_data['customer_id']),
('website', '=', Transaction().context['magento_website'])
])
return magento_parties and magento_parties[0].party or None
try:
magento_party, = MagentoParty.search([
('magento_id', '=', magento_data['customer_id']),
('website', '=', Transaction().context['magento_website'])
])
except ValueError:
return None
else:
return magento_party.party


class MagentoWebsiteParty(ModelSQL, ModelView):
Expand Down Expand Up @@ -180,11 +187,11 @@ def check_unique_party(cls, records):
:param records: List of active records
"""
for magento_partner in records:
if magento_partner.magento_id != 0 and cls.search_count([
if magento_partner.magento_id != 0 and cls.search([
('magento_id', '=', magento_partner.magento_id),
('website', '=', magento_partner.website.id),
('id', '!=', magento_partner.id),
]) > 0:
], count=True) > 0:
cls.raise_user_error('party_exists')


Expand Down

0 comments on commit d19ded5

Please sign in to comment.