o
    [hF                     @   s   d Z ddlZddl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 Zd	d
 Zdd Zdd ZG dd dZG dd dZG dd dZG dd deZG dd deZG dd deZG dd deZeZdS )a  
Syndication feed generation library -- used for generating RSS, etc.

Sample usage:

>>> from django.utils import feedgenerator
>>> feed = feedgenerator.Rss201rev2Feed(
...     title="Poynter E-Media Tidbits",
...     link="http://www.poynter.org/column.asp?id=31",
...     description="A group blog by the sharpest minds in online journalism.",
...     language="en",
... )
>>> feed.add_item(
...     title="Hello",
...     link="http://www.holovaty.com/test/",
...     description="Testing."
... )
>>> with open('test.rss', 'w') as fp:
...     feed.write(fp, 'utf-8')

For definitions of the different versions of RSS, see:
https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
    N)StringIO)urlparse)
iri_to_uri)SimplerXMLGeneratorc                 C   s*   t | tjstj| t } tj| S N)
isinstancedatetimecombinetimeemailutilsformat_datetimedate r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/utils/feedgenerator.pyrfc2822_date#   s   r   c                 C   s<   t | tjstj| t } |  |  d u rd S d S )NZ )r   r   r	   r
   	isoformat	utcoffsetr   r   r   r   rfc3339_date)   s   r   c                 C   s8   t | }d}|durd|d }d|j||j|jf S )z
    Create a TagURI.

    See
    https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
    r   Nz,%sz%Y-%m-%dztag:%s%s:%s/%s)r   strftimehostnamepathfragment)urlr   bitsdr   r   r   get_tag_uri/   s
   r   c                 C   s    t  }|td || S )z
    Return the given stylesheet's mimetype tuple, using a slightly custom
    version of Python's mimetypes.guess_type().
    ztext/xsl	xsl
text/xsl	xslt)	mimetypes	MimeTypesreadfpr   
guess_type)r   Z
mimetypedbr   r   r   _guess_stylesheet_mimetype=   s   
r$   c                   @   sB   e Zd ZdZdddZedd Zedd	 Zd
d Zdd Z	dS )
StylesheetzAn RSS stylesheetr   screenc                 C   s   || _ || _|| _d S r   )_url	_mimetypemedia)selfr   mimetyper)   r   r   r   __init__O   s   
zStylesheet.__init__c                 C   
   t | jS r   )r   r'   r*   r   r   r   r   V   s   
zStylesheet.urlc                 C   s   | j dkrt| jd S | j S )Nr   r   )r(   r$   r   r.   r   r   r   r+   Z   s   
zStylesheet.mimetypec                 C   sV   d| j  dg}| jd ur|d| j d | jd ur&|d| j d d|S )Nzhref=""ztype="zmedia=" )r   r+   appendr)   join)r*   datar   r   r   __str__`   s   


zStylesheet.__str__c                 C   s   t | j| j| jfS r   )reprr   r+   r)   r.   r   r   r   __repr__h      zStylesheet.__repr__N)r   r&   )
__name__
__module____qualname____doc__r,   propertyr   r+   r4   r6   r   r   r   r   r%   L   s    


r%   c                   @   s   e Zd ZdZ											dddZ												dddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )SyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc                    s   dd }dd  |	odd |	D }	|d ur-t |ttfr$td|j  fdd|D }||t|||||||||t||||	pHd	t|
|||pQ||||d
|| _g | _d S )Nc                 S      | d urt | S | S r   strsr   r   r   to_str   r7   z(SyndicationFeed.__init__.<locals>.to_strc                 S   s   t | tr| S t| S r   )r   r%   rA   r   r   r   to_stylesheet   s   z/SyndicationFeed.__init__.<locals>.to_stylesheetc                 S   s   g | ]}t |qS r   r?   .0cr   r   r   
<listcomp>       z,SyndicationFeed.__init__.<locals>.<listcomp>z"stylesheets should be a list, not c                       g | ]} |qS r   r   )rF   rB   rD   r   r   rH      rI   r   )titlelinkdescriptionlanguageauthor_emailauthor_nameauthor_linksubtitle
categoriesfeed_urlfeed_copyrightidttlstylesheets)r   r%   r@   	TypeError	__class__r   feeditems)r*   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   Z	feed_guidrX   rY   kwargsrC   r   rK   r   r,   o   s6   

zSyndicationFeed.__init__r   c                    s   dd  |o fdd|D }| j  |t| | | |t||| | |	|
|p0d|p3d | |d| dS )z
        Add an item to the feed. All args are expected to be strings except
        pubdate and updateddate, which are datetime.datetime objects, and
        enclosures, which is an iterable of instances of the Enclosure class.
        c                 S   r>   r   r?   rA   r   r   r   rC      r7   z(SyndicationFeed.add_item.<locals>.to_strc                    rJ   r   r   rE   rC   r   r   rH      rI   z,SyndicationFeed.add_item.<locals>.<listcomp>r   )rL   rM   rN   rP   rQ   rR   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresrT   item_copyrightrX   N)r]   r1   r   )r*   rL   rM   rN   rP   rQ   rR   r`   rb   rc   rd   rT   rf   rX   ra   re   r^   r   r_   r   add_item   s,   zSyndicationFeed.add_itemc                 C   r-   r   )lenr]   r.   r   r   r   	num_items   s   
