Exemplo n.º 1
0
def register(request):
    res = create_exception(
        request, __name__, exception="unknown request method")

    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            user_obj = form.save()
            user_obj.first_name = user_obj.first_name.capitalize()
            user_obj.last_name = user_obj.last_name.capitalize()

            username = user_obj.first_name.lower(
            )[0] + user_obj.last_name.lower() + str(randint(1, 10000))
            user_obj.username = username
            user_details_obj = Personal_Info(username=username, date_of_birth=form.cleaned_data['date_of_birth'],
                                             pin_code=form.cleaned_data['pin_code'], address=form.cleaned_data['address'],
                                             city=form.cleaned_data['city'], email_verified=False)
            try:
                user_obj.save()
                user_details_obj.save()
                print("{} : {} {} added".format(
                    username, user_obj.first_name, user_obj.last_name))
            except Exception as e:
                print(e)
                res = create_exception(request, __name__, exception=e)
            else:
                res = redirect('dashboard')
        else:
            res = create_exception(request, __name__, str(
                ValueError("form data in request not valid")))
    elif request.method == "GET":
        empty_form = UserForm()
        res = render(request, "users/register.html", {"form": empty_form, "helper": empty_form.get_helper()})

    return res
Exemplo n.º 2
0
def modify_user_details(request):
    res = create_exception(request,
                           __name__,
                           exception="unknown request method")

    if request.method == "POST":
        data_for_update = request.POST.dict()
        old_username = data_for_update.get('old_username', None)
        # retrieve User and Personal_Info from old_username and update them
        try:
            user_obj = User.objects.get(username=old_username)
            user_details_obj = Personal_Info.objects.get(username=old_username)
        except Exception as e:
            print(e)
            res = create_exception(request, __name__, exception=e)
        else:
            user_obj.username = data_for_update['username']
            user_details_obj.username = data_for_update['username']
            user_details_obj.date_of_joining = data_for_update[
                'date_of_joining']
            user_details_obj.position = data_for_update['position']
            user_details_obj.direct_manager = data_for_update['direct_manager']

            try:
                user_obj.save()
                user_details_obj.save()
            except Exception as e:
                res = create_exception(request, __name__, exception=e)
            else:
                # direct return of show_all_users
                request.method = "GET"
                res = show_all_users(request)

    elif request.method == "GET":
        data = request.GET.dict()
        username = data.get('username', None)
        try:
            user_details_obj = Personal_Info.objects.get(username=username)
        except Exception as e:
            print(e)
            res = create_exception(request,
                                   __name__,
                                   exception=e,
                                   additional_data=username)
        else:
            user_details_context = {
                'username': user_details_obj.username,
                'date_of_joining': user_details_obj.date_of_joining,
                'position': user_details_obj.position,
                'direct_manager': user_details_obj.direct_manager,
                'old_username': user_details_obj.username
            }
            form = UserDataUpdateForm(user_data=user_details_context)
            res = render(request, "obj_details_modif.html", {
                'form': form,
                'form_type': 'user'
            })

    return res
Exemplo n.º 3
0
def assign_unique_code(request):
    res = create_exception(request,
                           __name__,
                           exception="unknown request method")

    if request.method == "POST":
        data_for_update = request.POST.dict()
        old_unique_code = data_for_update.get('old_unique_code', None)
        try:
            dealer_obj = Dealer_Info.objects.get(unique_code=old_unique_code)
        except Exception as e:
            print(e)
            res = create_exception(request, __name__, exception=e)
        else:
            dealer_obj.unique_code = data_for_update['unique_code']
            try:
                dealer_obj.save()
            except Exception as e:
                res = create_exception(request, __name__, exception=e)
            else:
                # direct return of show_all_users
                request.method = "GET"
                res = show_all_dealers(request)

    elif request.method == "GET":
        data = request.GET.dict()
        unique_code = data.get('unique_code', None)
        try:
            dealer_obj = Dealer_Info.objects.get(unique_code=unique_code)
        except Exception as e:
            print(e)
            res = create_exception(request,
                                   __name__,
                                   exception=e,
                                   additional_data=unique_code)
        else:
            form = DealerUniqueCodeUpdateForm(old_unique_code=unique_code)
            res = render(request, "obj_details_modif.html", {
                'form': form,
                'form_type': 'dealer'
            })

    return res
