コード例 #1
0
def state_edit(request):
    request_data = json.loads(request.body)
    country_id = request_data.get('country_id')
    state_id = request_data.get('id')
    form = StateForm(request_data)
    errors = {}

    is_active = request_data.get('is_active', None)
    if not is_active:
        errors['is_active'] = ['This field is required.']

    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    country = Country.objects.filter(id=country_id).first()
    if not country:
        return JsonResponse({}, status=400)

    state = State.objects.filter(id=state_id).first()
    if not state:
        return JsonResponse({}, status=400)

    state.is_active = request_data.get('is_active', '')
    state.name = request_data.get('name', '')
    state.country = country
    state.save()

    return JsonResponse({}, status=200)
コード例 #2
0
def page_category_create(request):
    request_data = json.loads(request.body)

    validator = Validator()
    form = PageCategoryForm(request_data)

    if not form.is_valid():
        validator.errors.errors = get_form_errors(form, {})

    market_id = request_data.get('market_id', None)
    if market_id:
        market = Market.objects.filter(pk=market_id).first()
        if not market:
            return JsonResponse({'general': 'Market not found.'}, status=500)

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    is_active = to_boolean(request_data['is_active'])
    role_id = request_data.get('role_id')

    category = PageCategory()
    category.is_active = is_active
    category.market = market
    category.name = request_data['name']
    category.role_id = role_id
    category.save()

    return JsonResponse({}, status=200)
コード例 #3
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def item_update(request):
    pk = request.POST.get('id', 0)

    validator = Validator()

    item_form = ItemForm(request.POST)
    if not item_form.is_valid():
        validator.errors.errors = get_form_errors(item_form, {})

    request_file = request.FILES.get('file', None)

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    item = Item.objects.filter(pk=pk).first()
    if not item:
        return JsonResponse({}, status=400)

    if request_file:
        item.attachment = request_file

    if 'description' in request.POST:
        item.description = item_form.cleaned_data['description']

    if 'amount' in request.POST:
        item.amount = item_form.cleaned_data['amount']

    if 'type_id' in request.POST:
        item.type_id = item_form.cleaned_data['type_id']

    if 'currency_id' in request.POST:
        item.currency_id = item_form.cleaned_data['currency_id']
        if item.currency_id == 6:
            item.exchange_rate = Decimal('1.00')
            item.total = item.amount

    if 'exchange_rate' in request.POST and item.currency_id != 6:
        item.exchange_rate = Decimal(request.POST['exchange_rate'])
        if item.amount and item.exchange_rate:
            item.total = (item.amount * item.exchange_rate).quantize(
                Decimal("0.01"), decimal.ROUND_HALF_DOWN)

    if 'date' in request.POST and item_form.cleaned_data['date']:
        item.date = item_form.cleaned_data['date']

    item.save()

    response = {'id': item.id}

    item = Item.objects.filter(pk=pk).first()

    if request_file:
        response['url'] = item.attachment.url

    return JsonResponse(response)
コード例 #4
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def goal_update(request):
    request_data = json.loads(request.body)

    goal_id = request_data.get('id')
    amount = request_data.get('amount')
    market_id = request_data.get('market_id')
    from_category_id = request_data.get('from_category_id')
    to_category_id = request_data.get('to_category_id')
    from_role_id = request_data.get('from_role_id')
    to_role_id = request_data.get('to_role_id')

    form = GoalForm(request_data)
    errors = {}
    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    market = Market.objects.filter(pk=market_id).first()
    if not market:
        error = {'market_id': ['Market does not exist.']}
        return JsonResponse(error, status=400)

    from_category = CommissionCategory.objects.filter(
        pk=from_category_id, market_id=market_id).first()
    if not from_category:
        error = {'from_category_id': ['Commission category does not exist.']}
        return JsonResponse(error, status=400)

    to_category = CommissionCategory.objects.filter(
        pk=to_category_id, market_id=market_id).first()
    if not to_category:
        error = {'to_category_id': ['Commission category does not exist.']}
        return JsonResponse(error, status=400)

    goal = Goal.objects.filter(pk=goal_id).first()
    if not goal:
        error = {'id': ['Goal category does not exist.']}
        return JsonResponse(error, status=400)

    goal.amount = amount
    goal.commission_from = from_category
    goal.commission_to = to_category
    goal.role_from = from_role_id
    goal.role_to = to_role_id
    goal.save()

    return JsonResponse({}, status=200)
