Ejemplo n.º 1
0
def forgot_password(request):

    if request.method == 'POST':
        try:
            with transaction.atomic():

                if 'email' in request.POST and request.POST['email'] != '':
                    email = request.POST['email']

                    if Users.objects.filter(user__username=email).exists():
                        myuser = Users.objects.filter(user__username=email)[0]
                    else:
                        return bad_json(
                            message='We did not find a User with that email')

                    return ok_json(data={
                        'message':
                        'Email sent to {}'.format(myuser.user.email)
                    })

                else:
                    return bad_json(message='Email is required')

        except Exception as ex:
            return HttpResponseRedirect("/login?ret=" + request.POST['ret'] +
                                        "&error=3")

    return render(request, "forgot.html")
Ejemplo n.º 2
0
def profile(request):
    data = {'title': 'User Profile'}
    addUserData(request, data)

    myuser = Users.objects.get(user=request.user)

    if request.method == 'POST':

        try:
            with transaction.atomic():

                first_name = None
                if 'first_name' in request.POST and request.POST['first_name'] != '':
                    first_name = request.POST['first_name']

                last_name = None
                if 'last_name' in request.POST and request.POST['last_name'] != '':
                    last_name = request.POST['last_name']

                email = None
                if 'email' in request.POST and request.POST['email'] != '':
                    email = request.POST['email']

                phone = None
                if 'phone' in request.POST and request.POST['phone'] != '':
                    phone = request.POST['phone']

                company_name = None
                if 'company_name' in request.POST and request.POST['company_name'] != '':
                    company_name = request.POST['company_name']

                user = myuser.user
                user.first_name = first_name
                user.last_name = last_name
                user.email = email
                user.save()

                myuser.phone = phone
                myuser.save()

                company = myuser.company
                company.name = company_name
                company.save()

                return ok_json(data={'message': 'Profile has been succesfully Updated!'})

        except Exception as ex:
            print(ex.__str__())
            pass

    data['is_user_profile'] = True
    return render(request, "user/profile.html", data)
Ejemplo n.º 3
0
def links(request):
    data = {'title': 'User Links'}
    addUserData(request, data)

    myuser = Users.objects.get(user=request.user)

    if request.method == 'POST':

        try:
            with transaction.atomic():

                website = None
                if 'website' in request.POST and request.POST['website'] != '':
                    website = request.POST['website']

                facebook = None
                if 'facebook' in request.POST and request.POST['facebook'] != '':
                    facebook = request.POST['facebook']

                linkedin = None
                if 'linkedin' in request.POST and request.POST['linkedin'] != '':
                    linkedin = request.POST['linkedin']

                twitter = None
                if 'twitter' in request.POST and request.POST['twitter'] != '':
                    twitter = request.POST['twitter']

                youtube = None
                if 'youtube' in request.POST and request.POST['youtube'] != '':
                    youtube = request.POST['youtube']

                myuser.website = website
                myuser.facebook = facebook
                myuser.linkedin = linkedin
                myuser.twitter = twitter
                myuser.youtube = youtube
                myuser.save()

                return ok_json(data={'message': 'Profile Links has been succesfully Updated!'})

        except Exception as ex:
            print(ex.__str__())
            pass

    data['is_user_links'] = True
    return render(request, "user/links.html", data)
