Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain/plain/views/redirect.py: 45%

1 import logging

2

3 from plain.http import (

4 ResponseGone,

5 ResponsePermanentRedirect,

6 ResponseRedirect,

7)

8 from plain.urls import reverse

9

10 from .base import View

11

12 logger = logging.getLogger("plain.request")

13

14

15 class RedirectView(View):

16 """Provide a redirect on any GET request."""

17

18 permanent = False

19 url: str | None = None

20 pattern_name: str | None = None

21 query_string = False

22

23 def get_redirect_url(self):

24 """

25 Return the URL redirect to. Keyword arguments from the URL pattern

26 match generating the redirect request are provided as kwargs to this

27 method.

28 """

29 if self.url:

30 url = self.url % self.url_kwargs

31 elif self.pattern_name:

32 url = reverse(self.pattern_name, args=self.url_args, kwargs=self.url_kwargs)

33 else:

34 return None

35

36 args = self.request.META.get("QUERY_STRING", "")

37 if args and self.query_string:

38 url = f"{url}?{args}"

39 return url

40

41 def get(self):

42 url = self.get_redirect_url()

43 if url:

44 if self.permanent:

45 return ResponsePermanentRedirect(url)

46 else:

47 return ResponseRedirect(url)

48 else:

49 logger.warning(

50 "Gone: %s",

51 self.request.path,

52 extra={"status_code": 410, "request": self.request},

53 )

54 return ResponseGone()

55

56 def head(self):

57 return self.get()

58

59 def post(self):

60 return self.get()

61

62 def options(self):

63 return self.get()

64

65 def delete(self):

66 return self.get()

67

68 def put(self):

69 return self.get()

70

71 def patch(self):

72 return self.get()