コード例 #5
0
def city_edit(request):
    request_data = json.loads(request.body)

    validator = Validator()
    form = CityForm(request_data)

    if not form.is_valid():
        validator.errors.errors = get_form_errors(form, {})

    country_id = request_data.get('country_id', None)
    if country_id:
        country = Country.objects.filter(pk=country_id).first()
        if not country:
            return JsonResponse({'general': 'Country not found.'}, status=500)

    state_id = request_data.get('state_id', None)
    if state_id:
        state = State.objects.filter(pk=state_id).first()
        if not state:
            return JsonResponse({'general': 'State not found.'}, status=500)
        else:
            if state.country_id != country_id:
                validator.errors.add(
                    'state_id', 'This state do not belong to the country.')

    city_id = request_data.get('id', None)
    if not city_id:
        return JsonResponse({'general': 'City not found.'}, status=500)

    city = City.objects.filter(pk=city_id).first()
    if not city:
        return JsonResponse({'general': 'City not found.'}, status=500)

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    is_active = to_boolean(request_data['is_active'])

    city.name = request_data['name']
    city.state = state
    city.is_active = is_active
    city.save()

    return JsonResponse({}, status=200)
コード例 #6
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def deposit_account_create(request):
    request_data = json.loads(request.body)

    account_form = DepositAccountForm(request_data)

    if not account_form.is_valid():
        errors = get_form_errors(account_form, {})
        return JsonResponse(errors, status=400)

    account = DepositAccount()
    account.owner = request.user
    account.nickname = request_data['nickname']
    account.bank = request_data['bank']
    account.clabe = request_data['clabe']
    account.account_number = request_data['account_number']
    account.swift = request_data['swift']
    account.save()

    return JsonResponse(account.as_dict())
コード例 #7
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def commission_category_update(request):
    request_data = json.loads(request.body)
    category_id = request_data.get('id')
    market_id = request_data.get('market_id')

    form = CommissionCategoryForm(request_data)

    errors = {}
    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    category = CommissionCategory.objects.filter(pk=category_id).first()
    if not category:
        return JsonResponse({}, status=400)

    if market_id != category.market_id:
        market = Market.objects.filter(pk=market_id).first()
        if not market:
            return JsonResponse({}, status=400)

        count = ConsultantProfile.objects.only_consultants().is_active(
        ).filter(commission_category_id=category_id).count()

        if count > 0:
            error = {
                'no_field_errors':
                ['There is already users using this commission category']
            }
            return JsonResponse(error, status=400)

        ConsultantProfile.objects.filter(commission_category=category).update(
            commision_category=None)

        category.market = market

    category.name = request_data['name']
    category.primary = request_data['primary']
    category.secondary = request_data['secondary']
    category.ad_astra = request_data['ad_astra']
    category.save()

    return JsonResponse({}, status=200)
コード例 #8
0
def country_create(request):
    request_data = json.loads(request.body)
    form = CountryForm(request_data)
    errors = {}

    is_active = request_data.get('is_active', None)
    if not is_active:
        errors['is_active'] = ['This field is required.']

    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    country = Country()
    country.name = request_data['name']
    country.is_active = request_data['is_active']
    country.code = request_data['code']
    country.save()

    return JsonResponse({}, status=200)
コード例 #9
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def commission_special_update(request):
    request_data = json.loads(request.body)

    pk = request_data.get('id', 0)
    is_active = request_data.get('is_active', 0)
    request_data[u'action'] = 'edit'

    form = CommissionSpecialForm(request_data)

    errors = {}
    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    commission = CommissionSpecial.objects.filter(pk=pk).first()
    if not commission:
        return JsonResponse({}, status=400)

    commission.percentage = request_data.get('percentage')
    commission.is_active = is_active
    commission.save()

    return JsonResponse({}, status=200)
コード例 #10
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def commission_category_create(request):
    request_data = json.loads(request.body)

    form = CommissionCategoryForm(request_data)

    errors = {}
    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    market = Market.objects.filter(pk=request_data.get('market_id')).first()
    if not market:
        return JsonResponse({}, status=400)

    category = CommissionCategory()
    category.name = request_data['name']
    category.market = market
    category.primary = request_data['primary']
    category.secondary = request_data['secondary']
    category.ad_astra = request_data['ad_astra']
    category.save()

    return JsonResponse({}, status=200)
