
    k"gf              	       z   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZ  G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d deee      Z G d de      Z G d dee      Z G d dee      Z  G d dee       Z! G d  d!eee      Z" G d" d#ee"      Z# G d$ d%eee      Z$ G d& d'ee$      Z% G d( d)eeee      Z& G d* d+ee&      Z' G d, d-e&      Z( G d. d/ee(      Z) G d0 d1eeeee      Z* G d2 d3ee*      Z+	 d7d4Z,d5 Z-d6 Z.y)8    N)settings)ImproperlyConfigured)models)Http404)timezone)cached_property)gettext)View)BaseDetailView!SingleObjectTemplateResponseMixin)MultipleObjectMixin#MultipleObjectTemplateResponseMixinc                   <    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zy)
	YearMixinz-Mixin for views manipulating year-based data.z%YNc                     | j                   S )zv
        Get a year format string in strptime syntax to be used to parse the
        year from url variables.
        )year_formatselfs    S/var/www/html/djangosite/lib/python3.12/site-packages/django/views/generic/dates.pyget_year_formatzYearMixin.get_year_format       
     c                     | j                   }|	 | j                  d   }|S |S # t        $ r? 	 | j                  j                  d   }Y |S # t        $ r t        t        d            w xY ww xY w)z8Return the year for which this view should display data.yearzNo year specified)r   kwargsKeyErrorrequestGETr   _)r   r   s     r   get_yearzYearMixin.get_year"       yy<:{{6* t  ::<<++F3D    :!!$7"899::   # 	A+A		A''A+c                      t        | |dd      S )zGet the next valid year.Fr   is_previousperiod_get_next_prevr   dates     r   get_next_yearzYearMixin.get_next_year/       dDeFKKr   c                      t        | |dd      S )zGet the previous valid year.Tr   r$   r'   r)   s     r   get_previous_yearzYearMixin.get_previous_year3       dDd6JJr   c                     	 |j                  |j                  dz   dd      S # t        $ r t        t	        d            w xY w)
        Return the start date of the next interval.

        The interval is defined by start date <= item date < next start date.
           r   monthdayDate out of range)replacer   
ValueErrorr   r   r)   s     r   _get_next_yearzYearMixin._get_next_year7   sD    	2<<TYY]!<CC 	2!/011	2s	    # Ac                 (    |j                  dd      S ).Return the start date of the current interval.r2   r4   r5   r7   r)   s     r   _get_current_yearzYearMixin._get_current_yearB   s    ||!|++r   )__name__
__module____qualname____doc__r   r   r   r    r+   r.   r9   r>    r   r   r   r      s/    7KD LK	2,r   r   c                   <    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zy)

MonthMixinz.Mixin for views manipulating month-based data.z%bNc                     | j                   S )zx
        Get a month format string in strptime syntax to be used to parse the
        month from url variables.
        )month_formatr   s    r   get_month_formatzMonthMixin.get_month_formatM       
    r   c                     | j                   }|	 | j                  d   }|S |S # t        $ r? 	 | j                  j                  d   }Y |S # t        $ r t        t        d            w xY ww xY w)z9Return the month for which this view should display data.r4   zNo month specified)r4   r   r   r   r   r   r   )r   r4   s     r   	get_monthzMonthMixin.get_monthT   s    

=;G, u  ;; LL,,W5E    ;!!$8"9::;;r"   c                      t        | |dd      S )zGet the next valid month.Fr4   r$   r'   r)   s     r   get_next_monthzMonthMixin.get_next_montha   s    dDeGLLr   c                      t        | |dd      S )zGet the previous valid month.Tr4   r$   r'   r)   s     r   get_previous_monthzMonthMixin.get_previous_monthe   s    dDd7KKr   c                     |j                   dk(  r"	 |j                  |j                  dz   dd      S |j                  |j                   dz   d      S # t        $ r t	        t        d            w xY w)r1      r2   r3   r6   r<   )r4   r7   r   r8   r   r   r)   s     r   _get_next_monthzMonthMixin._get_next_monthi   sq     ::6||QaQ|GG <<djj1n!<<<  6a 34556s    A A0c                 &    |j                  d      S )z/Return the start date of the previous interval.r2   )r5   r=   r)   s     r   _get_current_monthzMonthMixin._get_current_monthw   s    |||""r   )r?   r@   rA   rB   rG   r4   rH   rK   rM   rO   rR   rT   rC   r   r   rE   rE   G   s/    8LE!ML=#r   rE   c                   <    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zy)
DayMixinz,Mixin for views manipulating day-based data.z%dNc                     | j                   S )zt
        Get a day format string in strptime syntax to be used to parse the day
        from url variables.
        )
