コード例 #1
0
def update(request, data, table_id):
    # Get table instance.
    try:
        table_record = Table.objects.get(id=table_id)
    except ValidationError:
        return custom_error_404(request, data)
    except ObjectDoesNotExist:
        return custom_error_404(request, data)

    if request.method == 'POST':
        # Update table data.
        table_record.name = request.POST['table_name']
        table_record.date = request.POST['date']
        table_record.save()

        # Update items data.
        for i, item in enumerate(table_record.items.all()):
            item.name = request.POST['item_name_' + str(i)]
            item.money_change = request.POST['item_money_change_' + str(i)]
            item.save()

        # Success
        data['alerts'].append(('success', 'Update successfully!',
                               'You have successfully update a table.'))
        return redirect_with_data(request, data,
                                  '/tables/details/' + table_id + '/')
    else:
        data['table'] = table_record
        return render(request, 'tables/update.html', data)
コード例 #2
0
def signup(request):
    # Collect alerts.
    alerts = get_alerts(request)

    # Test whether the user has logged in.
    if request.user.is_authenticated():
        alerts.append(
            ('info', 'You have logged in!',
             'If you want to sign up a new account, please log out first.'))
        request.session['alerts'] = alerts
        return HttpResponseRedirect('/info/')

    data = {'alerts': alerts}

    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db(
            )  # load the staff instance created by the signal
            user.staff.full_name = form.cleaned_data.get('full_name')
            user.staff.age = form.cleaned_data.get('age')
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            direct_login(request, user)
            return redirect_with_data(request, data, '/info/')
    else:
        form = SignUpForm()

    data['form'] = form
    return render(request, 'accounts/signup.html', data)
コード例 #3
0
def update(request, data, salary_id):
    # Get salary instance.
    try:
        salary_record = Salary.objects.get(id=salary_id)
    except ValidationError:
        return custom_error_404(request, data)
    except ObjectDoesNotExist:
        return custom_error_404(request, data)

    if request.method == 'POST':
        # Get form data.
        base_salary = request.POST['base_salary']
        bonus = request.POST['bonus']
        total = request.POST['total']
        date = request.POST['date']

        # Update salary.
        salary_record.base_salary = base_salary
        salary_record.bonus = bonus
        salary_record.total = total
        salary_record.date = date
        salary_record.save()

        # Success
        data['alerts'].append(
            ('success', 'Update successfully!',
             'You have successfully updated a record of salary.'))
        return redirect_with_data(request, data,
                                  '/salary/details/' + salary_id + '/')
    else:
        data['salary'] = salary_record
        return render(request, 'salary/update.html', data)
コード例 #4
0
def update(request, data, company_uuid):
    # Get Company instance.
    try:
        company = Company.objects.get(unique_id=company_uuid)
    except ValidationError:
        return custom_error_404(request, data)
    except ObjectDoesNotExist:
        return custom_error_404(request, data)

    if request.method == 'POST':
        # Get form data.
        name = request.POST['name']

        # Update company.
        company.name = name
        company.save()

        # Success
        data['alerts'].append(
            ('success', 'Update successfully!',
             'You have successfully updated information of a company.'))
        return redirect_with_data(request, data,
                                  '/companies/details/' + company_uuid + '/')
    else:
        data['company'] = company
        return render(request, 'companies/update.html', data)
コード例 #5
0
def fire_staff(request, data):
    if request.method == 'POST':
        # Collect form data.
        unique_id = request.POST['unique_id']
        employer_id = request.POST['employer_id']

        # Get Company instance.
        company = Company.objects.get(unique_id=unique_id)

        # Get Staff instance.
        employee = Staff.objects.get(id=employer_id)

        # Fire
        company.staff = company.staff.all().exclude(id=employer_id)
        company.save()
        employee.workplaces = employee.workplaces.all().exclude(
            unique_id=unique_id)
        employee.save()

        # Success
        data['alerts'].append(('success', 'Delete successfully!',
                               'You have successfully fired an employee.'))
        return redirect_with_data(request, data,
                                  '/companies/manage/' + unique_id + '/')
    else:
        return custom_error_404(request, data)