コード例 #11
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def commission_special_create(request):
    request_data = json.loads(request.body)

    market_id = request_data.get('market_id', 0)
    user_id = request_data.get('user_id', 0)
    is_active = request_data.get('is_active', 0)
    request_data[u'action'] = 'create'

    form = CommissionSpecialForm(request_data)

    errors = {}
    if not form.is_valid():
        errors = get_form_errors(form, errors)
        return JsonResponse(errors, status=400)

    market = Market.objects.filter(pk=market_id).first()
    if not market:
        return JsonResponse({}, status=400)

    user = get_user_model().objects.filter(pk=user_id).first()
    if not user:
        return JsonResponse({}, status=400)

    market = get_user_market(user)
    if not market:
        return JsonResponse({}, status=400)

    percentage = int(form.cleaned_data['percentage'])

    commission = CommissionSpecial()
    commission.market = market
    commission.user = user
    commission.percentage = percentage
    commission.is_active = is_active
    commission.save()

    return JsonResponse({}, status=200)
コード例 #12
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def deposit_account_edit(request):
    request_data = json.loads(request.body)

    account_form = DepositAccountForm(request_data)

    if not account_form.is_valid():
        errors = get_form_errors(account_form, {})
        return JsonResponse(errors, status=400)

    account = DepositAccount.objects.filter(pk=request_data['id']).first()
    if not account:
        return JsonResponse({}, status=400)

    if not account.owner_id == request.user.pk:
        return JsonResponse({}, status=400)

    account.nickname = request_data['nickname']
    account.bank = request_data['bank']
    account.clabe = request_data['clabe']
    account.account_number = request_data['account_number']
    account.swift = request_data['swift']
    account.save()

    return JsonResponse({})
コード例 #13
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def reimbursements_review(request):
    request_data = json.loads(request.body)

    pk = request_data.get('id', 0)

    reimbursement = Reimbursement.objects.prefetch_related('items').filter(
        pk=pk).first()
    if not reimbursement:
        return JsonResponse({}, status=400)

    items = reimbursement.items.all()

    validator = Validator()

    reimbursement_form = ReimbursementStrictForm({
        'account_id':
        reimbursement.account_id,
        'location':
        reimbursement.location,
        'spending_justification':
        reimbursement.spending_justification,
        'date_from':
        reimbursement.date_from,
        'date_to':
        reimbursement.date_to,
    })

    if not reimbursement_form.is_valid():
        validator.errors.errors = get_form_errors(reimbursement_form, {})

    if len(items) == 0:
        validator.errors.add('no_field_errors', 'Must be at least one item')
    else:
        item_errors = []
        for item in items:
            item_form = ItemStrictForm(item.as_dict())
            errors = {'id': item.pk}

            if not item_form.is_valid():
                errors = get_form_errors(item_form, errors)

            if not item.attachment:
                errors['file'] = ['File required.']

            if len(errors) > 1:
                item_errors.append(errors)

        if len(item_errors) > 0:
            validator.errors.errors['items'] = item_errors

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    main_currency = Currency.objects.filter(main=True).first()

    for item in reimbursement.items.all():
        if item.currency != main_currency:
            exchange = ExchangeRate.objects.filter(
                start_date__lte=item.date,
                end_date__gte=item.date,
                target=item.currency).first()

            exchange_rate = exchange.rate if exchange else Decimal('0.00')
            item.exchange_rate = exchange_rate
            item.total = (item.amount * exchange_rate).quantize(
                Decimal("0.01"), decimal.ROUND_HALF_DOWN)

        else:
            item.exchange_rate = Decimal('1.00')
            item.total = item.amount

        item.save()

    reimbursement.reject_reason = ''
    reimbursement.date_sent = dt.now()
    reimbursement.status = 3
    reimbursement.save()

    notification = create_notification_from(reimbursement)
    notification.creator = request.user
    notification.description = u'New reimbursement to review'
    notification.details = u'New reimbursement from {}'.format(
        request.user.person.get_full_name())
    notification.type = 12
    notification.save()

    user_ids = get_user_model().objects.filter(
        Q(groups__name="corporate admin")
        | Q(groups__name="finances")).values_list('id', flat=True)

    bulk_receiver_creation(notification, user_ids)

    return JsonResponse({})