day_formatr   s    r   get_day_formatzDayMixin.get_day_format   s    
 r   c                     | j                   }|	 | j                  d   }|S |S # t        $ r? 	 | j                  j                  d   }Y |S # t        $ r t        t        d            w xY ww xY w)z7Return the day for which this view should display data.r5   zNo day specified)r5   r   r   r   r   r   r   )r   r5   s     r   get_dayzDayMixin.get_day   s    hh;9kk%( 
s
  99,,**51C 
   9!!$6"78899r"   c                      t        | |dd      S )zGet the next valid day.Fr5   r$   r'   r)   s     r   get_next_dayzDayMixin.get_next_day   s    dDeEJJr   c                      t        | |dd      S )zGet the previous valid day.Tr5   r$   r'   r)   s     r   get_previous_dayzDayMixin.get_previous_day   s    dDd5IIr   c                 4    |t        j                  d      z   S )r1   r2   days)datetime	timedeltar)   s     r   _get_next_dayzDayMixin._get_next_day   s     h((a000r   c                     |S r;   rC   r)   s     r   _get_current_dayzDayMixin._get_current_day   s    r   )r?   r@   rA   rB   rX   r5   rY   r[   r]   r_   re   rh   rC   r   r   rV   rV   |   s/    6J
CKJ1r   rV   c                   B    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zd
 Zy)	WeekMixinz-Mixin for views manipulating week-based data.%UNc                     | j                   S )zv
        Get a week format string in strptime syntax to be used to parse the
        week from url variables.
        )week_formatr   s    r   get_week_formatzWeekMixin.get_week_format   r   r   c                     | j                   }|	 | j                  d   }|S |S # t        $ r? 	 | j                  j                  d   }Y |S # t        $ r t        t        d            w xY ww xY w)z8Return the week for which this view should display data.weekzNo week specified)rp   r   r   r   r   r   r   )r   rp   s     r   get_weekzWeekMixin.get_week   r!   r"   c                      t        | |dd      S )zGet the next valid week.Frp   r$   r'   r)   s     r   get_next_weekzWeekMixin.get_next_week   r,   r   c                      t        | |dd      S )zGet the previous valid week.Trp   r$   r'   r)   s     r   get_previous_weekzWeekMixin.get_previous_week   r/   r   c                     	 |t        j                  d| j                  |      z
        z   S # t        $ r t	        t        d            w xY w)r1      ra   r6   )rc   rd   _get_weekdayOverflowErrorr   r   r)   s     r   _get_next_weekzWeekMixin._get_next_week   sM    	2(,,!d6G6G6M2MNNN 	2!/011	2s	   *- Ac                 P    |t        j                  | j                  |            z
  S rg   )rc   rd   rx   r)   s     r   _get_current_weekzWeekMixin._get_current_week   s#    h(():):4)@AAAr   c                     | j                         }|dv r|j                         S |dk(  r|j                         dz   dz  S t        d|z        )z
        Return the weekday for a given date.

        The first day according to the week format is 0 and the last day is 6.
        >   %V%Wrk   r2   rw   zunknown week format: %s)rn   weekdayr8   )r   r*   rm   s      r   rx   zWeekMixin._get_weekday   sU     **,,&<<>!D LLNQ&!++6DEEr   )r?   r@   rA   rB   rm   rp   rn   rq   rs   ru   rz   r|   rx   rC   r   r   rj   rj      s6    7KD LK	2BFr   rj   c                   @    e Zd ZdZdZdZd Zd Zed        Z	d Z