Ejemplo n.º 4
0
def views(request):
    data = {'title': 'Projects'}
    addUserData(request, data)

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'add':
                try:
                    with transaction.atomic():

                        if 'project_name' in request.POST and request.POST['project_name'] != '':
                            name = request.POST['project_name']
                        else:
                            return bad_json(message='Please enter a project name')

                        if 'sel_customers' in request.POST and request.POST['sel_customers'] != '':
                            customer_id = int(request.POST['sel_customers'])
                        else:
                            return bad_json(message='Please select customer')

                        if Project.objects.filter(name=name, company=data['company']).exists():
                            return bad_json(message='A project already exist with that name')

                        project = Project(company=data['company'],
                                          name=name,
                                          customer_id=customer_id)
                        project.save()

                        return ok_json(data={'message': 'A project has been successfully created!'})

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'edit':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Project.objects.filter(pk=int(request.POST['eid'])).exists():
                            project = Project.objects.get(pk=int(request.POST['eid']))

                            if 'project_name' in request.POST and request.POST['project_name'] != '':
                                name = request.POST['project_name']
                            else:
                                return bad_json(message='Please enter a project name')

                            if 'sel_customers' in request.POST and request.POST['sel_customers'] != '':
                                customer_id = int(request.POST['sel_customers'])
                            else:
                                return bad_json(message='Please select customer')

                            if Project.objects.filter(name=name, company=data['company']).exclude(id=project.id).exists():
                                return bad_json(message='A project already exist with that name')

                            project.customer_id = customer_id
                            project.name = name
                            project.save()

                            return ok_json(data={'message': 'A project has been successfully edited!'})

                        return bad_json(message="Project does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            if action == 'delete':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Users.objects.filter(pk=int(request.POST['eid'])).exists():
                            project = Project.objects.get(pk=int(request.POST['eid']))
                            project.delete()
                            return ok_json(data={'message': 'A project has been successfully deleted!'})

                        return bad_json(message="Project does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            return bad_json(error=0)
    else:

        if 'action' in request.GET:
            action = request.GET['action']

            if action == 'get_project_data':
                try:
                    if 'eid' in request.GET and Project.objects.filter(pk=int(request.GET['eid'])).exists():
                        project = Project.objects.get(pk=int(request.GET['eid']))
                        return ok_json(data={'customer_id': project.customer_id,
                                             'project_name': project.name})

                    return bad_json(message="Project does not exist")
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    data['projects'] = Project.objects.filter(company=data['company'])
    data['customers'] = Users.objects.filter(type=USERS_GROUP_CUSTOMERS, usercompany__company=data['company'])
    return render(request, "projects.html", data)
Ejemplo n.º 5
0
def account(request):
    data = {'title': 'User Account'}
    addUserData(request, data)

    myuser = Users.objects.get(user=request.user)

    if request.method == 'POST':

        user = myuser.user

        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'close':
                try:
                    with transaction.atomic():

                        user.is_active = False
                        user.save()

                        logout(request)

                        return ok_json(data={'message': 'Your account has been closed!',
                                             'redirect_url': reverse('dashboard')})

                except Exception as ex:
                    print(ex.__str__())
                    pass

        else:

            try:
                with transaction.atomic():

                    if 'current_password' in request.POST and request.POST['current_password'] != '':
                        current_password = request.POST['current_password']
                    else:
                        return bad_json(message='Current password is required')

                    if 'new_password' in request.POST and request.POST['new_password'] != '':
                        new_password = request.POST['new_password']
                    else:
                        return bad_json(message='New password is required')

                    if 'confirm_new_password' in request.POST and request.POST['confirm_new_password'] != '':
                        confirm_new_password = request.POST['confirm_new_password']
                    else:
                        return bad_json(message='Confirm new password is required')

                    if not user.check_password('{}'.format(current_password)):
                        return bad_json(message='Current password is incorrect')

                    if current_password == new_password:
                        return bad_json(message='New password can not be the same as the current password')

                    if new_password != confirm_new_password:
                        return bad_json(message='Password does not match')

                    user.set_password('{}'.format(new_password))
                    user.save()
                    update_session_auth_hash(request, user)

                    return ok_json(data={'message': 'Password has been succesfully Updated!',
                                         'redirect_url': reverse('user_profile')})

            except Exception as ex:
                print(ex.__str__())
                pass

    data['is_user_account'] = True
    return render(request, "user/account.html", data)
Ejemplo n.º 6
0
def views(request):

    data = {'title': 'To Do Application - My Lists'}

    if request.method == 'POST':
        if 'action' in request.POST and request.POST['action'] != '':
            action = request.POST['action']

            if action == 'create':
                try:
                    with transaction.atomic():
                        f = ListForm(request.POST)
                        if f.is_valid():
                            name = f.cleaned_data['name']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']

                            if not name:
                                return bad_json(
                                    message='Please enter a list name.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this list.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this list.')

                            list = List(name=name,
                                        priority=priority,
                                        assigned_to=assigned_to,
                                        created_by=request.user,
                                        due_date=due_date)
                            list.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully created a list',
                                    'redirect_url': '/lists'
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'edit':
                try:
                    with transaction.atomic():
                        f = ListForm(request.POST)
                        if f.is_valid():
                            name = f.cleaned_data['name']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            list = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if List.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    list = List.objects.get(
                                        id=int(request.POST['id']))

                            if not list:
                                return bad_json(
                                    message='Please select a list to edit.')

                            if not name:
                                return bad_json(
                                    message='Please enter a list name.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this list.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this list.')

                            list.name = name
                            list.priority = priority
                            list.assigned_to = assigned_to
                            list.due_date = due_date
                            list.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully edited a list',
                                    'redirect_url': '/lists'
                                })
                        else:
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'add_task':
                try:
                    with transaction.atomic():
                        f = TaskForm(request.POST)
                        if f.is_valid():
                            title = f.cleaned_data['title']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            note = f.cleaned_data['note']
                            list = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if List.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    list = List.objects.get(
                                        id=int(request.POST['id']))

                            if not list:
                                return bad_json(
                                    message=
                                    'Please select a list to add this task to.'
                                )

                            if not title:
                                return bad_json(
                                    message='Please enter a task title.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this task to.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this task.')

                            if not note:
                                return bad_json(
                                    message='Please enter a note for this task.'
                                )

                            task = Task(list=list,
                                        title=title,
                                        priority=priority,
                                        assigned_to=assigned_to,
                                        created_by=request.user,
                                        due_date=due_date,
                                        note=note)
                            task.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully added a task',
                                    'redirect_url':
                                    '/lists?action=view&id={}'.format(list.id)
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'edit_task':
                try:
                    with transaction.atomic():
                        f = TaskForm(request.POST)
                        if f.is_valid():
                            title = f.cleaned_data['title']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            note = f.cleaned_data['note']
                            task = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if Task.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    task = Task.objects.get(
                                        id=int(request.POST['id']))

                            if not task:
                                return bad_json(
                                    message='Please select a task to edit.')

                            if not title:
                                return bad_json(
                                    message='Please enter a task title.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this task to.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this task.')

                            if not note:
                                return bad_json(
                                    message='Please enter a note for this task.'
                                )

                            task.title = title
                            task.priority = priority
                            task.assigned_to = assigned_to
                            task.due_date = due_date
                            task.note = note
                            task.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully edited a task',
                                    'redirect_url':
                                    '/lists?action=view&id={}'.format(
                                        task.list.id)
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'add_comment':
                try:
                    with transaction.atomic():
                        f = CommentForm(request.POST)
                        if f.is_valid():
                            body = f.cleaned_data['body']
                            task = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if Task.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    task = Task.objects.get(
                                        id=int(request.POST['id']))

                            if not task:
                                return bad_json(
                                    message=
                                    'Please select a task to add this comment to.'
                                )

                            if not body:
                                return bad_json(
                                    message=
                                    'Please enter a comment for this task.')

                            comment = Comment(
                                task=task,
                                author=User.objects.get(id=request.user.id),
                                body=body)
                            comment.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully added a comment',
                                    'redirect_url':
                                    '/lists?action=view_task&id={}'.format(
                                        task.id)
                                })
                        else:
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    else:
        if 'action' in request.GET:
            if 'action' in request.GET and request.GET['action'] != '':
                action = request.GET['action']

                if action == 'create':
                    data['title'] = 'Create a new List'
                    data['form'] = ListForm()
                    return render(request, 'lists/create.html', data)

                if action == 'edit':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Edit List: ' + list.name
                            data['form'] = ListForm(
                                initial={
                                    'name': list.name,
                                    'priority': list.priority,
                                    'assigned_to': list.assigned_to,
                                    'due_date': list.due_date
                                })

                            return render(request, 'lists/edit.html', data)

                if action == 'view':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))

                            data['title'] = 'Viewing List: ' + list.name
                            return render(request, 'lists/view.html', data)

                if action == 'add_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Add Task to: ' + list.name
                            data['form'] = TaskForm()
                    return render(request, 'tasks/create.html', data)

                if action == 'edit_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Edit Task for: ' + task.list.name
                            data['form'] = TaskForm(
                                initial={
                                    'title': task.title,
                                    'due_date': task.due_date,
                                    'assigned_to': task.assigned_to,
                                    'note': task.note,
                                    'priority': task.priority
                                })

                    return render(request, 'tasks/edit.html', data)

                if action == 'mark_complete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            task.completed = True
                            task.save()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(
                                    task.list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'mark_incomplete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            task.completed = False
                            task.save()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(
                                    task.list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'delete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            list = task.list
                            task.delete()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'delete':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            list = List.objects.get(id=int(request.GET['id']))
                            list.delete()
                    return HttpResponseRedirect('/lists')

                if action == 'view_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))

                            data['title'] = 'Viewing Task: ' + task.title
                            return render(request, 'tasks/view.html', data)

                if action == 'add_comment':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))
                            data['form'] = CommentForm()

                            data['title'] = 'Comment on Task: ' + task.title
                            return render(request, 'comment/create.html', data)

    data['lists'] = List.objects.all()
    return render(request, 'lists.html', data)