コード例 #6
0
def receipts(request, data, **kwargs):
    staff = Staff.objects.get(user=request.user)
    joined_workplaces = staff.workplaces.all()

    # Conditions on two kinds of url.
    if 'workplace_uuid' not in kwargs and 'page_num' not in kwargs:
        # No parameters

        # Test whether the staff has workplaces.
        if not joined_workplaces:
            data['no_workplaces'] = True
            return render(request, 'receipts/index.html', data)
        first_workplace_uuid = joined_workplaces[0].unique_id
        return redirect_with_data(request, data, '/receipts/' + str(first_workplace_uuid) + '/1/')
    else:
        # Two parameters
        data['no_workplaces'] = False
        data['joined_workplaces'] = joined_workplaces

        workplace_uuid = kwargs['workplace_uuid']
        page_num = kwargs['page_num']
        page_num = int(page_num)

        # If page number is zero.
        if page_num == 0:
            return custom_error_404(request, data)

        # If workplace_uuid is invalid...
        try:
            workplace = Company.objects.get(unique_id=workplace_uuid)
        except ValidationError:
            return custom_error_404(request, data)
        except ObjectDoesNotExist:
            return custom_error_404(request, data)

        # Get UUID.
        data['workplace_uuid'] = workplace.unique_id

        # Get all receipts in the company.
        receipt_records = Receipt.objects.filter(company=workplace)

        # If there is no receipt in company...
        if not receipt_records:
            data['no_receipt'] = True
        else:
            data['no_receipt'] = False

            # Get sliced records.
            data['receipt_records'], data['page_end'] = get_slice_and_page_end(receipt_records, page_num)

            # Get other necessary data
            data['page_range'] = range(1, data['page_end'] + 1)
            data['page_num'] = page_num

            # If sliced records are empty...
            if not data['receipt_records']:
                return custom_error_404(request, data)

        return render(request, 'receipts/index.html', data)
コード例 #7
0
def create(request, data, **kwargs):
    if request.method == 'POST':
        # Get receipt data.
        creator = Staff.objects.get(user=request.user)
        payer = request.POST['payer']
        payee = request.POST['payee']
        date = request.POST['date']
        address = request.POST['address']
        notes = request.POST['notes']
        workplace = Company.objects.get(unique_id=request.POST['workplace_uuid'])

        # Create new Receipt instance.
        new_receipt = Receipt(creator=creator, payer=payer, payee=payee, total_amount=0, date=date,
                              address=address, notes=notes, company=workplace)
        new_receipt.save()

        # Get items data.
        for i in range(0, 7):
            if 'name_' + str(i) not in request.POST:
                break
            name = request.POST['name_' + str(i)]
            spec = request.POST['spec_' + str(i)]
            number = request.POST['number_' + str(i)]
            unit = request.POST['unit_' + str(i)]
            price = request.POST['price_' + str(i)]
            total_cost = request.POST['total_cost_' + str(i)]

            # Create new Item instance.
            new_item = Item(name=name, spec=spec, number=number, unit=unit, price=price, total_cost=total_cost)
            new_item.save()

            # Update receipt instance.
            new_receipt.items.add(new_item)
            new_receipt.total_amount += int(total_cost)
            new_receipt.save()

        # Success
        data['alerts'].append(('success', 'Create successfully!', 'You have successfully create a new receipt.'))
        return redirect_with_data(request, data, '/receipts/' + request.POST['workplace_uuid'] + '/1/')
    else:
        if 'workplace_uuid' not in kwargs:
            return custom_error_404(request, data)

        workplace_uuid = kwargs['workplace_uuid']

        # If workplace_uuid is invalid...
        try:
            workplace = Company.objects.get(unique_id=workplace_uuid)
        except ValidationError:
            return custom_error_404(request, data)
        except ObjectDoesNotExist:
            return custom_error_404(request, data)

        # Get UUID.
        data['workplace_uuid'] = workplace.unique_id

        return render(request, 'receipts/create.html', data)