d Zy)		DateMixinz3Mixin class for views manipulating date-based data.NFc                 v    | j                   "t        d| j                  j                  z        | j                   S )z7Get the name of the date field to be used to filter by.z%s.date_field is required.)
date_fieldr   	__class__r?   r   s    r   get_date_fieldzDateMixin.get_date_field   s7    ??"&,t~~/F/FF  r   c                     | j                   S )zi
        Return `True` if the view should be allowed to display objects from
        the future.
        )allow_futurer   s    r   get_allow_futurezDateMixin.get_allow_future   rI   r   c                     | j                   | j                         j                   n| j                   }|j                  j                  | j	                               }t        |t        j                        S )zq
        Return `True` if the date field is a `DateTimeField` and `False`
        if it's a `DateField`.
        )modelget_queryset_meta	get_fieldr   
isinstancer   DateTimeField)r   r   fields      r   uses_datetime_fieldzDateMixin.uses_datetime_field  sW     .2ZZ-?!!#))TZZ%%d&9&9&;<%!5!566r   c                     | j                   r]t        j                  j                  |t        j                  j                        }t
        j                  rt        j                  |      }|S )z
        Convert a date into a datetime when the date field is a DateTimeField.

        When time zone support is enabled, `date` is assumed to be in the
        current time zone, so that displayed items are consistent with the URL.
        )	r   rc   combinetimeminr   USE_TZr   
make_aware)r   values     r   _make_date_lookup_argzDateMixin._make_date_lookup_arg  sK     ##%%--eX]]5F5FGE ++E2r   c                     | j                         }| j                  rE| j                  |      }| j                  |t        j                  d      z         }d|z  |d|z  |iS ||iS )z
        Get the lookup kwargs for filtering on a single date.

        If the date field is a DateTimeField, we can't just filter on
        date_field=date because that doesn't take the time into account.
        r2   ra   %s__gte%s__lt)r   r   r   rc   rd   )r   r*   r   sinceuntils        r   _make_single_date_lookupz"DateMixin._make_single_date_lookup  sv     ((*
