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")
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)
def plans(request): data = {'title': 'User Plans'} addUserData(request, data) company = data['company'] myuser = None if data['myuser']: myuser = Users.objects.get(user=request.user) if request.method == 'POST': if 'action' in request.POST: action = request.POST['action'] # if action == 'select_plan': # try: # with transaction.atomic(): # # if 'plan_id' in request.POST and request.POST['plan_id'] and Plan.objects.filter(pk=int(request.POST['plan_id'])).exists(): # plan = Plan.objects.get(pk=int(request.POST['plan_id'])) # # cardType = None # if 'cardTypes' in request.POST and request.POST['cardTypes'] != '': # cardType = int(request.POST['cardTypes']) # # cardOwner = '' # if 'cardOwner' in request.POST and request.POST['cardOwner']: # cardOwner = request.POST['cardOwner'].title() # # cardNumber = '' # if 'cardNumber' in request.POST and request.POST['cardNumber']: # cardNumber = request.POST['cardNumber'] # # cardMonth = '' # if 'cardMonth' in request.POST and request.POST['cardMonth']: # cardMonth = int(request.POST['cardMonth']) # # cardYear = '' # if 'cardYear' in request.POST and request.POST['cardYear']: # cardYear = int(request.POST['cardYear']) # # cardCVV = '' # if 'cardCVV' in request.POST and request.POST['cardCVV']: # cardCVV = int(request.POST['cardCVV']) # # # this is for real server # payment_source = { # 'object': 'card', # 'number': cardNumber, # 'exp_month': cardMonth, # 'exp_year': cardYear, # 'cvc': cardCVV, # 'name': cardOwner, # 'type': CREDIT_CARD_TYPES[cardType - 1][1] # } # # # Stripe recommends to use test tokens when testing your integration and creating charges, # # instead of passing card information directly to the API. # # Using tokens in place of card numbers helps ensure your production integration is # # developed in a PCI compliant manner and is not going to handle card information directly. # payment_source = "tok_visa" # # # Stripe Customer ID # # (if is not a stripe customer yet, we have to create a new stripe customer) # myuser.get_stripe_customer_id() # # # Stripe Create Suscription # create_subscription(myuser.stripe_customer_id, plan.stripe_plan_id, source=payment_source) # # # Save credit card (few info) # credit_card = get_credit_card(cardType, cardMonth, cardYear, cardNumber[-4:], company) # # # Save few data in payment history after API request to Stripe # payment_history = PaymentHistory(user=myuser, # plan=plan, # stripe_plan_id=plan.stripe_plan_id, # amount=plan.amount, # credit_card=credit_card) # payment_history.save() # # # update current plan in Users model # myuser.current_plan = plan # myuser.save() # # return ok_json(data={'message': 'Successful Payment. Thanks!'}) # # return bad_json(message='Plan does not exist.') # # except Exception as ex: # print(ex.__str__()) # pass return bad_json(error=0) change_plan = False if 'ch' in request.GET and request.GET['ch']: change_plan = True data['is_user_plans'] = True data['change_plan'] = change_plan data['months'] = MONTHS data['years'] = YEARS # data['plans_1'] = Plan.objects.all()[:3] # data['plans_2'] = Plan.objects.all()[3:] return render(request, "user/plans.html", data)
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)
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)
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)
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)
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)
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)
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)
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)