コード例 #8
0
def create(request, data, **kwargs):
    if request.method == 'POST':
        # Get table data.
        creator = Staff.objects.get(user=request.user)
        workplace = Company.objects.get(
            unique_id=request.POST['workplace_uuid'])
        table_name = request.POST['table_name']
        date = request.POST['date']

        # Create new Table instance.
        new_table = Table(creator=creator,
                          company=workplace,
                          date=date,
                          name=table_name)
        new_table.save()

        # Get items data.
        i = 0
        while 'item_name_' + str(i) in request.POST:
            item_name = request.POST['item_name_' + str(i)]
            money_change = request.POST['item_money_change_' + str(i)]

            # Create new Item instance
            new_item = Item(name=item_name, money_change=money_change)
            new_item.save()

            # Update receipt instance.
            new_table.items.add(new_item)

            i = i + 1

        # Success
        data['alerts'].append(('success', 'Create successfully!',
                               'You have successfully create a new table.'))
        return redirect_with_data(
            request, data, '/tables/' + request.POST['workplace_uuid'] + '/1/')
    else:
        if 'workplace_uuid' not in kwargs:
            return custom_error_404(request, data)

        workplace_uuid = kwargs['workplace_uuid']

        # If workplace_uuid is invalid...
        try:
            workplace = Company.objects.get(unique_id=workplace_uuid)
        except ValidationError:
            return custom_error_404(request, data)
        except ObjectDoesNotExist:
            return custom_error_404(request, data)

        # Get UUID.
        data['workplace_uuid'] = workplace.unique_id

    return render(request, 'tables/create.html', data)
コード例 #9
0
def join(request, data):
    if request.method == 'POST':
        # Collect form data.
        unique_id = request.POST['unique_id']

        # Get Company instance.
        try:
            company = Company.objects.get(unique_id=unique_id)
        except ValidationError:
            data['alerts'].append(
                ('error', 'Failed to join!', 'The UUID is invalid!'))
            return redirect_with_data(request, data, '/companies/join/')
        except ObjectDoesNotExist:
            data['alerts'].append(
                ('error', 'Failed to join!',
                 'The company with this UUID does not exist!'))
            return redirect_with_data(request, data, '/companies/join/')

        # Add the company to workplaces.
        staff = Staff.objects.get(user=request.user)

        # Add staff instance to staff field of company.
        company.staff.add(staff)

        # Test whether the user has already joined the company.
        if company in staff.workplaces.all():
            data['alerts'].append(('warning', 'Failed to join.',
                                   'You have already joined in this company!'))
            return redirect_with_data(request, data, '/companies/1+1/#tab1')

        staff.workplaces.add(company)

        # Success
        data['alerts'].append(('success', 'Join successfully!',
                               'You have successfully joined in a company.'))
        return redirect_with_data(request, data, '/companies/')
    else:
        return render(request, 'companies/join.html', data)
コード例 #10
0
def custom_login(request):
    data = {}

    # Collect alerts.
    alerts = get_alerts(request)
    data['alerts'] = alerts

    # Test whether the user has logged in.
    if request.user.is_authenticated():
        return redirect_with_data(request, data, '/info/')
    else:
        return login(request,
                     template_name='accounts/login.html',
                     extra_context=data)
コード例 #11
0
def delete(request, data):
    if request.method == 'POST':
        # Collect form data.
        receipt_id = request.POST['receipt_id']

        # Get Receipt instance.
        receipt = Receipt.objects.get(id=receipt_id)

        # Delete
        receipt.delete()

        # Success
        data['alerts'].append(('success', 'Delete successfully!', 'You have successfully deleted a receipt.'))
        return redirect_with_data(request, data, '/receipts/' + request.POST['workplace_uuid'] + '/1/')
    else:
        return custom_error_404(request, data)
コード例 #12
0
def delete(request, data):
    if request.method == 'POST':
        # Collect form data.
        unique_id = request.POST['unique_id']

        # Get Company instance.
        company = Company.objects.get(unique_id=unique_id)

        # Delete
        company.delete()

        # Success
        data['alerts'].append(('success', 'Delete successfully!',
                               'You have successfully deleted a company.'))
        return redirect_with_data(request, data, '/companies/')
    else:
        return custom_error_404(request, data)
コード例 #13
0
def delete(request, data):
    if request.method == 'POST':
        # Collect form data.
        table_id = request.POST['table_id']

        # Get Salary instance.
        table_record = Table.objects.get(id=table_id)

        # Delete
        table_record.delete()

        # Success
        data['alerts'].append(('success', 'Delete successfully!',
                               'You have successfully deleted a table.'))
        return redirect_with_data(
            request, data, '/tables/' + request.POST['workplace_uuid'] + '/1/')
    else:
        return custom_error_404(request, data)