##..t4E..th6H6Ha6P/PQEJ&:%u  %%r   )r?   r@   rA   rB   r   r   r   r   r   r   r   r   rC   r   r   r   r      s7    =JL! 7 7&r   r   c                   >    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
dd
Zy	)BaseDateListViewzFAbstract base class for date-based views displaying a list of objects.Fr   c                     | j                         \  | _        | _        } | j                  d| j                  | j                  d|}| j	                  |      S )N)object_list	date_listrC   )get_dated_itemsr   r   get_context_datarender_to_response)r   r   argsr   extra_contextcontexts         r   getzBaseDateListView.get4  s]    :>:N:N:P7(-'$'' 
((DNN
FS
 &&w//r   c                     t        d      )z#Obtain the list of dates and items.z>A DateView must provide an implementation of get_dated_items())NotImplementedErrorr   s    r   r   z BaseDateListView.get_dated_items;  s    !L
 	
r   c                 X    | j                   d| j                         z  S | j                   S )zu
        Return the field or fields to use for ordering the queryset; use the
        date field by default.
        -%s)orderingr   r   s    r   get_orderingzBaseDateListView.get_orderingA  s*    
 150Eut**,,X4==Xr   c                     | j                         j                  di |}| j                         }| j                         }| j	                         }| j                  |      }|sA| j                  rt        j                         n	t               } |j                  di d|z  |i}|sO|| n|j                          }|r7t        t        d      d|j                  j                  j                  iz        |S )zs
        Get a queryset properly filtered according to `allow_future` and any
        extra lookup kwargs.
        %s__lte$No %(verbose_name_plural)s availableverbose_name_pluralrC   )r   filterr   r   get_allow_emptyget_paginate_byr   r   nowtimezone_todayexistsr   r   r   r   r   )	r   lookupqsr   r   allow_emptypaginate_byr   is_emptys	            r   get_dated_querysetz#BaseDateListView.get_dated_querysetH  s    
 (T ''1&1((*
,,.**,**2.$($<$<(,,..BRC;i*4c:;B "-!42vbiik/H<=-rxx~~/Q/Q  	r   c                     | j                   S )zf
        Get the aggregation period for the list of dates: 'year', 'month', or
        'day'.
        )date_list_periodr   s    r   get_date_list_periodz%BaseDateListView.get_date_list_periode      
 $$$r   Nc                 J   | j                         }| j                         }|| j                         }| j                  r|j	                  |||      }n|j                  |||      }|;|s9|s7t        t        d      d|j                  j                  j                  iz        |S )z
        Get a date list by calling `queryset.dates/datetimes()`, checking
        along the way for empty lists that aren't allowed.
        r   r   )r   r   r   r   	datetimesdatesr   r   r   r   r   )r   queryset	date_typer   r   r   r   s          r   get_date_listzBaseDateListView.get_date_listl  s    
 ((*
**,113I## **:y(KI z9hGI ;89)8>>+?+?+S+S  r   )NASC)r?   r@   rA   rB   r   r   r   r   r   r   r   r   rC   r   r   r   r   .  s/    PK0
Y:%r   r   c                       e Zd ZdZdZd Zy)BaseArchiveIndexViewzQ
    Base class for archives of date-based items. Requires a response mixin.
    latestc                 v    | j                         }| j                  |d      }|s|j                         }||i fS ):Return (date_list, items, extra_context) for this request.DESC)r   )r   r   none)r   r   r   s      r   r   z$BaseArchiveIndexView.get_dated_items  s@    $$&&&rF&;	B2r""r   N)r?   r@   rA   rB   context_object_namer   rC   r   r   r   r     s     ##r   r   c                       e Zd ZdZdZy)ArchiveIndexViewz&Top-level archive of date-based items._archiveNr?   r@   rA   rB   template_name_suffixrC   r   r   r   r     s    0%r   r   c                   $    e Zd ZdZdZdZd Zd Zy)BaseYearArchiveView*List of objects published in a given year.r4   Fc                    | j                         }| j                         }t        || j                               }| j	                  |      }| j	                  | j                  |            }d|z  |d|z  |i} | j                  di |}| j                  |      }| j                         s|j                         }|||| j                  |      | j                  |      dfS )r   r   r   )r   	next_yearprevious_yearrC   )r    r   _date_from_stringr   r   r9   r   r   get_make_object_listr   r+   r.   )	r   r   r   r*   r   r   lookup_kwargsr   r   s	            r   r   z#BaseYearArchiveView.get_dated_items  s    }}((*
 t';';'=>**40**4+>+>t+DE
"Ez!5

 %T$$5}5&&r*	((* B !//5!%!7!7!=
 	
r   c                     | j                   S )zo
        Return `True` if this view should contain the full list of objects in
        the given year.
        )make_object_listr   s    r   r   z(BaseYearArchiveView.get_make_object_list  r   r   N)r?   r@   rA   rB   r   r   r   r   rC   r   r   r   r     s    4