zSyndicationFeed.num_itemsc                 C      i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r   r.   r   r   r   root_attributes      zSyndicationFeed.root_attributesc                 C      dS )zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr   r*   handlerr   r   r   add_root_elements   rl   z!SyndicationFeed.add_root_elementsc                 C   rm   )zE
        Add stylesheet(s) to the feed. Called from write().
        Nr   rn   r   r   r   add_stylesheets      zSyndicationFeed.add_stylesheetsc                 C   rj   )zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        r   )r*   itemr   r   r   item_attributes   rr   zSyndicationFeed.item_attributesc                 C   rm   )zF
        Add elements on each item (i.e. item/entry) element.
        Nr   r*   ro   rs   r   r   r   add_item_elements   rr   z!SyndicationFeed.add_item_elementsc                 C   s   t d)z
        Output the feed in the given encoding to outfile, which is a file-like
        object. Subclasses should override this.
        z;subclasses of SyndicationFeed must provide a write() method)NotImplementedError)r*   outfileencodingr   r   r   write   s   zSyndicationFeed.writec                 C   s   t  }| || | S )zD
        Return the feed in the given encoding as a string.
        )r   rz   getvalue)r*   ry   rB   r   r   r   writeString  s   zSyndicationFeed.writeStringc                 C   sV   d}d}| j D ]}|D ]}||}|r|du s||kr|}qq|p*tjjtjjdS )z
        Return the latest item's pubdate or updateddate. If no items
        have either of these attributes this return the current UTC date/time.
        N)ra   r`   )tz)r]   getr   nowtimezoneutc)r*   Zlatest_dateZ	date_keysrs   Zdate_keyZ	item_dater   r   r   latest_post_date	  s   

z SyndicationFeed.latest_post_date)NNNNNNNNNNN)NNNNNNNr   NNNN)r8   r9   r:   r;   r,   rg   ri   rk   rp   rq   rt   rv   rz   r|   r   r   r   r   r   r=   l   sH    
9
2	r=   c                   @      e Zd ZdZdd ZdS )	EnclosurezAn RSS enclosurec                 C   s   ||| _ | _t|| _dS )z#All args are expected to be stringsN)length	mime_typer   r   )r*   r   r   r   r   r   r   r,     s   zEnclosure.__init__N)r8   r9   r:   r;   r,   r   r   r   r   r         r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )RssFeedz"application/rss+xml; charset=utf-8c                 C   sl   t ||dd}|  | | |d|   |d|   | | | | | | |	d d S )NTZshort_empty_elementsZrsschannel)
r   startDocumentrq   startElementrss_attributesrk   rp   write_itemsendChannelElement
endElementr*   rx   ry   ro   r   r   r   rz   '  s   



zRssFeed.writec                 C   s   | j ddS )Nhttp://www.w3.org/2005/Atom)versionz
xmlns:atom)_versionr.   r   r   r   r   4  s   zRssFeed.rss_attributesc                 C   8   | j D ]}|d| | | || |d qd S )Nrs   r]   r   rt   rv   r   ru   r   r   r   r   :  
   
zRssFeed.write_itemsc                 C   s$   | j d pg D ]}|d| qd S )NrY   zxml-stylesheet)r\   ZprocessingInstruction)r*   ro   
stylesheetr   r   r   rq   @  s   zRssFeed.add_stylesheetsc                 C   s   | d| jd  | d| jd  | d| jd  | jd d ur/| dd d| jd d | jd d ur?| d| jd  | jd	 D ]}| d
| qD| jd d ur]| d| jd  | dt|   | jd d ury| d| jd  d S d S )NrL   rM   rN   rU   z	atom:linkr*   relhrefrO   rT   categoryrV   	copyrightZlastBuildDaterX   )addQuickElementr\   r   r   r*   ro   catr   r   r   rp   D  s"   zRssFeed.add_root_elementsc                 C   s   | d d S )Nr   )r   rn   r   r   r   r   V  s   zRssFeed.endChannelElementN)
r8   r9   r:   content_typerz   r   r   rq   rp   r   r   r   r   r   r   $  s    r   c                   @   r   )RssUserland091Feedz0.91c                 C   sD   | d|d  | d|d  |d d ur | d|d  d S d S )NrL   rM   rN   )r   ru   r   r   r   rv   ]  s
   z$RssUserland091Feed.add_item_elementsNr8   r9   r:   r   rv   r   r   r   r   r   Z  r   r   c                   @   r   )Rss201rev2Feedz2.0c                 C   s  | d|d  | d|d  |d d ur| d|d  |d r5|d r5| dd|d |d f  n|d rB| d|d  n|d rQ| d|d d	d
i |d d ura| dt|d  |d d uro| d|d  |d d uri }t|dtrt|d  |d< | d|d | |d d ur| d|d  |d rt|d }t|dkrt	d|d }| dd|j
|j|jd |d D ]}| d| qd S )NrL   rM   rN   rQ   rP   authorz%s (%s)z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/r`   ZpubDaterb   rc   rd   ZisPermaLinkZguidrX   re      zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   	enclosurer   )r   r   typerT   r   )r   r   r   r~   boolr@   lowerlistrh   
ValueErrorr   r   r   )r*   ro   rs   Z
guid_attrsre   r   r   r   r   r   rv   h  sZ   z Rss201rev2Feed.add_item_elementsNr   r   r   r   r   r   d  s    r   c                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )	Atom1Feedz#application/atom+xml; charset=utf-8r   c                 C   sH   t ||dd}|  |d|   | | | | |d d S )NTr   r\   )r   r   r   rk   rp   r   r   r   r   r   r   rz     s   