コード例 #14
0
def delete(request, data):
    if request.method == 'POST':
        # Collect form data.
        salary_id = request.POST['salary_id']

        # Get Salary instance.
        salary_record = Salary.objects.get(id=salary_id)

        # Delete
        salary_record.delete()

        # Success
        data['alerts'].append(
            ('success', 'Delete successfully!',
             'You have successfully deleted a record of salary.'))
        return redirect_with_data(
            request, data, '/salary/' + request.POST['company_uuid'] + '/1/')
    else:
        return custom_error_404(request, data)
コード例 #15
0
def create(request, data):
    if request.method == 'POST':
        # Collect form data.
        company_name = request.POST['company_name']
        owned_by = Staff.objects.get(user=request.user)

        # Create new Company instance.
        new_company = Company(name=company_name, owner=owned_by)
        new_company.save()

        # Add new company to workplaces.
        owned_by.workplaces.add(new_company)

        # Add owner to staff of new company.
        new_company.staff.add(owned_by)

        # Success
        data['alerts'].append(('success', 'Create successfully!',
                               'You have successfully create a new company.'))
        return redirect_with_data(request, data, '/companies/1+1/#tab2')
    else:
        return render(request, 'companies/create.html', data)
コード例 #16
0
def leave(request, data):
    if request.method == 'POST':
        # Get form data.
        unique_id = request.POST['unique_id']

        # Get Company instance.
        company = Company.objects.get(unique_id=unique_id)

        # Get Staff instance.
        staff = Staff.objects.get(user=request.user)

        # Quit
        staff.workplaces = staff.workplaces.all().exclude(unique_id=unique_id)
        staff.save()
        company.staff = company.staff.all().exclude(user=request.user)
        company.save()

        # Success
        data['alerts'].append(('success', 'Quit successfully!',
                               'You have successfully quited a company.'))
        return redirect_with_data(request, data, '/companies/1+1/#tab1')
    else:
        return custom_error_404(request, data)
コード例 #17
0
def salary(request, data, **kwargs):
    payer = Staff.objects.get(user=request.user)
    owned_companies = Company.objects.filter(owner=payer)

    # Conditions on two kinds of url.
    if 'company_uuid' not in kwargs and 'page_num' not in kwargs:
        # No parameters

        # Test whether the staff has companies.
        if not owned_companies:
            data['no_owned_company'] = True
            return render(request, 'salary/index.html', data)

        first_company_uuid = owned_companies[0].unique_id
        return redirect_with_data(request, data,
                                  '/salary/' + str(first_company_uuid) + '/1/')
    else:
        # Two parameters
        data['no_owned_company'] = False
        data['owned_companies'] = owned_companies

        company_uuid = kwargs['company_uuid']
        page_num = kwargs['page_num']
        page_num = int(page_num)

        # If page number is zero...
        if page_num == 0:
            return custom_error_404(request, data)

        # If company_uuid is invalid...
        try:
            company = Company.objects.get(unique_id=company_uuid)
        except ValidationError:
            return custom_error_404(request, data)
        except ObjectDoesNotExist:
            return custom_error_404(request, data)

        # If the logged staff is not the owner of the company:
        if company.owner != payer:
            return custom_error_404(request, data)

        # Get UUID.
        data['company_uuid'] = company.unique_id

        # Get all salary belonging to staffs in the company.
        salary_records = Salary.objects.filter(company=company)

        # If there is no salary record in company...
        if not salary_records:
            data['no_salary_records'] = True
        else:
            data['no_salary_records'] = False

            # Get sliced records.
            data['salary_records'], data['page_end'] = get_slice_and_page_end(
                salary_records, page_num)

            # Get other necessary data.
            data['page_range'] = range(1, data['page_end'] + 1)
            data['page_num'] = page_num

            # If sliced records are empty...
            if not data['salary_records']:
                return custom_error_404(request, data)

        return render(request, 'salary/index.html', data)