Exemplo n.º 4
0
def get_my_dealers(request):
    res = create_exception(
        request, __name__, exception="unknown request method")

    class Dealer_Info_Table(tables.Table):
        class Meta:
            model = Dealer_Info

    if request.method == "GET":
        username = str(request.user)
        dealer_objs = Dealer_Info.objects.filter(managed_by=username)
        table = Dealer_Info_Table(dealer_objs)
        res = render(request, 'users/dealers_under_user.html',
                     {'table': table})
    elif request.method == "POST":
        res = create_exception(
            request, __name__, exception="POST method not implemented")

    return res
Exemplo n.º 5
0
def add_dealer(request):
    res = create_exception(
        request, __name__, exception="unknown request method")
    if request.method == "POST":
        data = request.POST.dict()
        dealer_obj = Dealer_Info(first_name=data['first_name'], last_name=data['last_name'], pin_code=data['pin_code'], address=data['address'], city=data['city'],
                                 managed_by=data['managed_by'], date_of_registration=date.today(), pan_number=data['pan_number'], aadhar_number=data['aadhar_number'], unique_code='NOT_ASSIGNED')
        try:
            dealer_obj.save()
        except Exception as e:
            print(e)
            res = create_exception(request, __name__, exception=e)
        else:
            res = redirect('dashboard')
    elif request.method == "GET":
        empty_form = DealerInfoForm(username=request.user)
        res = render(request, "users/add_dealer.html",
                     {"form": empty_form, "helper": empty_form.get_helper()})

    return res
Exemplo n.º 6
0
def upload_dealer_docs(request):
    res = create_exception(
        request, __name__, exception="unknown request method")
    if request.method == "POST":
        fs = FileSystemStorage()
        unique_code = request.POST.dict()['unique_code']
        aadhar_card = request.FILES['aadhar_card']
        pan_card = request.FILES['pan_card']

        # save both the files correctly
        file_name_aadhar_card = './dealers/' + unique_code + '/' + aadhar_card.name
        file_name_pan_card = './dealers/' + unique_code + '/' + pan_card.name

        if fs.exists(file_name_aadhar_card):
            fs.delete(file_name_aadhar_card)
        fs.save(file_name_aadhar_card, aadhar_card)

        if fs.exists(file_name_pan_card):
            fs.delete(file_name_pan_card)
        fs.save(file_name_pan_card, pan_card)

        res = render(request, 'users/dashboard.html')

    elif request.method == "GET":
        unique_code = request.GET.dict()['unique_code']
        dealer_obj = None
        try:
            dealer_obj = Dealer_Info.objects.get(unique_code=unique_code)
        except Exception as e:
            print(e)
            res = create_exception(request, __name__, exception=e)
        if dealer_obj is not None:
            res = render(request, 'users/add_dealer_docs.html',
                         {'dealer': dealer_obj})
        else:
            res = create_exception(
                request, __name__, exception="dealer with unique_code({}) not found".format(unique_code))

    return res
Exemplo n.º 7
0
def show_tables(request, table_name=None):
    class User_Info_Table(tables.Table):
        class Meta:
            model = Personal_Info

    class Dealer_Info_Table(tables.Table):
        class Meta:
            model = Dealer_Info

    if table_name is None:
        return create_exception(request,
                                __name__,
                                exception="data no provided")

    res = create_exception(request,
                           __name__,
                           exception="unknown request method")

    if request.method == "GET":
        if table_name == 'dealers':
            all_objects = Dealer_Info.objects.all()
            table = Dealer_Info_Table(all_objects)
        elif table_name == 'users':
            all_objects = Personal_Info.objects.all()
            table = User_Info_Table(all_objects)
        res = render(request, 'show_table.html', {
            'table_type': table_name,
            'table': table
        })

    elif request.method == "POST":
        res = create_exception(request,
                               __name__,
                               exception="POST method not implemented")

    return res
