Skip to content

Commit 6c8ae51

Browse files
Merge pull request #69 from usermicrodevices/develop
change api doc cash format field customer
2 parents 9cb079e + 0d02307 commit 6c8ae51

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,4 @@ static/
177177
*.pem
178178
*.key
179179
*.xml
180+
*.pdf

api/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_docs(self):
144144
print()
145145
url = '/api/doc/cash/'
146146
prods = get_model('refs.Product').objects.all()[:5]
147-
data = json.dumps({'sum_final':1000, 'registered_at':datetime.now().astimezone().strftime('%Y-%m-%dT%H:%M:%S %z'), 'records':[{'product':p.id, 'count':10, 'price':f'{p.price}'} for p in prods]})
147+
data = json.dumps({'sum_final':1000, 'registered_at':datetime.now().astimezone().strftime('%Y-%m-%dT%H:%M:%S %z'), 'records':[{'product':p.id, 'count':10, 'price':f'{p.price}'} for p in prods], 'customer':{'name':'New Customer'}})
148148
print('⚽POST', url, data)
149149
response = self.client.post(url, data, 'json', headers={'X-CSRFToken':self.csrfmiddlewaretoken})
150150
print('CONTENT♡', response.content)

api/views.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,30 @@ def post(self, request, *args, **kwargs):
273273
dtype = data.get('type', 'sale')
274274
doc_type, created = DocType.objects.get_or_create(alias=dtype, defaults={'alias':dtype, 'name':dtype.title()})
275275
doc = Doc(type=doc_type, registered_at=parse_datetime(registered_at), owner_id=id_owner, contractor_id=id_contractor, author=request.user, sum_final=sum_final)
276-
id_customer = data.get('customer', None)
277-
if id_customer:
278-
doc.customer_id = id_customer
276+
customer = data.get('customer', {'id':None, 'name':'', 'extinfo':{}})
277+
if not isinstance(customer, dict):
278+
self.logw(customer, 'CUSTOMER MUST BE AS DICTIONARY')
279+
else:
280+
if customer.get('id', None):
281+
doc.customer_id = id_customer
282+
else:
283+
customer_name = customer.get('name', '')
284+
if customer_name:
285+
dbcustomer = None
286+
try:
287+
dbcustomer = Customer.objects.get(name=customer_name)
288+
except Customer.DoesNotExist as e:
289+
self.logw(e)
290+
dbcustomer = Customer(name=customer_name)
291+
try:
292+
dbcustomer.save()
293+
except Exception as e:
294+
self.loge(e, customer)
295+
dbcustomer = None
296+
except Exception as e:
297+
self.loge(e, customer)
298+
if dbcustomer:
299+
doc.customer_id = dbcustomer.id
279300
recs = []
280301
for r in records:
281302
try:

0 commit comments

Comments
 (0)