Ejemplo n.º 7
0
def views(request):
    data = {'title': 'Works and Tasks'}
    addUserData(request, data)

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'add':
                try:
                    with transaction.atomic():

                        if 'address' in request.POST and request.POST['address'] != '':
                            address = request.POST['address']
                        else:
                            return bad_json(message='Please enter a work address')

                        if 'sel_project' in request.POST and request.POST['sel_project'] != '':
                            project_id = int(request.POST['sel_project'])
                        else:
                            return bad_json(message='Please select a project')

                        if 'sel_supervisor' in request.POST and request.POST['sel_supervisor'] != '':
                            supervisor_id = int(request.POST['sel_supervisor'])
                        else:
                            return bad_json(message='Please select a supervisor')

                        notes = ''
                        if 'notes' in request.POST and request.POST['notes'] != '':
                            notes = request.POST['notes']

                        work = Work(project_id=project_id,
                                    supervisor_id=supervisor_id,
                                    address=address,
                                    notes=notes)
                        work.save()

                        return ok_json(data={'message': 'A work order has been successfully created!'})

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'edit':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Work.objects.filter(pk=int(request.POST['eid'])).exists():
                            work = Work.objects.get(pk=int(request.POST['eid']))

                            if 'address' in request.POST and request.POST['address'] != '':
                                address = request.POST['address']
                            else:
                                return bad_json(message='Please enter a work address')

                            if 'sel_project' in request.POST and request.POST['sel_project'] != '':
                                project_id = int(request.POST['sel_project'])
                            else:
                                return bad_json(message='Please select a project')

                            if 'sel_supervisor' in request.POST and request.POST['sel_supervisor'] != '':
                                supervisor_id = int(request.POST['sel_supervisor'])
                            else:
                                return bad_json(message='Please select a supervisor')

                            notes = ''
                            if 'notes' in request.POST and request.POST['notes'] != '':
                                notes = request.POST['notes']

                            work.project_id = project_id
                            work.supervisor_id = supervisor_id
                            work.address = address
                            work.notes = notes
                            work.save()

                            return ok_json(data={'message': 'A work has been successfully edited!'})

                        return bad_json(message="Work does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            if action == 'delete':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Work.objects.filter(pk=int(request.POST['eid'])).exists():
                            work = Work.objects.get(pk=int(request.POST['eid']))
                            work.delete()
                            return ok_json(data={'message': 'A work has been successfully deleted!'})

                        return bad_json(message="Work does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            return bad_json(error=0)
    else:

        if 'action' in request.GET:
            action = request.GET['action']

            if action == 'get_work_data':
                try:
                    if 'eid' in request.GET and Work.objects.filter(pk=int(request.GET['eid'])).exists():
                        work = Work.objects.get(pk=int(request.GET['eid']))
                        return ok_json(data={'project_id': work.project_id,
                                             'supervisor_id': work.supervisor_id,
                                             'address': work.address,
                                             'notes': work.notes})

                    return bad_json(message='Work does not exist')
                except Exception:
                    return bad_json(error=2)

    data['works'] = Work.objects.filter(project__company=data['company'])
    data['projects'] = Project.objects.filter(company=data['company'])
    data['work_types'] = WorkType.objects.filter(company=data['company'])
    data['supervisors'] = Users.objects.filter(type=USERS_GROUP_EMPLOYEES, is_supervisor=True, usercompany__company=data['company'])
    return render(request, "works.html", data)
Ejemplo n.º 8
0
def views(request):
    data = {'title': 'Challenges'}

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'create_challenge':
                try:
                    with transaction.atomic():

                        if 'challenge_name' in request.POST and request.POST[
                                'challenge_name'] != '':
                            challenge_name = request.POST['challenge_name']
                            if Challenges.objects.filter(
                                    name=challenge_name).exists():
                                return bad_json(
                                    message="Challenge Name already exists. "
                                    "Please change the name of the challenge and try again. "
                                )

                            new_code = generate_code()
                            challenge = Challenges(name=challenge_name,
                                                   code=new_code)
                            challenge.save()
                            return ok_json(
                                data={
                                    'message':
                                    'Good Job!. You successfully created a new Challenge.'
                                })

                        else:
                            return bad_json(message="Not Challenge received")

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'questions':
                challenge = Challenges.objects.get(pk=int(request.POST['id']))

                question = None
                if 'qid' in request.POST and request.POST['qid'] != '':
                    question = Questions.objects.get(
                        pk=int(request.POST['qid']))

                correct_answer = int(request.POST['q_answers_checks'])

                try:
                    with transaction.atomic():

                        if not question:

                            question = Questions(
                                challenge=challenge,
                                text=request.POST['q_text'],
                                order=request.POST['q_order'],
                                counter_time=request.POST['q_counter'],
                                answer1=request.POST['q_answer1'],
                                answer2=request.POST['q_answer2'],
                                answer3=request.POST['q_answer3'],
                                answer4=request.POST['q_answer4'],
                                is_correct_answer1=True
                                if correct_answer == 1 else False,
                                is_correct_answer2=True
                                if correct_answer == 2 else False,
                                is_correct_answer3=True
                                if correct_answer == 3 else False,
                                is_correct_answer4=True
                                if correct_answer == 4 else False)

                        else:

                            question.text = request.POST['q_text']
                            question.order = request.POST['q_order']
                            question.counter_time = request.POST['q_counter']

                            question.answer1 = request.POST['q_answer1']
                            question.answer2 = request.POST['q_answer2']
                            question.answer3 = request.POST['q_answer3']
                            question.answer4 = request.POST['q_answer4']
                            question.is_correct_answer1 = True if correct_answer == 1 else False
                            question.is_correct_answer2 = True if correct_answer == 2 else False
                            question.is_correct_answer3 = True if correct_answer == 3 else False
                            question.is_correct_answer4 = True if correct_answer == 4 else False

                        question.save()

                        return ok_json(
                            data={
                                'redirect_url':
                                '/challenges',
                                'message':
                                'Successfully {} a Question'.format(
                                    'Created' if not question else 'Edited')
                            })

                except Exception:
                    return bad_json(message="Error saving data")

            if action == 'delete_question':
                try:
                    question = Questions.objects.get(
                        pk=int(request.POST['qid']))
                    with transaction.atomic():
                        if question.responses_set.exists():
                            question.responses_set.all().delete()
                        question.delete()
                        return ok_json(
                            data={
                                'redirect_url': '/challenges',
                                'message': 'Successfully Deleted the Question.'
                            })
                except Exception:
                    return bad_json(message="Error deleting Question")

        return bad_json(message="Bad Request")

    else:

        if 'action' in request.GET:
            if 'action' in request.GET:
                action = request.GET['action']

            return HttpResponseRedirect('/access')

        return render(request, 'challenges/access.html', data)
Ejemplo n.º 9
0
def views(request):
    data = {'title': 'Employees'}
    addUserData(request, data)

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'add':
                try:
                    with transaction.atomic():

                        if 'first_name' in request.POST and request.POST[
                                'first_name'] != '':
                            first_name = request.POST['first_name'].capitalize(
                            )
                        else:
                            return bad_json(
                                message='Please enter a first name')

                        if 'last_name' in request.POST and request.POST[
                                'last_name'] != '':
                            last_name = request.POST['last_name'].capitalize()
                        else:
                            return bad_json(message='Please enter a last name')

                        if 'email' in request.POST and request.POST[
                                'email'] != '':
                            email = request.POST['email']
                        else:
                            return bad_json(
                                message='Please enter a valid email')

                        if 'phone' in request.POST and request.POST[
                                'phone'] != '':
                            phone = request.POST['phone']
                        else:
                            return bad_json(
                                message='Please enter a phone number')

                        gender = None
                        if 'genders' in request.POST and request.POST[
                                'genders'] != '':
                            gender = int(request.POST['genders'])

                        is_supervisor = None
                        if 'supervisors' in request.POST and request.POST[
                                'supervisors'] != '':
                            is_supervisor = True if int(
                                request.POST['supervisors']) == 1 else False

                        if Users.objects.filter(type=USERS_GROUP_EMPLOYEES,
                                                phone=phone).exists():
                            return bad_json(
                                message=
                                'An employee already exist with this phone number'
                            )

                        if Users.objects.filter(type=USERS_GROUP_EMPLOYEES,
                                                user__email=email).exists():
                            return bad_json(
                                message=
                                'An employee already exist with this email')

                        django_user = User(username=email,
                                           first_name=first_name,
                                           last_name=last_name,
                                           email=email)
                        django_user.save()

                        employee = Users(user=django_user,
                                         type=USERS_GROUP_EMPLOYEES,
                                         phone=phone,
                                         gender=gender,
                                         is_supervisor=is_supervisor)
                        employee.save()

                        employee_company = UserCompany(user=employee,
                                                       company=data['company'])
                        employee_company.save()

                        return ok_json(
                            data={
                                'message':
                                'An employee has been successfully created!'
                            })

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'edit':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Users.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            employee = Users.objects.get(
                                pk=int(request.POST['eid']))

                            if 'first_name' in request.POST and request.POST[
                                    'first_name'] != '':
                                first_name = request.POST[
                                    'first_name'].capitalize()
                            else:
                                return bad_json(
                                    message='Please enter a first name')

                            if 'last_name' in request.POST and request.POST[
                                    'last_name'] != '':
                                last_name = request.POST[
                                    'last_name'].capitalize()
                            else:
                                return bad_json(
                                    message='Please enter a last name')

                            if 'email' in request.POST and request.POST[
                                    'email'] != '':
                                email = request.POST['email']
                            else:
                                return bad_json(
                                    message='Please enter a valid email')

                            if 'phone' in request.POST and request.POST[
                                    'phone'] != '':
                                phone = request.POST['phone']
                            else:
                                return bad_json(
                                    message='Please enter a phone number')

                            gender = None
                            if 'genders' in request.POST and request.POST[
                                    'genders'] != '':
                                gender = int(request.POST['genders'])

                            is_supervisor = None
                            if 'supervisors' in request.POST and request.POST[
                                    'supervisors'] != '':
                                is_supervisor = True if int(
                                    request.POST['supervisors']
                                ) == 1 else False

                            if Users.objects.filter(
                                    type=USERS_GROUP_EMPLOYEES,
                                    phone=phone).exclude(
                                        id=employee.id).exists():
                                return bad_json(
                                    message=
                                    'An employee already exist with this phone number'
                                )

                            if Users.objects.filter(
                                    type=USERS_GROUP_EMPLOYEES,
                                    user__email=email).exclude(
                                        id=employee.id).exists():
                                return bad_json(
                                    message=
                                    'An employee already exist with this email'
                                )

                            django_user = employee.user
                            django_user.first_name = first_name
                            django_user.last_name = last_name
                            django_user.email = email
                            django_user.username = email
                            django_user.save()

                            employee.phone = phone
                            employee.gender = gender
                            employee.is_supervisor = is_supervisor
                            employee.save()

                            return ok_json(
                                data={
                                    'message':
                                    'An employee has been successfully edited!'
                                })

                        return bad_json(message="Employee does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            if action == 'delete':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Users.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            employee = Users.objects.get(
                                pk=int(request.POST['eid']))
                            django_user = employee.user
                            employee.delete()
                            django_user.delete()
                            return ok_json(
                                data={
                                    'message':
                                    'Am employee has been successfully deleted!'
                                })

                        return bad_json(message="Employee does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            return bad_json(error=0)
    else:

        if 'action' in request.GET:
            action = request.GET['action']

            if action == 'get_employee_data':
                try:
                    if 'eid' in request.GET and Users.objects.filter(
                            pk=int(request.GET['eid'])).exists():
                        employee = Users.objects.get(
                            pk=int(request.GET['eid']))
                        return ok_json(
                            data={
                                'first_name':
                                employee.user.first_name,
                                'last_name':
                                employee.user.last_name,
                                'email':
                                employee.user.email,
                                'phone':
                                employee.phone,
                                'gender':
                                employee.gender,
                                'supervisor':
                                "1" if employee.is_supervisor else "2"
                            })

                    return bad_json(message="Employee does not exist")
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    data['employees'] = Users.objects.filter(
        type=USERS_GROUP_EMPLOYEES, usercompany__company=data['company'])
    return render(request, "employees.html", data)
Ejemplo n.º 10
0
def views(request):
    data = {'title': 'Customers'}
    addUserData(request, data)

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'add':
                try:
                    with transaction.atomic():

                        if 'first_name' in request.POST and request.POST[
                                'first_name'] != '':
                            first_name = request.POST['first_name'].capitalize(
                            )
                        else:
                            return bad_json(
                                message='Please enter a first name')

                        if 'last_name' in request.POST and request.POST[
                                'last_name'] != '':
                            last_name = request.POST['last_name'].capitalize()
                        else:
                            return bad_json(message='Please enter a last name')

                        if 'email' in request.POST and request.POST[
                                'email'] != '':
                            email = request.POST['email']
                        else:
                            return bad_json(
                                message='Please enter a valid email')

                        if 'phone' in request.POST and request.POST[
                                'phone'] != '':
                            phone = request.POST['phone']
                        else:
                            return bad_json(
                                message='Please enter a phone number')

                        gender = None
                        if 'genders' in request.POST and request.POST[
                                'genders'] != '':
                            gender = int(request.POST['genders'])

                        if Users.objects.filter(type=USERS_GROUP_CUSTOMERS,
                                                phone=phone).exists():
                            return bad_json(
                                message=
                                'A customer already exist with that phone number'
                            )

                        if Users.objects.filter(type=USERS_GROUP_CUSTOMERS,
                                                user__email=email).exists():
                            return bad_json(
                                message=
                                'A customer already exist with that email')

                        django_user = User(username=email,
                                           first_name=first_name,
                                           last_name=last_name,
                                           email=email)
                        django_user.save()

                        customer = Users(user=django_user,
                                         type=USERS_GROUP_CUSTOMERS,
                                         phone=phone,
                                         gender=gender)
                        customer.save()

                        customer_company = UserCompany(user=customer,
                                                       company=data['company'])
                        customer_company.save()

                        return ok_json(data={
                            'message':
                            'A customer has been successfully created!'
                        })

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'edit':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Users.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            customer = Users.objects.get(
                                pk=int(request.POST['eid']))

                            if 'first_name' in request.POST and request.POST[
                                    'first_name'] != '':
                                first_name = request.POST[
                                    'first_name'].capitalize()
                            else:
                                return bad_json(
                                    message='Please enter a first name')

                            if 'last_name' in request.POST and request.POST[
                                    'last_name'] != '':
                                last_name = request.POST[
                                    'last_name'].capitalize()
                            else:
                                return bad_json(
                                    message='Please enter a last name')

                            if 'email' in request.POST and request.POST[
                                    'email'] != '':
                                email = request.POST['email']
                            else:
                                return bad_json(
                                    message='Please enter a valid email')

                            if 'phone' in request.POST and request.POST[
                                    'phone'] != '':
                                phone = request.POST['phone']
                            else:
                                return bad_json(
                                    message='Please enter a phone number')

                            gender = None
                            if 'genders' in request.POST and request.POST[
                                    'genders'] != '':
                                gender = int(request.POST['genders'])

                            if Users.objects.filter(
                                    type=USERS_GROUP_CUSTOMERS,
                                    phone=phone).exclude(
                                        id=customer.id).exists():
                                return bad_json(
                                    message=
                                    'A customer already exist with that phone number'
                                )

                            if Users.objects.filter(
                                    type=USERS_GROUP_CUSTOMERS,
                                    user__email=email).exclude(
                                        id=customer.id).exists():
                                return bad_json(
                                    message=
                                    'A customer already exist with that email')

                            django_user = customer.user
                            django_user.first_name = first_name
                            django_user.last_name = last_name
                            django_user.email = email
                            django_user.username = email
                            django_user.save()

                            customer.phone = phone
                            customer.gender = gender
                            customer.save()

                            return ok_json(
                                data={
                                    'message':
                                    'A customer has been successfully edited!'
                                })

                        return bad_json(message="Customer does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            if action == 'delete':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and Users.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            customer = Users.objects.get(
                                pk=int(request.POST['eid']))
                            django_user = customer.user
                            customer.delete()
                            django_user.delete()
                            return ok_json(
                                data={
                                    'message':
                                    'A customer has been successfully deleted!'
                                })

                        return bad_json(message="Customer does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            return bad_json(error=0)
    else:

        if 'action' in request.GET:
            action = request.GET['action']

            if action == 'get_customer_data':
                try:
                    if 'eid' in request.GET and Users.objects.filter(
                            pk=int(request.GET['eid'])).exists():
                        customer = Users.objects.get(
                            pk=int(request.GET['eid']))
                        return ok_json(
                            data={
                                'first_name': customer.user.first_name,
                                'last_name': customer.user.last_name,
                                'email': customer.user.email,
                                'phone': customer.phone,
                                'gender': customer.gender
                            })

                    return bad_json(message="Customer does not exist")
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    data['customers'] = Users.objects.filter(
        type=USERS_GROUP_CUSTOMERS, usercompany__company=data['company'])
    return render(request, "customers.html", data)
Ejemplo n.º 11
0
def views(request):
    data = {'title': 'Challenges'}

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'create_challenge':
                try:
                    with transaction.atomic():

                        if 'challenge_name' in request.POST and request.POST['challenge_name'] != '':
                            challenge_name = request.POST['challenge_name']
                            if Challenges.objects.filter(name=challenge_name).exists():
                                return bad_json(message="Challenge Name already exists. "
                                                        "Please change the name of the challenge and try again. ")

                            new_code = generate_code()
                            challenge = Challenges(name=challenge_name, code=new_code)
                            challenge.save()
                            return ok_json(data={'message': 'Good Job!. You successfully created a new Challenge.'})

                        else:
                            return bad_json(message="Not Challenge received")

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'questions':
                challenge = Challenges.objects.get(pk=int(request.POST['id']))

                question = None
                if 'qid' in request.POST and request.POST['qid'] != '':
                    question = Questions.objects.get(pk=int(request.POST['qid']))

                correct_answer = int(request.POST['q_answers_checks'])

                try:
                    with transaction.atomic():

                        if not question:

                            question = Questions(challenge=challenge,
                                                 text=request.POST['q_text'],
                                                 order=request.POST['q_order'],
                                                 answer1=request.POST['q_answer1'],
                                                 answer2=request.POST['q_answer2'],
                                                 answer3=request.POST['q_answer3'],
                                                 answer4=request.POST['q_answer4'],
                                                 is_correct_answer1=True if correct_answer == 1 else False,
                                                 is_correct_answer2=True if correct_answer == 2 else False,
                                                 is_correct_answer3=True if correct_answer == 3 else False,
                                                 is_correct_answer4=True if correct_answer == 4 else False)

                        else:

                            question.text = request.POST['q_text']
                            question.order = request.POST['q_order']
                            question.counter_time = request.POST['q_counter']

                            question.answer1 = request.POST['q_answer1']
                            question.answer2 = request.POST['q_answer2']
                            question.answer3 = request.POST['q_answer3']
                            question.answer4 = request.POST['q_answer4']
                            question.is_correct_answer1 = True if correct_answer == 1 else False
                            question.is_correct_answer2 = True if correct_answer == 2 else False
                            question.is_correct_answer3 = True if correct_answer == 3 else False
                            question.is_correct_answer4 = True if correct_answer == 4 else False

                        question.save()

                        return ok_json(data={'redirect_url': '/challenges',
                                             'message': 'Successfully {} a Question'.format('Created' if not question else 'Edited')})

                except Exception:
                    return bad_json(message="Error saving data")

            if action == 'delete_question':
                try:
                    question = Questions.objects.get(pk=int(request.POST['qid']))
                    with transaction.atomic():
                        if question.responses_set.exists():
                            question.responses_set.all().delete()
                        question.delete()
                        return ok_json(data={'redirect_url': '/challenges',
                                             'message': 'Successfully Deleted the Question.'})
                except Exception:
                    return bad_json(message="Error deleting Question")

            if action == 'access_challenge':
                try:
                    code = request.POST['code']
                    if not Challenges.objects.filter(code=code).exists():
                        return bad_json(message='Code does not exist in the system.')

                    return ok_json(data={'challengeID': Challenges.objects.filter(code=code)[0].id})

                except Exception:
                    return bad_json(message='Error getting data')

            if action == 'response_question':
                question = Questions.objects.get(pk=int(request.POST['qid']))
                response = int(request.POST['response'])

                if response == 1 and question.is_correct_answer1:
                    return ok_json()

                elif response == 2 and question.is_correct_answer2:
                    return ok_json()

                elif response == 3 and question.is_correct_answer3:
                    return ok_json()

                elif response == 4 and question.is_correct_answer4:
                    return ok_json()

                else:
                    return bad_json(message='INCORRECT')

        return bad_json(message="Bad Request")

    else:

        if 'action' in request.GET:
            if 'action' in request.GET:
                action = request.GET['action']

                if action == 'questions':
                    try:
                        data['title'] = 'New Question'
                        data['challenge'] = Challenges.objects.get(pk=request.GET['id'])
                        question = None
                        if 'qid' in request.GET and request.GET['qid'] != '':
                            question = Questions.objects.get(pk=int(request.GET['qid']))
                        data['question'] = question
                        return render(request, 'challenges/questions.html', data)
                    except Exception:
                        pass

                if action == 'play':
                    try:
                        data['title'] = 'Play Challenge'
                        data['challenge'] = challenge = Challenges.objects.get(pk=request.GET['id'])
                        data['questions'] = questions = challenge.get_my_questions()
                        data['first_question'] = questions[0]
                        data['current_question'] = questions[0]
                        return render(request, 'challenges/play.html', data)
                    except Exception:
                        pass

            return HttpResponseRedirect('/challenges')

        data['challenges'] = Challenges.objects.all()
        return render(request, 'challenges/view.html', data)
Ejemplo n.º 12
0
def views(request):
    data = {'title': 'Work Types'}
    addUserData(request, data)

    if request.method == 'POST':
        if 'action' in request.POST:
            action = request.POST['action']

            if action == 'add':
                try:
                    with transaction.atomic():

                        if 'worktype_name' in request.POST and request.POST[
                                'worktype_name'] != '':
                            name = request.POST['worktype_name']
                        else:
                            return bad_json(message='Please enter a work type')

                        work_type = WorkType(company=data['company'],
                                             name=name)
                        work_type.save()

                        return ok_json(
                            data={
                                'message':
                                'A work type has been successfully created!'
                            })

                except Exception as ex:
                    return bad_json(error=1)

            if action == 'edit':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and WorkType.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            work_type = WorkType.objects.get(
                                pk=int(request.POST['eid']))

                            if 'worktype_name' in request.POST and request.POST[
                                    'worktype_name'] != '':
                                name = request.POST['worktype_name']
                            else:
                                return bad_json(
                                    message='Please enter a work type')

                            if WorkType.objects.filter(
                                    name=name,
                                    company=data['company']).exclude(
                                        id=work_type.id).exists():
                                return bad_json(
                                    message=
                                    'A work type already exist with that name')

                            work_type.name = name
                            work_type.save()

                            return ok_json(
                                data={
                                    'message':
                                    'A project has been successfully edited!'
                                })

                        return bad_json(message="Project does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            if action == 'delete':
                try:
                    with transaction.atomic():

                        if 'eid' in request.POST and WorkType.objects.filter(
                                pk=int(request.POST['eid'])).exists():
                            work_type = WorkType.objects.get(
                                pk=int(request.POST['eid']))
                            work_type.delete()
                            return ok_json(
                                data={
                                    'message':
                                    'A work type has been successfully deleted!'
                                })

                        return bad_json(message="Project does not exist")
                except Exception as ex:
                    return bad_json(error=1)

            return bad_json(error=0)
    else:

        if 'action' in request.GET:
            action = request.GET['action']

            if action == 'get_work_type_data':
                try:
                    if 'eid' in request.GET and WorkType.objects.filter(
                            pk=int(request.GET['eid'])).exists():
                        work_type = WorkType.objects.get(
                            pk=int(request.GET['eid']))
                        return ok_json(data={'worktype_name': work_type.name})

                    return bad_json(message="Work Type does not exist")
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    data['work_types'] = WorkType.objects.filter(company=data['company'])
    return render(request, "work_types.html", data)