Exemplo n.º 8
0
def upload_user_headshot(request):
    res = create_exception(
        request, __name__, exception="unknown request method")
    fs = FileSystemStorage()
    if request.method == "POST": 
        if request.FILES['profile_picture']:
            file = request.FILES['profile_picture']
            file_name = './users/' + \
                str(request.user) + '.' + file.name.split('.')[-1]

            if fs.exists(file_name):
                fs.delete(file_name)

            try:
                filename = fs.save(file_name, file)
            except Exception as e:
                print(e)
                res = create_exception(request, __name__, exception=e)
            else:
                res = render(request, 'users/user_headshot_upload.html',
                            {'uploaded_file_url': fs.url(filename)})
        else:
            res = create_exception(request, __name__, exception="no file uploaded")

    elif request.method == "GET":
        if str(request.user) != "AnonymousUser":
            file_name = './users/' + str(request.user) + '.png'
            if fs.exists(file_name):
                res = render(request, 'users/user_headshot_upload.html',
                             {'uploaded_file_url': fs.url(file_name)})
        # user is AnonymousUser or doesn't have profilePic
        file_name = './users/profilePic.png'
        res = render(request, 'users/user_headshot_upload.html',
                     {'uploaded_file_url': fs.url(file_name)})

    return res
Exemplo n.º 9
0
def modify_dealer_details(request):
    res = create_exception(request,
                           __name__,
                           exception="unknown request method")

    if request.method == "POST":
        data_for_update = request.POST.dict()
        unique_code = data_for_update.get('unique_code', None)
        try:
            dealer_info_obj = Dealer_Info.objects.get(unique_code=unique_code)
        except Exception as e:
            print(e)
            res = create_exception(request,
                                   __name__,
                                   exception=e,
                                   additional_data=unique_code)
        else:
            dealer_info_obj.first_name = data_for_update['first_name']
            dealer_info_obj.last_name = data_for_update['last_name']
            dealer_info_obj.pin_code = data_for_update['pin_code']
            dealer_info_obj.address = data_for_update['address']
            dealer_info_obj.city = data_for_update['city']
            dealer_info_obj.pan_number = data_for_update['pan_number']
            dealer_info_obj.aadhar_number = data_for_update['aadhar_number']
            try:
                dealer_info_obj.save()
            except Exception as e:
                res = create_exception(request, __name__, exception=e)
            else:
                # direct return of show_all_users
                request.method = "GET"
                res = show_all_dealers(request)

    elif request.method == "GET":
        data = request.GET.dict()
        unique_code = data.get('unique_code', None)
        try:
            dealer_info_obj = Dealer_Info.objects.get(unique_code=unique_code)
        except Exception as e:
            print(e)
            res = create_exception(request,
                                   __name__,
                                   exception=e,
                                   additional_data=unique_code)
        else:
            dealer_details_context = {
                'first_name': dealer_info_obj.first_name,
                'last_name': dealer_info_obj.last_name,
                'pin_code': dealer_info_obj.pin_code,
                'address': dealer_info_obj.address,
                'city': dealer_info_obj.city,
                'pan_number': dealer_info_obj.pan_number,
                'aadhar_number': dealer_info_obj.aadhar_number,
                'unique_code': dealer_info_obj.unique_code
            }
            form = DealerDataUpdateForm(dealer_details_context)
            res = render(request, "obj_details_modif.html", {
                'form': form,
                'form_type': 'dealer'
            })

    return res