def form_valid(self, form): form.save(request=self.request) mailTemplate = "mails/user_password_change.html" context = { "user_full_name": self.object.get_full_name(), "password": self.request.POST.get('password') } subject = "Password Change" to = self.object.email CommonView.sendEmail(self.request, mailTemplate, context, subject, to, self.object.id) return HttpResponseRedirect(self.get_success_url())
def forget_password(request): try: response = {} email = request.data.pop("email", '') users = Users.objects.filter(email=email, is_active=1) if users.exists(): user = users[0] current_time = datetime.now() expired_date = current_time + timedelta(hours=1) reset_code = user.resetpassword_set.filter( already_used=0, expired_at__gt=current_time) if reset_code.exists(): hash_code = reset_code[0].hash_code ResetPassword.objects.filter(id=reset_code[0].id).update( expired_at=expired_date) else: # generate hash code and store key = ''.join( random.choice(string.ascii_letters + string.digits + string.ascii_letters) for _ in range(10)) + str(datetime.now()) key = key.encode('utf-8') hash_code = hashlib.sha224(key).hexdigest() ResetPassword(user=user, hash_code=hash_code, expired_at=expired_date).save() # base_url = settings.SITE_URL base_url = "http://" + request.get_host() mail_template = "mails/reset_password.html" context = {'base_url': base_url, 'key': hash_code} subject = "Supplementer ::Password Reset" to = user.email CommonView.sendEmail(request, mail_template, context, subject, to, user.id) response['success'] = True response[ 'message'] = "A reset password email is sent to you with confirmation link" return Response(response, status=status.HTTP_200_OK) else: return Response( { 'success': False, 'message': "Email doesn't found" }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: LogHelper.efail(e) return Response( { 'success': False, 'message': "Something went wrong." }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, *args, **kwargs): form = self.form_class components = CommonView.get_all_main_component(request) return render(request, self.template_name, { 'form': form, 'components': components })
def post(self, request, *args, **kwargs): form = self.form_class(request.POST) project_id = kwargs['project_id'] try: if form.is_valid(): request.project_id = project_id obj = form.save(request=request) default_components = CommonView.create_default_building_components(request, obj) qr = CommonView.generate_qr_code(request, obj) if default_components: building_components = BuildingComponents.objects.filter(building_id=obj.id, flat__isnull=True) default_tasks = CommonView.create_default_tasks(request, building_components) return HttpResponseRedirect('/projects/'+str(obj.project_id)+'/buildings/') except Exception as e: LogHelper.efail(e) return render(request, self.template_name, {'form': form, 'project_id': project_id})
def get(self, request, *args, **kwargs): try: building_id = kwargs['building_id'] context = CommonView.common_datatable_context(self) context['building_id'] = building_id building = Buildings.objects.get(id=building_id) context['building'] = building return render(request, 'flats/flat.html', context) except Exception as e: LogHelper.efail(e) return redirect('index')
def get(self, request, *args, **kwargs): try: project_id = kwargs['project_id'] context = CommonView.common_datatable_context(self) context['project_id'] = project_id project = Projects.objects.get(id=project_id) context['project'] = project return render(request, 'buildings/building.html', context) except Exception as e: LogHelper.efail(e) return redirect('index')
def post(self, request, *args, **kwargs): if CommonView.superuser_login(request): form = self.form_class(request.POST) try: if form.is_valid(): form.save(request=request) return HttpResponseRedirect('/projects/') except Exception as e: LogHelper.efail(e) return render(request, self.template_name, {'form': form}) else: return redirect('index')
def post(self, request, *args, **kwargs): form = self.form_class(request.POST, request.FILES) working_types = [] working_components = request.POST.getlist('working_type') for component in working_components: component_id = component.split("-")[0] component_name = component.split("-")[1] working_types.append({ 'id': component_id, 'component': component_name }) if form.is_valid(): obj = form.save(self, request) data = { "company_name": request.POST.get('company_name'), "telephone_office": request.POST.get('telephone_office'), "telephone_mobile": request.POST.get('telephone_mobile'), "working_type": working_types, "user_id": obj.id, } CompaniesView.create_worker(request, data) mailTemplate = "mails/user_registered.html" context = { "user_full_name": obj.get_full_name(), "password": self.request.POST.get('password'), "username": self.request.POST.get('username') } subject = "Worker Register" to = obj.email CommonView.sendEmail(self.request, mailTemplate, context, subject, to, obj.id) return HttpResponseRedirect('/companies/') components = CommonView.get_all_main_component(request) return render( request, self.template_name, { 'form': form, 'components': components, 'working_types': working_components })
def delete(request): response = {} if CommonView.superuser_login(request): try: project_id = request.POST.get('id') Projects.objects.get(id=project_id).delete() response['success'] = True response['message'] = "Project deleted successfully" except Exception as e: LogHelper.elog(e) response['success'] = False response['message'] = "Something went wrong. Please try again" return HttpResponse(json.dumps(response), content_type='application/json')
def get_context_data(self, **kwargs): context = super(CompanyUpdateView, self).get_context_data(**kwargs) context['components'] = CommonView.get_all_main_component(self.request) context[ 'avatar'] = self.object.avatar.url if self.object.avatar else '' context['company_name'] = self.object.handworker.company_name context['telephone_office'] = self.object.handworker.telephone_office context['telephone_mobile'] = self.object.handworker.telephone_mobile working_components = self.object.handworker.working_type working_types = [] if working_components: for component in working_components: value = component['id'] + "-" + component['component'] working_types.append(value) context['working_type'] = working_types return context
def post(self, request, *args, **kwargs): if CommonView.superuser_login(request): response = {} form = self.form_class(request.POST) form.created_by = request.user parents = Components.objects.filter( Q(parent__isnull=True) | Q(parent_id=0)) response['parents'] = parents response['form'] = form try: if form.is_valid(): form.save(request=request) return HttpResponseRedirect('/components/') except Exception as e: LogHelper.efail(e) return render(request, self.template_name, response) else: return redirect('index')
def add_new_comment(request, *args, **kwargs): task_id = kwargs['task_id'] try: comment = request.POST.get('task_comment_text') files = request.FILES.getlist('task_comment_files') file_list = [] dir = os.path.join(settings.MEDIA_ROOT, "comments") if not os.path.exists(dir): os.makedirs(dir) for file in files: uploaded_file = CommonView.handle_uploaded_file(request, file) if uploaded_file != "": file_list.append(uploaded_file) if comment != '' or len(file_list) > 0: comment_form = { "text": comment, "file_type": file_list if(len(file_list) > 0) else None, "task_id": task_id, "user": request.user, "type": "text" } new_comment = Comments(**comment_form) new_comment.save() task = Tasks.objects.get(id=task_id) task.save() if comment != '': # Send Notification message = NotificationText.get_task_comment_notification_text(request.user.get_full_name(), task.building_component.component.name) NotificationsView.create_notfication(request, 'task_comment', message, task_id, request.user.id) if len(file_list) > 0: # Send Notification message = NotificationText.get_attach_file_notification_text(request.user.get_full_name(), task.building_component.component.name) NotificationsView.create_notfication(request, 'attach_file', message, task_id, request.user.id) except Exception as e: LogHelper.efail(e) return HttpResponseRedirect('/tasks/'+str(task_id)+'/')
def post(self, request, **kwargs): try: task_id = kwargs['task_id'] comment = '' file_list = [] dir = os.path.join(settings.MEDIA_ROOT, "comments") if not os.path.exists(dir): os.makedirs(dir) if 'files' in request.data: files = request.data.getlist('files') for file in files: uploaded_file = CommonView.handle_uploaded_file( request, file) if uploaded_file != "": file_list.append(uploaded_file) if 'text' in request.data: comment = request.data["text"] if comment != '' or len(file_list) > 0: comment_form = { "text": comment, "file_type": file_list if (len(file_list) > 0) else None, "task_id": task_id, "user": request.user, "type": "text" } new_comment = Comments(**comment_form) new_comment.save() task = Tasks.objects.get(id=task_id) task.save() if request.user.is_staff: user_name = request.user.get_full_name() else: user_name = request.user.handworker.company_name if comment != '': # Send Notification message = NotificationText.get_task_comment_notification_text( user_name, task.building_component.component.name) task_thread = threading.Thread( target=NotificationsView.create_notfication, args=(request, 'task_comment', message, task_id, request.user.id)) task_thread.start() if len(file_list) > 0: # Send Notification message = NotificationText.get_attach_file_notification_text( user_name, task.building_component.component.name) task_thread = threading.Thread( target=NotificationsView.create_notfication, args=(request, 'attach_file', message, task_id, request.user.id)) task_thread.start() paginator = PageNumberPagination() paginator.page_size = 10 comments = Comments.objects.filter( task_id=task_id).order_by('-created_at') result_page = paginator.paginate_queryset(comments, request) serializer = CommentSerializer(result_page, many=True) return paginator.get_paginated_response(data=serializer.data) else: return Response( { 'success': False, 'message': "Something went wrong." }, status=status.HTTP_404_NOT_FOUND) except Exception as e: LogHelper.efail(e) return Response( { 'success': False, 'message': "Something went wrong." }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request): context = CommonView.common_datatable_context(self) return render(request, 'companies/worker.html', context)
def form_valid(self, form): if CommonView.superuser_login(self.request): form.update(request=self.request) return HttpResponseRedirect(self.get_success_url()) else: return redirect('index')
def get(self, request, *args, **kwargs): if CommonView.superuser_login(request): form = self.form_class return render(request, self.template_name, {'form': form}) else: return redirect('index')
def get(self, request): context = CommonView.common_datatable_context(self) context['sorted_column'] = 0 context['sorting_order'] = 'desc' return render(request, 'projects/project.html', context)