def get_company(request, company_id): try: c = Company.objects.get(id=company_id) except Company.DoesNotExist: return JsonError(_("Company does not exist")) return JsonOk(extra=company_to_dict(request.user, c, android=True))
def action_to_dict(user, notification, android=False): c = { 'id': notification.id, 'company': company_to_dict(user, notification.company, android, with_config=True), 'sender': notification.sender, 'receiver': notification.receiver, 'type': notification.type, 'status': notification.status, 'data': json.loads(notification.data), 'reference': notification.reference } return c
def edit_company(request, company_id): try: c = Company.objects.get(id=company_id) except Company.DoesNotExist: return JsonError(_("Company does not exist")) if not has_permission(request.user, c, 'company', 'edit'): return JsonError(message=_("edit company details")) data = JsonParse(request.POST['data']) validate_data = validate_company(request.user, c, data) if validate_data['status']: data = validate_data['data'] else: return JsonError(message=validate_data['message']) c.name = data.get('name') c.url_name = data.get('url_name') c.email = data.get('email') c.postcode = data.get('postcode') c.city = data.get('city') c.state = data.get('state') c.phone = data.get('phone') c.vat_no = data.get('vat_no') c.website = data.get('website') c.notes = data.get('notes') c.tax_payer = data.get('tax_payer') if data.get('change_color_logo'): if data.get('color_logo'): d = {'image': data.get('color_logo')} save_color_image(c, d) else: save_color_image(c, {}) if data.get('change_monochrome_logo'): if data.get('monochrome_logo'): d = {'image': data.get('monochrome_logo')} save_monochrome_image(c, d) else: save_monochrome_image(c, {}) c.save() return JsonOk(extra=company_to_dict(request.user, c, android=True))
def post(self, request, backend): if backend == 'auth': if request.META and 'HTTP_AUTHORIZATION' in request.META: user = get_user(request) else: serializer = self.serializer_class(data=request.DATA) if serializer.is_valid(): user = serializer.object['user'] else: return JsonError(message=_("wrong credentials")) elif backend == "google-oauth2": d = google_login_or_register(request, mobile=True) if d['status'] == 'ok': user = BlocklogicUser.objects.get(id=d['user_id']) else: return JsonResponse(d) else: return JsonError(message=_("wrong login")) token, created = Token.objects.get_or_create(user=user) if user: user_credentials = get_user_credentials(user) else: return JsonError(_("User authentication failed.")) companies = user.companies actions = get_actions(user) return JsonResponse({'token': token.key, 'user': user_credentials, 'actions': actions, 'companies': [company_to_dict(user, i, android=True, with_config=True) for i in companies], 'status': "ok"})
def bill_to_dict(user, company, bill): # fields in bill: # company # type # recipient_contact > FK contact # note # sub_total | # discount | decimal fields, with everything calculated # tax | # timestamp # status > choices in g.BILL_STATUS b = { "id": bill.id, "issuer": company_to_dict(user, bill.issuer), "register": register_to_dict(user, company, bill.register), "user_id": bill.user_id, "user_name": bill.user_name, "serial": bill.serial, "serial_prefix": bill.serial_prefix, "serial_number": bill.serial_number, "notes": bill.notes, "discount_amount": format_number(user, company, bill.discount_amount), "discount_type": bill.discount_type, "currency": bill.payment.currency, # prices "base": format_number(user, company, bill.base), "discount": format_number(user, company, bill.discount), "tax": format_number(user, company, bill.tax), "total": format_number(user, company, bill.total), "timestamp": format_date(user, company, bill.timestamp) + " " + format_time(user, company, bill.timestamp), } if bill.contact: b["contact"] = contact_to_dict(user, company, bill.contact) # get items items = list(BillItem.objects.filter(bill=bill)) # gather tax rates for all items grouped_taxes = group_tax_rates(items) tax_rates = grouped_taxes["rates"] tax_sums = grouped_taxes["sums"] for rate in tax_rates: rate["amount"] = format_number(user, company, rate["amount"]) rate["tax_sum"] = format_number(user, company, rate["tax_sum"]) rate["net_sum"] = format_number(user, company, rate["net_sum"]) rate["gross_sum"] = format_number(user, company, rate["gross_sum"]) tax_sums["tax_sum"] = format_number(user, company, tax_sums["tax_sum"]) tax_sums["net_sum"] = format_number(user, company, tax_sums["net_sum"]) tax_sums["gross_sum"] = format_number(user, company, tax_sums["gross_sum"]) b["tax_rates"] = tax_rates b["tax_sums"] = tax_sums # format all items i = [] for item in items: i.append(bill_item_to_dict(user, company, item)) b["items"] = i b["payment"] = payment_to_dict(user, company, bill.payment) return b
def mobile_sync_db(request, company_id): try: company = Company.objects.get(id=company_id) except Company.DoesNotExist: return JsonError(_("Company does not exist")) data = JsonParse(request.POST['data']) seq = data['database_version'] device_id = data['device_id'] sync_objects = Sync.objects.only('seq')\ .filter(company=company).order_by('-seq') last_key = 0 if len(sync_objects) > 0: last_key = sync_objects[0].seq ret = {'version': last_key} if seq == 0 or last_key - 5000 > seq or seq > last_key: # if first sync or sync outdated, why not load whole DB ret['updated'] = False ret['drop'] = True # get taxes ret['taxes'] = get_all_taxes(request.user, company) # get contacts contacts = Contact.objects.filter(company=company) cs = [] for contact in contacts: cs.append(contact_to_dict(request.user, company, contact)) ret['contacts'] = cs # get categories categories = Category.objects.filter(company=company) ct = [] for category in categories: ct.append(category_to_dict(category, android=True)) ret['categories'] = ct # get discounts discounts = Discount.objects.filter(company=company) ds = [] for discount in discounts: ds.append(discount_to_dict(request.user, company, discount, android=True)) ret['discounts'] = ds # get products products = Product.objects.filter(company=company) pr = [] for product in products: pr.append(product_to_dict(request.user, company, product, android=True)) ret['products'] = pr try: register = Register.objects.get(company=company, device_id=device_id) ret['register'] = register_to_dict(request.user, company, register) except Register.DoesNotExist: ret['register'] = None ret['company'] = company_to_dict(request.user, company, android=True, with_config=True) elif seq < last_key: ret['updated'] = False ret['drop'] = False seq_list = Sync.objects.filter(company=company, seq__lte=last_key).order_by('seq') items = [] for seq_item in seq_list: item_ret = {'action': seq_item.action, 'model': seq_item.model, 'object_id': seq_item.object_id} if seq_item.action == 'save': if seq_item.model == 'Category': c = Category.objects.get(id=seq_item.object_id) item_ret['item'] = category_to_dict(c, android=True) elif seq_item.model == 'Product': p = Product.objects.get(id=seq_item.object_id) item_ret['item'] = product_to_dict(request.user, company, p, android=True) elif seq_item.model == 'Tax': t = Tax.objects.get(id=seq_item.object_id) item_ret['item'] = tax_to_dict(request.user, company, t) elif seq_item.model == 'Discount': d = Discount.objects.get(id=seq_item.object_id) item_ret['item'] = discount_to_dict(request.user, company, d, android=True) elif seq_item.model == 'Contact': con = Contact.objects.get(id=seq_item.object_id) item_ret['item'] = contact_to_dict(request.user, company, con) elif seq_item.model == 'Register': r = Register.objects.get(id=seq_item.object_id) item_ret['item'] = register_to_dict(request.user, company, r) elif seq_item.model == 'Company': item_ret['item'] = company_to_dict(request.user, company, android=True, with_config=True) items.append(item_ret) ret['items'] = items else: ret['updated'] = True return JsonOk(extra=ret, safe=False)