コード例 #14
0
ファイル: api_views.py プロジェクト: wearetherobots/acs40
def reimbursements_update(request):
    deposit_account = None
    request_data = json.loads(request.body)
    new_items = request_data.get('new_items', [])
    remove_items = request_data.get('remove_items', [])
    update_items = request_data.get('update_items', [])

    reimbursement = Reimbursement.objects.prefetch_related(
        'account__owner').filter(pk=request_data.get('id', 0)).first()
    if not reimbursement:
        return JsonResponse({}, status=400)

    if request_data['account_id']:
        deposit_account = DepositAccount.objects.select_related(
            'owner__role').filter(pk=request_data['account_id']).first()
        if not deposit_account:
            return JsonResponse({}, status=400)

    validator = Validator()
    reimbursement_form = ReimbursementForm(request_data)

    if not reimbursement_form.is_valid():
        validator.errors.errors = get_form_errors(reimbursement_form, {})

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    reimbursement.creator = request.user
    reimbursement.location = request_data['location']
    reimbursement.description = request_data['description']
    reimbursement.spending_justification = request_data[
        'spending_justification']

    if deposit_account:
        reimbursement.account_id = deposit_account.id
        reimbursement.account_number = deposit_account.account_number
        reimbursement.swift = deposit_account.swift
        reimbursement.bank = deposit_account.bank
        reimbursement.clabe = deposit_account.clabe
        reimbursement.position = deposit_account.owner.role.name
    else:
        reimbursement.account_id = None
        reimbursement.account_number = ''
        reimbursement.swift = ''
        reimbursement.bank = ''
        reimbursement.clabe = ''
        reimbursement.position = ''

    if request_data['date_from']:
        reimbursement.date_from = request_data['date_from']

    if request_data['date_to']:
        reimbursement.date_to = request_data['date_to']

    reimbursement.save()

    items = []
    for new_item in new_items:
        item = Item()
        item.reimbursement = reimbursement

        if new_item['date']:
            item.date = new_item['date']

        item.type_id = new_item['type_id']
        item.description = new_item['description']
        item.amount = new_item['amount']
        item.currency_id = new_item['currency_id']
        item.save()

        if 'uid' in new_item:
            items.append({'id': item.id, 'uid': new_item['uid']})

    # TODO we can make this fastest if ask for all instead for every one
    for update_item in update_items:
        item = Item.objects.filter(pk=update_item['id'],
                                   reimbursement_id=reimbursement.pk).first()
        if not item:
            continue

        if update_item['date']:
            item.date = update_item['date']

        item.type_id = update_item['type_id']
        item.description = update_item['description']
        item.amount = update_item['amount']
        item.currency_id = update_item['currency_id']
        item.save()

    if len(remove_items) > 0:
        Item.objects.filter(pk__in=remove_items,
                            reimbursement_id=reimbursement.pk).delete()

    response = {}

    if len(items) > 0:
        response['items'] = items

    return JsonResponse(response)
コード例 #15
0
def page_category_edit(request):
    request_data = json.loads(request.body)

    validator = Validator()
    form = PageCategoryEditForm(request_data)

    role_id = request_data.get('role_id', None)
    if not form.is_valid():
        validator.errors.errors = get_form_errors(form, {})

    page_category_id = request_data.get('id', None)
    if page_category_id:
        page_category = PageCategory.objects.filter(
            pk=page_category_id).first()
        if not page_category:
            return JsonResponse({'general': 'Page Category not found.'},
                                status=500)

    market_id = request_data.get('market_id', None)
    if market_id:
        market = Market.objects.filter(pk=market_id).first()
        if not market:
            return JsonResponse({'general': 'Market not found.'}, status=500)

    if validator.has_errors():
        return JsonResponse(validator.get_errors(), status=400)

    to_create = request_data.get('to_create', [])
    to_remove = request_data.get('to_remove', [])
    to_edit = request_data.get('to_edit', [])

    for e_page in to_edit:
        id_page = e_page['id']
        name_page = e_page['name']
        if name_page:
            page = Page.objects.filter(pk=id_page).update(title=name_page)

    for r_page in to_create:
        page = Page()
        page.title = r_page['name']
        page.type = r_page['type']
        if page.type == 2:
            for icons in r_page['icons']:
                icons_id = icons['id']
        if page.type == 3 or page.type == 4:
            page.view = r_page['view']
        if page.type == 5:
            page.view_video = r_page['view_video']
        page.category = page_category
        page.save()

        if r_page['type'] == 2:
            for icons in r_page['icons']:
                icons_id = icons['id']
                attachment_page = AttachmentCategory.objects.get(pk=icons_id)
                page.attachment_category.add(attachment_page)

    Page.objects.filter(category_id=page_category_id,
                        id__in=to_remove).delete()

    page_category.role_id = role_id
    page_category.is_active = to_boolean(request_data['is_active'])
    page_category.market = market
    page_category.name = request_data['name']
    page_category.save()

    return JsonResponse({}, status=200)