def __call__(self, request): # Code to be executed for each request before # the view (and later middleware) are called. response = self.get_response(request) ip = get_client_ip(request) user = request.user if type( request.user) is not AnonymousUser else 'Anonymous User' log_msg = f'API request {request}, from {ip} by {user}' try: if request.data is not None and not request.path.__contains__( login_method_name): log_msg = log_msg + f'data : {request.data}' except Exception as e: access_logs.error(e) access_logs.info(log_msg) # Code to be executed for each request/response after # the view is called. if response and response[ 'content-type'] == 'application/json' and \ not request.path.__contains__(login_method_name): response_body = response.content access_logs.info( f'API response {response_body} to {ip} for {request} by {user}' ) return response
def get(self, request): """User Login function""" try: ip = get_client_ip(request) return self.service.get_question() except Exception as e: logger.error(f'Request -- Error : Login in to system {e}') return response.exception_500(e)
def get(self, request): """Function to logout the current user""" try: ip = get_client_ip(request) return self.user_service.logout(request, ip=ip) except Exception as e: logger.error( f'Request by user : {request.user} -- Error : Logging out of the system {e}' ) return response.exception_500(e)
def post(self, request): entry = Entry.objects.get(pk=int(request.body)) if request.user.is_authenticated: user = request.user username = user.username if user.reported.filter(pk=entry.pk).exists(): raise Http404() user.reported.add(entry) else: username = '******'.format(get_client_ip(request)) print('{} reported {}'.format(username, entry)) return HttpResponse(status=200)
def banners_to_view(page_type, context, request): if page_type == 'main': banners = list(Banner.objects.main_page_banners()) else: banners = list(Banner.objects.detail_page_banners()) for banner in banners: view = View.objects.create(banner=banner, ip=get_client_ip(request), meta=self.request.META) view.save() banner_dict = { 'banner{}'.format(elm.position.index): elm.code for elm in banners } context.update(banner_dict)
def _create_report(self): report = mainapp_models.Report(**self.get_report_data()) if self.request.user.is_authenticated(): report.user = self.request.user else: report.user = self._create_user() report.ip = get_client_ip(self.request) report.ward = mainapp_models.Ward.objects.get( geom__contains=self.get_point()) report.save() if report.user.is_confirmed: report.email_councillor() messages.success(self.request, _('Report was successfuly created')) else: messages.success( self.request, _('Report was successfuly created. But not yet confirmed')) messages.success( self.request, _('Please check your email for the confirmation link')) return report
def detect_user_language(): if not 'country_code' in session: client_info = json.loads(get_client_ip(request)) session['country_code'] = client_info['country_code']
def new(request): form_error = category_error = title = street = author = email = phone = desc = pnt = city = None if request.method == "POST": POST = request.POST.copy() point_str = "POINT(" + POST['lon'] + " " + POST['lat'] + ")" pnt = fromstr(point_str, srid=4326) title = POST['title'] street = POST['street'] author = POST['author'] email = POST['email'] phone = POST['phone'] desc = POST['desc'] city = POST['city'] update_form = ReportUpdateForm({'email': email, 'desc': desc, 'author': author, 'phone': phone}, request.FILES) report_form = ReportForm({'title': title, 'street': street}, request.FILES) if update_form.is_valid and report_form.is_valid: pass else: HttpResponseRedirect('/') if request.POST.get('step') == '2': # this is a lot more complicated than it has to be because of the infortmation # spread across two records. if request.POST['category_id'] != "" and update_form.is_valid() and report_form.is_valid(): report = report_form.save(commit=False) report.point = pnt report.category_id = request.POST['category_id'] report.ip = utils.get_client_ip(request) report.author = request.POST['author'] report.desc = request.POST['desc'] report.ward = Ward.objects.get(geom__contains=pnt) report.save() update = update_form.save(commit=False) update.report = report update.ip = utils.get_client_ip(request) update.first_update = True update.created_at = report.created_at update.save() return HttpResponseRedirect(report.get_absolute_url()) # other form errors are handled by the form objects. if not request.POST['category_id']: category_error = _("Please select a category") else: return HttpResponseRedirect('/') return render_to_response("reports/new.html", {"lat": pnt.y, "lon": pnt.x, "city": city, "google": FixMyStreetMap(pnt, True), "title": title, "street": street, "author": author, "email": email, "phone": phone, "update_form": update_form, "report_form": report_form, "categories": ReportCategory.objects.all().order_by("category_class"), "category_error": category_error, "form_error": form_error}, context_instance=RequestContext(request))