def episode(request, episode): site = RequestSite(request) podcast = episode.podcast if not check_publisher_permission(request.user, podcast): return HttpResponseForbidden() if request.method == 'POST': form = None # EpisodeForm(request.POST, instance=e) # if form.is_valid(): # form.save() elif request.method == 'GET': form = None # EpisodeForm(instance=e) timeline_data = list(episode_listener_data(episode)) return render( request, 'publisher/episode.html', { 'is_secure': request.is_secure(), 'domain': site.domain, 'episode': episode, 'podcast': podcast, 'form': form, 'timeline_data': timeline_data, }, )
def episode(request, episode): site = RequestSite(request) podcast = episode.podcast if not check_publisher_permission(request.user, podcast): return HttpResponseForbidden() if request.method == "POST": form = None # EpisodeForm(request.POST, instance=e) # if form.is_valid(): # form.save() elif request.method == "GET": form = None # EpisodeForm(instance=e) timeline_data = list(episode_listener_data(episode)) return render( request, "publisher/episode.html", { "is_secure": request.is_secure(), "domain": site.domain, "episode": episode, "podcast": podcast, "form": form, "timeline_data": timeline_data, }, )
def get_track_kwargs(self, request, response=None): """ Get kwargs that can be passed to the TRACKING_FUNCTION (track_page_view by default). Return value must be JSON serializable if settings.PIWIK_USE_CELERY. """ # copy the META members that might actually be HTTP headers headers = dict(( k, v.decode('cp1250') ) # There have been some wacky bytes in headers. Make sure they can be JSON serialized for k, v in request.META.items() if isinstance(k, str) and isinstance(v, str)) headers['HTTPS'] = request.is_secure() userid = request.user.username if hasattr( request, 'user') and request.user.is_authenticated else None kwargs = { 'headers': headers, 'title': None, 'userid': userid, 'reqtime': time.time(), #'session_key': request.COOKIES.get(SESSION_COOKIE_NAME, None), 'status_code': response.status_code if response else None, } return kwargs
def episode(request, episode): site = RequestSite(request) podcast = episode.podcast if not check_publisher_permission(request.user, podcast): return HttpResponseForbidden() if request.method == 'POST': form = None # EpisodeForm(request.POST, instance=e) # if form.is_valid(): # form.save() elif request.method == 'GET': form = None # EpisodeForm(instance=e) timeline_data = list(episode_listener_data(episode)) return render( request, 'publisher/episode.html', { 'is_secure': request.is_secure(), 'domain': site.domain, 'episode': episode, 'podcast': podcast, 'form': form, 'timeline_data': timeline_data, }, )
def get_track_kwargs(self, request, response=None): """ Get kwargs that can be passed to the TRACKING_FUNCTION (track_page_view by default). Return value must be JSON serializable if settings.PIWIK_USE_CELERY. """ # copy the META members that might actually be HTTP headers headers = dict( (k, v.decode('cp1250')) # There have been some wacky bytes in headers. Make sure they can be JSON serialized for k,v in request.META.items() if isinstance(k, str) and isinstance(v, str) ) headers['HTTPS'] = request.is_secure() userid = request.user.username if hasattr(request, 'user') and request.user.is_authenticated else None kwargs = { 'headers': headers, 'title': None, 'userid': userid, 'reqtime': time.time(), #'session_key': request.COOKIES.get(SESSION_COOKIE_NAME, None), 'status_code': response.status_code if response else None, } return kwargs
def process_response(self, request, response): try: if response.status_code == 404: log('got 404') url = request.get_full_path() parts = urlsplit(url) if parts.scheme == '': if request.is_secure(): scheme = 'https' else: scheme = 'http' else: scheme = parts.scheme old_host = request.get_host() new_host = settings.PROXY_404_SERVER if new_host != old_host: log(f'old host: {old_host}, new host: {new_host}') #DEBUG new_url = urlunsplit([scheme, new_host, parts.path, parts.query, parts.fragment]) log(f'url: {url}') #DEBUG log('parts: %s' % repr(parts)) #DEBUG log(f'new url: {new_url}') #DEBUG try: log(f'opening {new_url}') #DEBUG stream = urllib.request.urlopen(new_url) log(f'reading {new_url}') #DEBUG try: new_response = http.HttpResponse(stream.read()) finally: stream.close() if new_response.status_code == 404: log('404 ' % new_url) #DEBUG else: log(f'got {new_url}') #DEBUG response = new_response except Exception: # just log it and return the exsting 404 log(f'url: {url}') log('parts: %s' % repr(parts)) log(f'new url: {new_url}') log(traceback.format_exc()) except Exception: log(traceback.format_exc()) raise return response
def get_base_url(request=None): proto = 'https' if request.is_secure() else 'http' addr = request.META.get("SERVER_ADDR") if not addr: addr = request.get_host() else: port = int(request.META.get("SERVER_PORT", 80)) if ((proto == 'http' and port != 80) or (proto == 'https' and port != 443)): addr = "%s:%d" % (addr, port) try: IPv6Address(addr) addr = "[%s]" % addr except: pass return "%s://%s" % (proto, addr)
def store_data(request): """Save user PII and budget info to database""" # Process form data if POST request if request.method == 'POST': form = SubmitBudgetForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] ward = form.cleaned_data['ward'] budget = form.cleaned_data['json_data'] # Generate a new uuid id = uuid.uuid4() # save to database new_budget = models.BudgetSubmission() new_budget.the_id = id new_budget.submitter_email = email new_budget.submitter_json = json.loads(budget) new_budget.submitter_ward = ward new_budget.save() # send email to user send_email(email, id) # Confirm submitted data in template return render( request, 'store-data.html', { 'email': email, 'ward': ward, 'id': id, 'viewUrl': ('https://' if request.is_secure() else 'http://') + request.get_host() + '/' + str(id) + '/view/' }) else: return render(request, 'submit-budget.html', {'form': form}) else: return redirect("/change-the-budget")
def get_base_url(request=None): proto = 'https' if request.is_secure() else 'http' addr = request.META.get("SERVER_ADDR") if not addr: addr = request.get_host() else: port = int(request.META.get("SERVER_PORT", 80)) if ( (proto == 'http' and port != 80) or (proto == 'https' and port != 443) ): addr = "%s:%d" % (addr, port) try: IPv6Address(addr) addr = "[%s]" % addr except: pass return "%s://%s" % (proto, addr)
def transDownloadView(request, slug): import urllib.request try: query_data = Transmission.objects.filter(slug=slug) if not query_data: raise Http404 except Transmission.DoesNotExist: raise Http404 query_data2 = limit_transmission_history(request, query_data) if not query_data2: raise Http404 # Just raise 404 if its too old restricted, new_query = restrict_talkgroups(request, query_data) if not new_query: raise Http404 trans = new_query[0] if trans.audio_file_type == 'm4a': audio_type = 'audio/m4a' else: audio_type = 'audio/mp3' response = HttpResponse(content_type=audio_type) start_time = timezone.localtime( trans.start_datetime).strftime('%Y%m%d_%H%M%S') filename = '{}_{}.{}'.format(start_time, trans.talkgroup_info.slug, trans.audio_file_type) response['Content-Disposition'] = 'attachment; filename="{}"'.format( filename) url = 'https:{}{}.{}'.format(trans.audio_url, trans.audio_file, trans.audio_file_type) if trans.audio_url[:2] != '//': url = 'http:' if request.is_secure(): url = 'https:' url += '//{}/{}{}.{}'.format(request.get_host(), trans.audio_url, trans.audio_file, trans.audio_file_type) req = urllib.request.Request(url) with urllib.request.urlopen(req) as web_response: response.write(web_response.read()) return response
def get_redirect_url(cls, request): protocol = "http" + ("s" if request.is_secure() else "") + "://" return protocol + request.get_host() + reverse( "utils:facebook_connect")
def plugin_fcgi_client(request, name, oid, path): """ This is a view that works as a FCGI client It is used for development server (no nginx) for easier development """ jc = JailsConfiguration.objects.all() if not jc.exists(): raise Http404 jc = jc[0] qs = models.Plugins.objects.filter(id=oid, plugin_name=name) if not qs.exists(): raise Http404 plugin = qs[0] try: jail = Jails.objects.filter(jail_host=plugin.plugin_jail)[0] except IndexError: raise Http404 jail_ip = jail.jail_ipv4_addr fastcgi_env_path = "%s/%s/%s/fastcgi_env" % (jc.jc_path, jail.jail_host, plugin.plugin_path) app = FCGIApp(host=str(jail_ip), port=plugin.plugin_port) env = request.META.copy() try: if os.path.exists(fastcgi_env_path): plugin_fascgi_env = {} exec( compile( open(fastcgi_env_path).read(), fastcgi_env_path, 'exec'), {}, plugin_fascgi_env) env.update(plugin_fascgi_env) except Exception as e: log.debug("Failed to update CGI headers: %s", e) env.pop('wsgi.file_wrapper', None) env.pop('wsgi.version', None) env.pop('wsgi.input', None) env.pop('wsgi.errors', None) env.pop('wsgi.multiprocess', None) env.pop('wsgi.run_once', None) env['SCRIPT_NAME'] = env['PATH_INFO'] if request.is_secure(): env['HTTPS'] = 'on' # Always use Ipv4 to talk to plugins try: host_ip = get_ipv4_addr() if host_ip: env['SERVER_ADDR'] = host_ip env['HTTP_HOST'] = host_ip except: log.debug('Failed to get default ipv4 for plugin comm', exc_info=True) args = request.POST if request.method == "POST" else request.GET try: status, headers, body, err = app(env, args=args) except socket.error as e: resp = HttpResponse(str(e)) resp.status_code = 503 return resp if err: log.debug('Error in FastCGI proxy call %r', err) resp = HttpResponse(body) for header, value in headers: resp[header] = value return resp
def plugin_fcgi_client(request, name, oid, path): """ This is a view that works as a FCGI client It is used for development server (no nginx) for easier development """ jc = JailsConfiguration.objects.all() if not jc.exists(): raise Http404 jc = jc[0] qs = models.Plugins.objects.filter(id=oid, plugin_name=name) if not qs.exists(): raise Http404 plugin = qs[0] try: jail = Jails.objects.filter(jail_host=plugin.plugin_jail)[0] except IndexError: raise Http404 jail_ip = jail.jail_ipv4_addr fastcgi_env_path = "%s/%s/%s/fastcgi_env" % ( jc.jc_path, jail.jail_host, plugin.plugin_path ) app = FCGIApp(host=str(jail_ip), port=plugin.plugin_port) env = request.META.copy() try: if os.path.exists(fastcgi_env_path): plugin_fascgi_env = { } exec(compile(open(fastcgi_env_path).read(), fastcgi_env_path, 'exec'), {}, plugin_fascgi_env) env.update(plugin_fascgi_env) except Exception as e: log.debug("Failed to update CGI headers: %s", e) env.pop('wsgi.file_wrapper', None) env.pop('wsgi.version', None) env.pop('wsgi.input', None) env.pop('wsgi.errors', None) env.pop('wsgi.multiprocess', None) env.pop('wsgi.run_once', None) env['SCRIPT_NAME'] = env['PATH_INFO'] if request.is_secure(): env['HTTPS'] = 'on' # Always use Ipv4 to talk to plugins try: host_ip = get_ipv4_addr() if host_ip: env['SERVER_ADDR'] = host_ip env['HTTP_HOST'] = host_ip except: log.debug('Failed to get default ipv4 for plugin comm', exc_info=True) args = request.POST if request.method == "POST" else request.GET try: status, headers, body, err = app(env, args=args) except socket.error as e: resp = HttpResponse(str(e)) resp.status_code = 503 return resp if err: log.debug('Error in FastCGI proxy call %r', err) resp = HttpResponse(body) for header, value in headers: resp[header] = value return resp