@%r   r   c                       e Zd ZdZdZy)YearArchiveViewr   _archive_yearNr   rC   r   r   r   r         4*r   r   c                       e Zd ZdZdZd Zy)BaseMonthArchiveView+List of objects published in a given month.r5   c                    | j                         }| j                         }| j                         }t        || j	                         || j                               }| j                  |      }| j                  | j                  |            }d|z  |d|z  |i} | j                  di |}| j                  |      }	|	||| j                  |      | j                  |      dfS )r   r   r   )r4   
next_monthprevious_monthrC   )r    rK   r   r   r   rH   r   rR   r   r   rM   rO   )
r   r   r4   r   r*   r   r   r   r   r   s
             r   r   z$BaseMonthArchiveView.get_dated_items  s    }} ((*
 $&&(%1F1F1H
 **40**4+?+?+EF
"Ez!5

 %T$$5}5&&r*	 "11$7"&"9"9$"?
 	
r   N)r?   r@   rA   rB   r   r   rC   r   r   r   r     s    5
r   r   c                       e Zd ZdZdZy)MonthArchiveViewr   _archive_monthNr   rC   r   r   r   r     s    5+r   r   c                       e Zd ZdZd Zy)BaseWeekArchiveView*List of objects published in a given week.c                 j   | j                         }| j                         }| j                         }| j                         }dddd}	 ||   }| j                         }|dk(  r|dk7  rt        d	|d
|d      t        |||d||      }| j                  |      }	| j                  | j                  |            }
d|z  |	d|z  |
i} | j                  di |}d||| j                  |      | j                  |      dfS # t        $ r* t        d|ddj                  t        |                  w xY w)r   10)r   rk   r~   zUnknown week format z. Choices are: z, r~   z%GzISO week directive 'z+' is incompatible with the year directive 'z!'. Use the ISO year '%G' instead.z%wr   r   N)rp   	next_weekprevious_weekrC   )r    rq   r   rn   r   r8   joinsortedr   r   r   rz   r   rs   ru   )r   r   rp   r   rm   week_choices
week_startr   r*   r   r   r   r   s                r   r   z#BaseWeekArchiveView.get_dated_items  sj   }}}}((*
**,!C8		%k2J **,$;$#6  	  !{JdKX**40**4+>+>t+DE
"Ez!5

 %T$$5}5 !//5!%!7!7!=
 	
9  	  IIf\23 	s   C? ?3D2Nr?   r@   rA   rB   r   rC   r   r   r   r     s
    4.
r   r   c                       e Zd ZdZdZy)WeekArchiveViewr   _archive_weekNr   rC   r   r   r  r  /  r   r   r  c                       e Zd ZdZd Zd Zy)BaseDayArchiveView)List of objects published on a given day.c           	          | j                         }| j                         }| j                         }t        || j	                         || j                         || j                               }| j                  |      S r   )r    rK   r[   r   r   rH   rY   _get_dated_items)r   r   r4   r5   r*   s        r   r   z"BaseDayArchiveView.get_dated_items8  sn    }} lln   "!!#!
 $$T**r   c           	          | j                  |      } | j                  di |}d||| j                  |      | j                  |      | j	                  |      | j                  |      dfS )z
        Do the actual heavy lifting of getting the dated items; this accepts a
        date object so that TodayArchiveView can be trivial.
        N)r5   previous_daynext_dayr   r   rC   )r   r   r_   r]   rO   rM   )r   r*   r   r   s       r   r  z#BaseDayArchiveView._get_dated_itemsI  s{    
 55d;$T$$5}5  $ 5 5d ; --d3"&"9"9$"?"11$7

 
	
