Beispiel #1
0
def mobile_get_products(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, 'product', 'view'):
        return JsonError(_("You have no permission to view taxes"))

    products = Product.objects.filter(company=c)

    r = []
    for p in products:
        r.append(product_to_dict(request.user, c,  p, android=True))

    return JsonResponse(r, safe=False)
Beispiel #2
0
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)