本文整理汇总了Python中django.core.handlers.wsgi.WSGIRequest方法的典型用法代码示例。如果您正苦于以下问题:Python wsgi.WSGIRequest方法的具体用法?Python wsgi.WSGIRequest怎么用?Python wsgi.WSGIRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.handlers.wsgi
的用法示例。
在下文中一共展示了wsgi.WSGIRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_suggestion
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def random_suggestion(cls, request: WSGIRequest) -> HttpResponse:
"""This method returns a random suggestion from the database.
Depending on the value of :param playlist:,
either a previously pushed playlist or song is returned."""
suggest_playlist = request.GET["playlist"] == "true"
if not suggest_playlist:
if ArchivedSong.objects.count() == 0:
return HttpResponseBadRequest("No songs to suggest from")
index = random.randint(0, ArchivedSong.objects.count() - 1)
song = ArchivedSong.objects.all()[index]
return JsonResponse({"suggestion": song.displayname(), "key": song.id})
# exclude radios from suggestions
remaining_playlists = (
ArchivedPlaylist.objects.all()
.exclude(list_id__startswith="RD")
.exclude(list_id__contains="&list=RD")
)
if remaining_playlists.count() == 0:
return HttpResponseBadRequest("No playlists to suggest from")
index = random.randint(0, remaining_playlists.count() - 1)
playlist = remaining_playlists.all()[index]
return JsonResponse({"suggestion": playlist.title, "key": playlist.id})
示例2: request_radio
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def request_radio(self, request: WSGIRequest) -> HttpResponse:
"""Endpoint to request radio for the current song."""
# only get ip on user requests
if self.base.settings.basic.logging_enabled:
request_ip, _ = ipware.get_client_ip(request)
if request_ip is None:
request_ip = ""
else:
request_ip = ""
try:
current_song = CurrentSong.objects.get()
except CurrentSong.DoesNotExist:
return HttpResponseBadRequest("Need a song to play the radio")
provider = SongProvider.create(self, external_url=current_song.external_url)
return provider.request_radio(request_ip)
示例3: post_song
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def post_song(self, request: WSGIRequest) -> HttpResponse:
"""This endpoint is part of the API and exempt from CSRF checks.
Shareberry uses this endpoint."""
# only get ip on user requests
if self.base.settings.basic.logging_enabled:
request_ip, _ = ipware.get_client_ip(request)
if request_ip is None:
request_ip = ""
else:
request_ip = ""
query = request.POST.get("query")
if not query:
return HttpResponseBadRequest("No query to share.")
# Set the requested platform to 'spotify'.
# It will automatically fall back to Youtube
# if Spotify is not enabled or a youtube link was requested.
successful, message, _ = self.do_request_music(
request_ip, query, None, False, "spotify"
)
if not successful:
return HttpResponseBadRequest(message)
return HttpResponse(message)
示例4: disabled_when_voting
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def disabled_when_voting(func: Callable) -> Callable:
"""A decorator for controls that are disabled during voting.
Only users with appropriate privileges are still able to perform this action."""
def _decorator(
self: "Controller", request: WSGIRequest, *args, **kwargs
) -> HttpResponse:
if (
self.musiq.base.settings.basic.voting_system
and not self.musiq.base.user_manager.has_controls(request.user)
):
return HttpResponseForbidden()
func(self, request, *args, **kwargs)
self.musiq.update_state()
return HttpResponse()
return wraps(func)(_decorator)
示例5: set_volume
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def set_volume(self, request: WSGIRequest) -> None:
"""Sets the playback volume.
value has to be a float between 0 and 1."""
self.volume = float(request.POST.get("value")) # type: ignore
try:
# Try to set the volume via the pulse server.
# This is faster and does not impact visualization
subprocess.run(
f"pactl set-sink-volume @DEFAULT_SINK@ {round(self.volume*100)}%".split(),
env={"PULSE_SERVER": "127.0.0.1"},
check=True,
)
except (FileNotFoundError, subprocess.CalledProcessError):
# pulse is not installed or there is no server running.
# change mopidy's volume
with self.playback.mopidy_command() as allowed:
if allowed:
self.playback.player.mixer.set_volume(round(self.volume * 100))
示例6: remove
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def remove(self, request: WSGIRequest) -> HttpResponse:
"""Removes a song identified by the given key from the queue."""
key = request.POST.get("key")
if key is None:
return HttpResponseBadRequest()
ikey = int(key)
try:
removed = self.playback.queue.remove(ikey)
self.playback.queue_semaphore.acquire(blocking=False)
# if we removed a song and it was added by autoplay,
# we want it to be the new basis for autoplay
if not removed.manually_requested:
self.playback.handle_autoplay(removed.external_url or removed.title)
else:
self.playback.handle_autoplay()
except models.QueuedSong.DoesNotExist:
return HttpResponseBadRequest("song does not exist")
return HttpResponse()
示例7: reorder
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def reorder(self, request: WSGIRequest) -> HttpResponse:
"""Reorders the queue.
The song specified by element is inserted between prev and next."""
prev_key = request.POST.get("prev")
cur_key = request.POST.get("element")
next_key = request.POST.get("next")
if not cur_key:
return HttpResponseBadRequest()
if not prev_key:
iprev_key = None
else:
iprev_key = int(prev_key)
icur_key = int(cur_key)
if not next_key:
inext_key = None
else:
inext_key = int(next_key)
try:
self.playback.queue.reorder(iprev_key, icur_key, inext_key)
except ValueError:
return HttpResponseBadRequest("request on old state")
return HttpResponse()
示例8: enable_streaming
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def enable_streaming(self, _request: WSGIRequest) -> HttpResponse:
"""Enable icecast streaming."""
icecast_exists = False
for line in subprocess.check_output(
"systemctl list-unit-files --full --all".split(), universal_newlines=True
).splitlines():
if "icecast2.service" in line:
icecast_exists = True
break
if not icecast_exists:
return HttpResponseBadRequest("Please install icecast2")
subprocess.call(["sudo", "/usr/local/sbin/raveberry/enable_streaming"])
config_file = os.path.join(settings.BASE_DIR, "setup/mopidy_icecast.conf")
self.update_mopidy_config(config_file)
return HttpResponse()
示例9: get_latest_version
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def get_latest_version(self, _request: WSGIRequest) -> HttpResponse:
"""Looks up the newest version number from PyPi and returns it."""
try:
subprocess.run(
"pip3 install raveberry==nonexistingversion".split(),
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True,
)
except subprocess.CalledProcessError as e:
# parse the newest verson from pip output
for line in e.stderr.splitlines():
if "from versions" in line:
versions = [re.sub(r"[^0-9.]", "", token) for token in line.split()]
versions = [version for version in versions if version]
latest_version = versions[-1]
return HttpResponse(latest_version)
return HttpResponseBadRequest("Could not determine latest version.")
示例10: upgrade_raveberry
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def upgrade_raveberry(self, _request: WSGIRequest) -> HttpResponse:
"""Performs an upgrade of raveberry."""
@background_thread
def do_upgrade() -> None:
subprocess.call(
[
"sudo",
"/usr/local/sbin/raveberry/upgrade_raveberry",
os.path.join(settings.BASE_DIR, "config/raveberry.ini"),
]
)
do_upgrade()
return HttpResponse("Upgrading... Look for logs in /var/www/")
示例11: option
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def option(
func: Callable[[T, WSGIRequest], Optional[HttpResponse]]
) -> Callable[[T, WSGIRequest], HttpResponse]:
"""A decorator that makes sure that only the admin changes a setting."""
def _decorator(self: T, request: WSGIRequest) -> HttpResponse:
# don't allow option changes during alarm
if request.user.username != "admin":
return HttpResponseForbidden()
response = func(self, request)
self.base.settings.update_state()
if response is not None:
return response
return HttpResponse()
return wraps(func)(_decorator)
示例12: list_subdirectories
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def list_subdirectories(self, request: WSGIRequest) -> HttpResponse:
"""Returns a list of all subdirectories for the given path."""
path = request.GET.get("path")
if path is None:
return HttpResponseBadRequest("path was not supplied.")
basedir, subdirpart = os.path.split(path)
if path == "":
suggestions = ["/"]
elif os.path.isdir(basedir):
suggestions = [
os.path.join(basedir, subdir + "/")
for subdir in next(os.walk(basedir))[1]
if subdir.lower().startswith(subdirpart.lower())
]
suggestions.sort()
else:
suggestions = ["not a valid directory"]
if not suggestions:
suggestions = ["not a valid directory"]
return JsonResponse(suggestions, safe=False)
示例13: scan_library
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def scan_library(self, request: WSGIRequest) -> HttpResponse:
"""Scan the folder at the given path and add all its sound files to the database."""
library_path = request.POST.get("library_path")
if library_path is None:
return HttpResponseBadRequest("library path was not supplied.")
if not os.path.isdir(library_path):
return HttpResponseBadRequest("not a directory")
library_path = os.path.abspath(library_path)
self.scan_progress = "0 / 0 / 0"
self.base.settings.update_state()
self._scan_library(library_path)
return HttpResponse(
f"started scanning in {library_path}. This could take a while"
)
示例14: set_output_device
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def set_output_device(self, request: WSGIRequest) -> HttpResponse:
"""Sets the given device as default output device."""
device = request.POST.get("device")
if not device:
return HttpResponseBadRequest("No device selected")
try:
subprocess.run(
["pactl", "set-default-sink", device],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
env={"PULSE_SERVER": "127.0.0.1"},
check=True,
)
except subprocess.CalledProcessError as e:
return HttpResponseBadRequest(e.stderr)
# restart mopidy to apply audio device change
subprocess.call(["sudo", "/usr/local/sbin/raveberry/restart_mopidy"])
return HttpResponse(
f"Set default output. Restarting the current song might be necessary."
)
示例15: request_music
# 需要导入模块: from django.core.handlers import wsgi [as 别名]
# 或者: from django.core.handlers.wsgi import WSGIRequest [as 别名]
def request_music(self, request: WSGIRequest) -> HttpResponse:
"""Endpoint to request music. Calls internal function."""
key = request.POST.get("key")
query = request.POST.get("query")
playlist = request.POST.get("playlist") == "true"
platform = request.POST.get("platform")
if query is None or not platform:
return HttpResponseBadRequest(
"query, playlist and platform have to be specified."
)
ikey = None
if key:
ikey = int(key)
# only get ip on user requests
if self.base.settings.basic.logging_enabled:
request_ip, _ = ipware.get_client_ip(request)
if request_ip is None:
request_ip = ""
else:
request_ip = ""
successful, message, queue_key = self.do_request_music(
request_ip, query, ikey, playlist, platform
)
if not successful:
return HttpResponseBadRequest(message)
return JsonResponse({"message": message, "key": queue_key})