zAtom1Feed.writec                 C   s*   | j d d ur| j| j d dS d| jiS )NrO   )xmlnszxml:langr   )r\   nsr.   r   r   r   rk     s   
zAtom1Feed.root_attributesc                 C   sZ  | d| jd  | ddd| jd d | jd d ur*| ddd| jd d | d| jd  | d	t|   | jd
 d urx|di  | d| jd
  | jd d urc| d| jd  | jd d urs| d| jd  |d | jd d ur| d| jd  | jd D ]}| ddd|i q| jd d ur| d| jd  d S d S )NrL   rM   r   	alternater   rU   r*   rW   updatedrQ   r   namerP   r   rR   urirS   rT   r   termrV   rights)r   r\   r   r   r   r   r   r   r   r   rp     s2   
zAtom1Feed.add_root_elementsc                 C   r   )Nentryr   ru   r   r   r   r     r   zAtom1Feed.write_itemsc              
   C   s  | d|d  | dd|d dd |d d ur$| dt|d  |d d ur4| d	t|d  |d
 d uri|di  | d|d
  |d d urV| d|d  |d d urd| d|d  |d |d d urt|d }n	t|d |d }| d| |d d ur| d|d ddi |d D ]}| ddd|j|j|jd q|d D ]}| ddd|i q|d d ur| d|d  d S d S )NrL   rM   r   r   )r   r   r`   Z	publishedra   r   rQ   r   r   rP   r   rR   r   rc   rW   rN   summaryr   htmlre   r   )r   r   r   r   rT   r   r   rf   r   )r   r   r   r   r   r   r   r   )r*   ro   rs   rc   r   r   r   r   r   rv     sF   

zAtom1Feed.add_item_elementsN)
r8   r9   r:   r   r   rz   rk   rp   r   rv   r   r   r   r   r     s    r   )r;   r   r   r    ior   urllib.parser   Zdjango.utils.encodingr   Zdjango.utils.xmlutilsr   r   r   r   r$   r%   r=   r   r   r   r   r   ZDefaultFeedr   r   r   r   <module>   s*      0	6
<j