Here are the examples of the python api django.http.get_host taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
11 Examples
3
Example 1
def get_url_host(request):
if request.is_secure():
protocol = 'https'
else:
protocol = 'http'
host = escape(get_host(request))
return '%s://%s' % (protocol, host)
3
Example 2
def _redirect(self, request, secure):
if settings.DEBUG and request.method == 'POST':
raise RuntimeError(
"""Django can't perform a SSL redirect while maintaining POST data.
Please structure your views so that redirects only occur during GETs.""")
protocol = secure and "https" or "http"
host = "%s://%s" % (protocol, get_host(request))
# In certain proxying situations, we need to strip out the 443 port
# in order to prevent inifinite redirects
if not secure:
host = host.replace(':443','')
if secure and SSLPORT:
host = "%s:%s" % (host, SSLPORT)
newurl = "%s%s" % (host, request.get_full_path())
return HttpResponseRedirect(newurl)
3
Example 3
Project: theyworkforyou Source File: doc.py
def bookmarklets(request):
# Hack! This couples this view to the URL it lives at.
admin_root = request.path[:-len('doc/bookmarklets/')]
return render_to_response('admin_doc/bookmarklets.html', {
'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', get_host(request), admin_root),
}, context_instance=RequestContext(request))
0
Example 4
def __init__(self, slug, request):
super(JobFeed, self).__init__(slug, request)
server_hostname = django.http.get_host(request)
self.full_link = 'http://' + server_hostname + self.link
0
Example 5
Project: hubplus Source File: views.py
def details(request, id, template_name="photos/details.html"):
"""
show the photo details
"""
photo = get_object_or_404(Image, id=id)
# @@@: test
if not photo.is_public and request.user != photo.member:
raise Http404
photo_url = photo.get_display_url()
tribes = []
projects = []
# Build a list of tribes and the photos from the pool
for tribe in Tribe.objects.filter(members=request.user):
phototribe = Tribe.objects.get(pk=tribe.id)
if phototribe.photos.filter(photo=photo).count():
tribes.append({
"name": tribe.name,
"slug": tribe.slug,
"id": tribe.id,
"has_photo": True,
})
else:
tribes.append({
"name": tribe.name,
"slug": tribe.slug,
"id": tribe.id,
"has_photo": False,
})
# Build a list of projects and the photos from the pool
for project in Project.objects.filter(members__user=request.user):
photoproject = Project.objects.get(pk=project.id)
if photoproject.photos.filter(photo=photo).count():
projects.append({
"name": project.name,
"slug": project.slug,
"id": project.id,
"has_photo": True,
})
else:
projects.append({
"name": project.name,
"slug": project.slug,
"id": project.id,
"has_photo": False,
})
title = photo.title
host = "http://%s" % get_host(request)
if photo.member == request.user:
is_me = True
else:
is_me = False
# TODO: check for authorized user and catch errors
if is_me:
if request.method == "POST" and request.POST["action"] == "add_to_project":
projectid = request.POST["project"]
myproject = Project.objects.get(pk=projectid)
if not myproject.photos.filter(photo=photo).count():
myproject.photos.create(photo=photo)
request.user.message_set.create(message=_("Successfully add photo '%s' to project") % title)
else:
# TODO: this applies to pinax in general. dont use ugettext_lazy here. its usage is fragile.
request.user.message_set.create(message=_("Did not add photo '%s' to project because it already exists.") % title)
return HttpResponseRedirect(reverse('photo_details', args=(photo.id,)))
if request.method == "POST":
if request.POST["action"] == "addtotribe":
tribeid = request.POST["tribe"]
mytribe = Tribe.objects.get(pk=tribeid)
if not mytribe.photos.filter(photo=photo).count():
mytribe.photos.create(photo=photo)
request.user.message_set.create(message=_("Successfully add photo '%s' to tribe") % title)
else:
# TODO: this applies to pinax in general. dont use ugettext_lazy here. its usage is fragile.
request.user.message_set.create(message=_("Did not add photo '%s' to tribe because it already exists.") % title)
return HttpResponseRedirect(reverse('photo_details', args=(photo.id,)))
if request.POST["action"] == "removefromtribe":
tribeid = request.POST["tribe"]
mytribe = Tribe.objects.get(pk=tribeid)
if mytribe.photos.filter(photo=photo).count():
mytribe.photos.filter(photo=photo).delete()
request.user.message_set.create(message=_("Successfully removed photo '%s' from tribe") % title)
else:
# TODO: this applies to pinax in general. dont use ugettext_lazy here. its usage is fragile.
request.user.message_set.create(message=_("Did not remove photo '%s' from tribe.") % title)
return HttpResponseRedirect(reverse('photo_details', args=(photo.id,)))
if request.POST["action"] == "addtoproject":
projectid = request.POST["project"]
myproject = Project.objects.get(pk=projectid)
if not myproject.photos.filter(photo=photo).count():
myproject.photos.create(photo=photo)
request.user.message_set.create(message=_("Successfully add photo '%s' to project") % title)
else:
# TODO: this applies to pinax in general. dont use ugettext_lazy here. its usage is fragile.
request.user.message_set.create(message=_("Did not add photo '%s' to project because it already exists.") % title)
return HttpResponseRedirect(reverse('photo_details', args=(photo.id,)))
if request.POST["action"] == "removefromproject":
projectid = request.POST["project"]
myproject = Project.objects.get(pk=projectid)
if myproject.photos.filter(photo=photo).count():
myproject.photos.filter(photo=photo).delete()
request.user.message_set.create(message=_("Successfully removed photo '%s' from project") % title)
else:
# TODO: this applies to pinax in general. dont use ugettext_lazy here. its usage is fragile.
request.user.message_set.create(message=_("Did not remove photo '%s' from project.") % title)
return HttpResponseRedirect(reverse('photo_details', args=(photo.id,)))
return render_to_response(template_name, {
"host": host,
"photo": photo,
"photo_url": photo_url,
"is_me": is_me,
"projects": projects,
"tribes": tribes,
}, context_instance=RequestContext(request))
0
Example 6
def get_url_host(request):
scheme = 'https' if request.is_secure() else 'http'
host = escape(get_host(request))
return '%s://%s' % (scheme, host)
0
Example 7
Project: hunch-sample-app Source File: middleware.py
def process_request(self, request):
"""
Rewrite the URL based on settings.SMART_APPEND_SLASH
"""
# Check for a redirect based on settings.SMART_APPEND_SLASH
host = http.get_host(request)
old_url = [host, request.path]
new_url = old_url[:]
# Append a slash if SMART_APPEND_SLASH is set and the resulting URL
# resolves.
if settings.SMART_APPEND_SLASH and (not old_url[1].endswith('/')) and not _resolves(old_url[1]) and _resolves(old_url[1] + '/'):
new_url[1] = new_url[1] + '/'
if settings.DEBUG and request.method == 'POST':
raise RuntimeError, "You called this URL via POST, but the URL doesn't end in a slash and you have SMART_APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to %s%s (note the trailing slash), or set SMART_APPEND_SLASH=False in your Django settings." % (new_url[0], new_url[1])
if new_url != old_url:
# Redirect
if new_url[0]:
newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', new_url[0], new_url[1])
else:
newurl = new_url[1]
if request.GET:
newurl += '?' + request.GET.urlencode()
return http.HttpResponsePermanentRedirect(newurl)
return None
0
Example 8
Project: pyamf Source File: views.py
def get_snapshots(http_request):
"""
Gets a list of snapshots in the images dir.
@return: list with 3 elements: URL of image folder, allowed filetypes and
the L{ArrayCollection} of snapshots
"""
url = base_url % get_host(http_request)
extensions = file_types.split(',')
l = []
for type in extensions:
location = os.path.join(gateway.images_root, '*.' + type.strip())
for img in glob.glob(location):
name = img[len(gateway.images_root) + 1:]
obj = {
'name': name
}
l.append(obj)
l.reverse()
return [url, extensions, ArrayCollection(l[:max_result])]
0
Example 9
def save_snapshot(http_request, image, type):
"""
Saves an image to the static image dir.
@param image: A L{pyamf.amf3.ByteArray} instance
"""
fp = tempfile.mkstemp(dir=gateway.images_root, prefix='snapshot_',
suffix='.' + type)
fp = open(fp[1], 'wb+')
fp.write(image.getvalue())
fp.close()
url = base_url % get_host(http_request)
name = fp.name[len(gateway.images_root) + 1:]
return {
'url': url + name,
'name': name
}
0
Example 10
def _add_protocol(self, request, protocol):
return "%s://%s%s" % (protocol, get_host(request), request.get_full_path())
0
Example 11
Project: theyworkforyou Source File: common.py
def process_response(self, request, response):
"Check for a flat page (for 404s) and calculate the Etag, if needed."
if response.status_code == 404:
if settings.SEND_BROKEN_LINK_EMAILS:
# If the referrer was from an internal link or a non-search-engine site,
# send a note to the managers.
domain = http.get_host(request)
referer = request.META.get('HTTP_REFERER', None)
is_internal = _is_internal_request(domain, referer)
path = request.get_full_path()
if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
ua = request.META.get('HTTP_USER_AGENT', '<none>')
mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
"Referrer: %s\nRequested URL: %s\nUser agent: %s\n" % (referer, request.get_full_path(), ua))
return response
# Use ETags, if requested.
if settings.USE_ETAGS:
etag = md5.new(response.content).hexdigest()
if request.META.get('HTTP_IF_NONE_MATCH') == etag:
response = http.HttpResponseNotModified()
else:
response['ETag'] = etag
return response