コード例 #18
0
def update(request, data, receipt_id):
    # Get receipt instance.
    try:
        receipt = Receipt.objects.get(id=receipt_id)
    except ValidationError:
        return custom_error_404(request, data)
    except ObjectDoesNotExist:
        return custom_error_404(request, data)
    items = receipt.items.all()

    if request.method == 'POST':
        # Get form data.
        creator = Staff.objects.get(user=request.user)
        payer = request.POST['payer']
        payee = request.POST['payee']
        date = request.POST['date']
        address = request.POST['address']
        notes = request.POST['notes']

        # Update receipt.
        receipt.creator = creator
        receipt.payee = payee
        receipt.payer = payer
        receipt.date = date
        receipt.address = address
        receipt.notes = notes
        receipt.save()

        # Update items.
        for i, item in enumerate(items):
            if 'name_' + str(i) not in request.POST:
                break
            name = request.POST['name_' + str(i)]
            spec = request.POST['spec_' + str(i)]
            number = request.POST['number_' + str(i)]
            unit = request.POST['unit_' + str(i)]
            price = request.POST['price_' + str(i)]
            total_cost = request.POST['total_cost_' + str(i)]

            # Update item instance.
            item.name = name
            item.spec = spec
            item.number = number
            item.unit = unit
            item.price = price
            item.total_cost = total_cost
            item.save()

        # Update total amount of receipt.
        total_amount = 0
        for item in receipt.items.all():
            total_amount += item.total_cost
        receipt.total_amount = total_amount
        receipt.save()

        # Success
        data['alerts'].append(('success', 'Update successfully!', 'You have successfully updated a receipt.'))
        return redirect_with_data(request, data, '/receipts/details/' + receipt_id + '/')
    else:
        data['receipt'] = receipt

        # Generate names and ids for input tag.
        frontend_fields = {
            'name': 'update-receipt-item-name-',
            'spec': 'update-receipt-item-spec-',
            'number': 'update-receipt-item-number-',
            'unit': 'update-receipt-item-unit-',
            'price': 'update-receipt-item-price-',
            'total_cost': 'update-receipt-item-total-cost-',
        }

        backend_fields = {
            'name': 'name_',
            'spec': 'spec_',
            'number': 'number_',
            'unit': 'unit_',
            'price': 'price_',
            'total_cost': 'total_cost_',
        }

        item_containers = []
        for i, item in enumerate(items):
            container = {'item': item}
            front_temp = {}
            for key, value in frontend_fields.items():
                front_temp[key] = frontend_fields[key] + str(i)
            container['frontend_fields'] = front_temp

            back_temp = {}
            for key, value in backend_fields.items():
                back_temp[key] = backend_fields[key] + str(i)
            container['backend_fields'] = back_temp

            item_values = {'name': item.name,
                           'spec': item.spec,
                           'number': item.number,
                           'unit': item.unit,
                           'price': item.price,
                           'total_cost': item.total_cost}
            container['item_values'] = item_values

            item_containers.append(container)

        data['item_containers'] = item_containers

        return render(request, 'receipts/update.html', data)
コード例 #19
0
def create(request, data, **kwargs):
    if request.method == 'POST':
        # Collect form data.
        company_uuid = request.POST['company_uuid']
        payee_id = request.POST['payee_id']
        base_salary = request.POST['base_salary']
        bonus = request.POST['bonus']
        total = request.POST['total']
        date = request.POST['date']

        # Create new salary instance.
        payee = Staff.objects.get(id=payee_id)
        payer = Staff.objects.get(user=request.user)
        company = Company.objects.get(unique_id=company_uuid)
        new_salary = Salary(payer=payer,
                            payee=payee,
                            company=company,
                            base_salary=base_salary,
                            bonus=bonus,
                            total=total,
                            date=date)
        new_salary.save()

        # Success
        data['alerts'].append(
            ('success', 'Create successfully!',
             'You have successfully create a new salary record.'))
        return redirect_with_data(
            request, data, '/salary/' + request.POST['company_uuid'] + '/1/')
    else:
        payer = Staff.objects.get(user=request.user)

        if 'company_uuid' not in kwargs:
            return custom_error_404(request, data)
        company_uuid = kwargs['company_uuid']

        # If company_uuid is invalid...
        try:
            company = Company.objects.get(unique_id=company_uuid)
        except ValidationError:
            return custom_error_404(request, data)
        except ObjectDoesNotExist:
            return custom_error_404(request, data)

        # If the logged staff is not the owner of the company:
        if company.owner != payer:
            return custom_error_404(request, data)

        # Get UUID.
        data['company_uuid'] = company.unique_id

        # Get all staff WHO HAS NOT BEEN PAID in this company.
        salaries_in_company = Salary.objects.filter(company=company)
        raw_payees = company.staff.all()
        for temp_salary in salaries_in_company:
            if temp_salary.payee in raw_payees:
                raw_payees = raw_payees.exclude(user=temp_salary.payee.user)

        data['company_payees'] = raw_payees
        data['company_name'] = company.name

        return render(request, 'salary/create.html', data)