def get_all_current_buildings(request): response = {} try: project_id = request.POST.get('project_id') change_project = request.POST.get('change_project') buildings = Buildings.objects.annotate( total_flats=Count('flats')).filter(project_id=project_id) if change_project == 'true': CurrentProjects.change_active_project(request, project_id) building_list_tab = render_to_string( 'profiles/buildings.html', { "buildings": buildings, "request": request }) response['building_list_tab'] = building_list_tab current_buildings = [] for building in buildings: current_buildings.append({ 'id': building.id, 'number': building.display_number }) response['success'] = True response['current_buildings'] = current_buildings 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_file_path(file): file = str(file) file_path = "" try: # path = file.split("adminapp/")[1] file_path = settings.MEDIA_URL + file except Exception as e: LogHelper.elog(e) return file_path
def get_all_flats_by_active_building(request): try: building_id = request.session['active_building']['id'] flats = Flats.objects.annotate( total_tasks=Count('buildingcomponents__tasks')).filter( building_id=building_id) return render(request, 'profiles/flats.html', {"flats": flats}) except Exception as e: LogHelper.elog(e) return redirect('index')
def get_all_buildings_by_active_project(request): try: project_id = request.session['active_project']['id'] buildings = Buildings.objects.annotate( total_flats=Count('flats')).filter(project_id=project_id) return render(request, 'profiles/buildings.html', {"buildings": buildings}) except Exception as e: LogHelper.elog(e) return redirect('index')
def delete(request): response = {} try: building_id = request.POST.get('id') Buildings.objects.get(id=building_id).delete() response['success'] = True response['message'] = "Hause 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 change_project_status(request): response = {} try: project_id = request.POST.get('id') status = request.POST.get('status') Projects.objects.filter(id=project_id).update(is_complete=status) response['success'] = True response['message'] = "Status Changed 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 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_all_current_flats(request): response = {} try: building_id = request.session["active_building"]['id'] flats = Flats.objects.filter(building_id=building_id) current_flats = [] for flat in flats: current_flats.append({'id': flat.id, 'number': flat.number}) response['success'] = True response['current_flats'] = current_flats 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 post(self, request): try: old_password = request.POST['old_password'] new_password = request.POST['new_password'] user = Users.objects.get(id=request.user.id) if user.check_password(old_password): user.set_password(new_password) user.save() update_session_auth_hash(request, user) return redirect('index') else: return render(request, 'profiles/change_password.html', {'msg': 'Password is not Correct. Try Again'}) except Exception as e: LogHelper.elog(e) return render(request, 'profiles/change_password.html', {'msg': 'Something went wrong. Please Try Again'})
def get_more_notifications(request): response_data = {} try: response = {} page_num = int(request.POST.get('page_number')) notifications = NotificationStatus.objects.filter( user_id=request.user.id).order_by('-sending_at') total = len(notifications) limit = 20 more_btn_visible = True if total > limit: offset = (page_num - 1) * limit highest = (offset + limit) no_of_pages = math.ceil(total / limit) pages = range(1, no_of_pages + 1) if page_num in pages: next_page_number = page_num + 1 last_page_no = pages[-1] if page_num == last_page_no: more_btn_visible = False notifications_list = notifications[offset:highest] response['notifications_list'] = notifications_list response['today'] = datetime.today().strftime('%Y-%m-%d') response['request'] = request all_list = render_to_string( 'profiles/notification_list.html', response) response_data['new_lists'] = all_list response_data['success'] = True response_data['total_notifications'] = len( notifications_list) response_data['next_page_number'] = next_page_number response_data['more_btn_visible'] = more_btn_visible else: response_data['success'] = False else: response_data['success'] = False except Exception as e: LogHelper.elog(e) response_data['success'] = False return HttpResponse(json.dumps(response_data), content_type="application/json")
def post(self, request, *args, **kwargs): try: hash_code = request.POST.get('key') user_id = request.POST.get('uid') password = request.POST.get('password') confirm_password = request.POST.get('confirm_password') reset_password = ResetPassword.objects.get(hash_code=hash_code, user__id=user_id) user = reset_password.user if datetime.now() > reset_password.expired_at: raise PasswordResetException("expired") elif reset_password.already_used: raise PasswordResetException("used") if password == confirm_password: user.set_password(password) user.save() reset_password.already_used = True reset_password.save() messages.success( request, 'Your password is changed successfully. You can now login') return redirect('login') else: raise Exception except ObjectDoesNotExist: raise Http404() except PasswordResetException as e: LogHelper.elog(e) if e.message == 'expired': context = {'message': 'The link is already expired.'} elif e.message == 'used': context = {'message': 'The link is already used once by you.'} else: context = {'message': 'Something went wrong.'} return render(request, 'reset_password/reset_password.html', context) except Exception: LogHelper.elog('Something went wrong') messages.success(request, 'Something went wrong.') return redirect('login')
def sendEmail(request, template, context, subject, to, user_id=None): response = {} try: context["request"] = request # context["base_url"] = settings.SITE_URL context["base_url"] = "http://" + request.get_host() context["project_title"] = settings.PROJECT_TITLE mail_template_content = render_to_string(template, context) sender_mail = settings.EMAIL_HOST_USER # if user_id: # CommonHelper.set_history(user_id=user_id, subject=subject, type="email") import threading task = threading.Thread(target=MailHelper.mail_send, args=(mail_template_content, subject, to, sender_mail)) task.start() response['success'] = True except Exception as e: LogHelper.elog(e) response['success'] = False return HttpResponse(json.dumps(response), content_type='application/json')
def get(self, request, *args, **kwargs): hash_code = request.GET.get('key') try: reset_password = ResetPassword.objects.get(hash_code=hash_code) if datetime.now() > reset_password.expired_at: raise PasswordResetException("expired") elif reset_password.already_used: raise PasswordResetException("used") else: user = reset_password.user context = {'hash_code': hash_code, 'user_id': user.id} return render(request, 'reset_password/reset_password.html', context) except ObjectDoesNotExist: raise Http404("Key not exist") except PasswordResetException as e: LogHelper.elog(e) context = {} if e.message == 'expired': context = {'message': 'The link is already expired.'} elif e.message == 'used': context = {'message': 'The link is already used once by you.'} return render(request, 'reset_password/reset_password.html', context)