r   N)r?   r@   rA   rB   r   r  rC   r   r   r  r  5  s    3+"
r   r  c                       e Zd ZdZdZy)DayArchiveViewr  _archive_dayNr   rC   r   r   r  r  ^  s    3)r   r  c                       e Zd ZdZd Zy)BaseTodayArchiveView List of objects published today.c                 \    | j                  t        j                  j                               S r  )r  rc   r*   todayr   s    r   r   z$BaseTodayArchiveView.get_dated_itemsg  s     $$X]]%8%8%:;;r   Nr   rC   r   r   r  r  d  s
    *<r   r  c                       e Zd ZdZdZy)TodayArchiveViewr  r  Nr   rC   r   r   r  r  l  s    *)r   r  c                   $     e Zd ZdZd fd	Z xZS )BaseDateDetailView
    Detail view of a single object on a single date; this differs from the
    standard DetailView by accepting a year/month/day in the URL.
    c           	      `   | j                         }| j                         }| j                         }t        || j	                         || j                         || j                               }|| j                         n|}| j                         sm|t        j                  j                         kD  rLt        t        d      |j                  j                  j                   | j"                  j$                  dz        | j'                  |      } |j(                  di |}t*        | Y  |      S )z%Get the object this request displays.zZFuture %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.)r   
class_name)r   rC   )r    rK   r[   r   r   rH   rY   r   r   rc   r*   r  r   r   r   r   r   r   r?   r   r   super
get_object)	r   r   r   r4   r5   r*   r   r   r   s	           r   r  zBaseDateDetailView.get_objectx  s   }} lln   "!!#!
 %-$4T ($$&4(--2E2E2G+G<
 ,.88>>+M+M"&.."9"9		 	 55d;RYY''w!2!..r   )N)r?   r@   rA   rB   r  __classcell__)r   s   @r   r  r  r  s    
#/ #/r   r  c                       e Zd ZdZdZy)DateDetailViewr  _detailNr   rC   r   r   r  r    s    
 %r   r  c                    ||z   |z   |z   |z   }t        |       |z   t        |      z   |z   t        |      z   }	 t        j                  j                  ||      j                         S # t        $ r t        t        d      ||dz        w xY w)z
    Get a datetime.date object given a format string and a year, month, and day
    (only year is mandatory). Raise a 404 for an invalid date.
    uC   Invalid date string “%(datestr)s” given format “%(format)s”)datestrformat)strrc   strptimer*   r8   r   r   )	r   r   r4   rG   r5   rX   delimr#  r"  s	            r   r   r     s     5 </%7*DF$i%#e*,u4s3x?G	
  ))'6:??AA 
ST" 
 	

s   -A' '$Bc                 2   | j                         }| j                         }| j                         }t        | d|z        }t        | d|z        } ||       ||      }
}	|r6|r  ||	t	        j
                  d      z
        }n|
}|s|t               k  r|S y|rd|z  | j                  |	      i}d|z  }nd|z  | j                  |
      i}|}|s3| j                  rt        j                         }n
t               }||d	|z  <    | j                         j                  di |j                  |      }	 t        |d
   |      }| j                  r5t        j                   rt        j"                  |      }|j%                         } ||      S # t        $ r Y yw xY w)aV  
    Get the next or the previous valid date. The idea is to allow links on
    month/day views to never be 404s by never providing a date that'll be
    invalid for the given view.

    This is a bit complicated since it handles different intervals of time,
    hence the coupling to generic_view.

    However in essence the logic comes down to:

        * If allow_empty and allow_future are both true, this is easy: just
          return the naive result (just the next/previous day/week/month,
          regardless of object existence.)

        * If allow_empty is true, allow_future is false, and the naive result
          isn't in the future, then return it; otherwise return None.

        * If allow_empty is false and allow_future is true, return the next
          date *that contains a valid object*, even if it's in the future. If
          there are no next objects, return None.

        * If allow_empty is false and allow_future is false, return the next
          date that contains a valid object. If that date is in the future, or
          if there are no next objects, return None.
    z_get_current_%sz_get_next_%sr2   ra   Nr   r   r   r   r   rC   )r   r   r   getattrrc   rd   r   r   r   r   r   r   r   order_by
IndexErrorr   r   	localtimer*   )generic_viewr*   r%   r&   r   r   r   get_currentget_nextstartendresultr   r   r   r   s                   r   r(   r(     s   4 ,,.J..0K002L,(9F(BCK|^f%<=H T"HTN3E  ););)C!CDFF6^%55M +\-O-OPU-VWFz)H*,l.P.PQT.UVF!H  //lln$&-0F9z)*/\&&(//9&9BB8L	RUJ/F
 ++!++F3[[]F 6""  		s   2F
 
	FFc                      t         j                  rt        j                         S t        j
                  j                         S )z1Return the current date in the current time zone.)r   r   r   	localdaterc   r*   r  rC   r   r   r   r     s+    !!##}}""$$r   ) r4  r4  r4  __)/rc   django.confr   django.core.exceptionsr   	django.dbr   django.httpr   django.utilsr   django.utils.functionalr   django.utils.translationr	   r   django.views.generic.baser
   django.views.generic.detailr   r   django.views.generic.listr   r   r   rE   rV   rj   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r   r(   r   rC   r   r   <module>r@     sx      7   ! 3 1 */, /,d2# 2#j, ,^=F =F@@& @&FT*It Tn#+ #$&:<P &+%)%5 +%\+9;N +!
9j2B !
H,:<P ,1
)Y0@ 1
h+9;N +&
J:J &
R*8:L *<- <*:<P *)/J)^ )/X%68J % PT
*W#t%r   