
    sghF                         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 Zd Zd	 Zd
 Z G d d      Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      ZeZy)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                     t        | t        j                        s2t        j                  j                  | t        j                               } t        j
                  j                  |       S N)
isinstancedatetimecombinetimeemailutilsformat_datetimedates    ^/var/www/enzed_healthcare/enzed_env/lib/python3.12/site-packages/django/utils/feedgenerator.pyrfc2822_dater   #   sE    dH--.  ((x}}?;;&&t,,    c                     t        | t        j                        s2t        j                  j                  | t        j                               } | j	                         | j                         dz   S dz   S )NZ )r	   r
   r   r   	isoformat	utcoffsetr   s    r   rfc3339_dater   )   sV    dH--.  ((x}}?>>dnn&6&>sGGBGGr   c                     t        |       }d}|d|j                  d      z  }d|j                  |d|j                  d|j                  S )z
    Create a TagURI.

    See
    https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
    r   z,%sz%Y-%m-%dztag::/)r   strftimehostnamepathfragment)urlr   bitsds       r   get_tag_urir%   /   sG     C=D
ADMM*--#}}aDMMJJr   c                     t        j                         }|j                  t        d             |j	                  |       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"   
mimetypedbs     r   _guess_stylesheet_mimetyper,   =   s9    
 $$&J
 h>?@  %%r   c                   D    e Zd ZdZddZed        Zed        Zd Zd Z	y)	
StylesheetzAn RSS stylesheetc                 .    || _         || _        || _        y r   )_url	_mimetypemedia)selfr"   mimetyper2   s       r   __init__zStylesheet.__init__O   s    	!
r   c                 ,    t        | j                        S r   )r   r0   r3   s    r   r"   zStylesheet.urlV   s    $))$$r   c                 h    | j                   dk(  rt        | j                        d   S | j                   S )Nr   r   )r1   r,   r"   r7   s    r   r4   zStylesheet.mimetypeZ   s,    >>R-dhh7::~~r   c                     d| j                    dg}| j                  |j                  d| j                   d       | j                  |j                  d| j                   d       dj	                  |      S )Nzhref=""ztype="zmedia=" )r"   r4   appendr2   join)r3   datas     r   __str__zStylesheet.__str__`   sk    
!$%==$KK&q12::!KK'$**Q/0xx~r   c                 Z    t        | j                  | j                  | j                  f      S r   )reprr"   r4   r2   r7   s    r   __repr__zStylesheet.__repr__h   s     TXXt}}djj9::r   N)r   screen)
__name__
__module____qualname____doc__r5   propertyr"   r4   r?   rB    r   r   r.   r.   L   s<     % %  
;r   r.   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zy)SyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc                    d }d }|	xr |	D cg c]  }t        |       c}}	|Ct        |t        t         f      rt        d|j                         |D cg c]
  } ||       }} ||      t        |       ||       ||       ||       ||      t        |       ||      |	xs dt        |
       ||      |xs | ||      |d|| _        g | _        y c c}w c c}w )Nc                      | t        |       S | S r   strss    r   to_strz(SyndicationFeed.__init__.<locals>.to_str       ]3q611r   c                 <    t        | t              r| S t        |       S r   )r	   r.   rP   s    r   to_stylesheetz/SyndicationFeed.__init__.<locals>.to_stylesheet   s    "1j11Dz!}Dr   z"stylesheets should be a list, not rI   )titlelinkdescriptionlanguageauthor_emailauthor_nameauthor_linksubtitle
categoriesfeed_urlfeed_copyrightidttlstylesheets)rO   r	   r.   	TypeError	__class__r   feeditems)r3   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   	feed_guidrb   rc   kwargsrR   rU   crQ   s                       r   r5   zSyndicationFeed.__init__o   s	   $	2	E  @Z$@SV$@
"+
C'8989N9N8OP  6AA=+AKA E]t$!+.x("<0!+.%k2x($*"8,$^4#t#;&
 
	" 
5 %A Bs   CC!c                 6   d }|xr |D cg c]
  } ||       c}}| j                   j                   ||      t        |       ||       ||       ||      t        |      || ||       ||	      |
|xs d|xs d ||       ||      d|       yc c}w )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                      | t        |       S | S r   rN   rP   s    r   rR   z(SyndicationFeed.add_item.<locals>.to_str   rS   r   rI   )rV   rW   rX   rZ   r[   r\   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresr^   item_copyrightrb   N)rg   r<   r   )r3   rV   rW   rX   rZ   r[   r\   rm   ro   rp   rq   r^   rs   rb   rn   rr   ri   rR   rj   s                      r   add_itemzSyndicationFeed.add_item   s    2	2  C
$C1VAY$C


"4(%k2 &| 4%k2)+6"*"8,#I.*@(.B(.B"("8c{  !	
 %Ds   Bc                 ,    t        | j                        S r   )lenrg   r7   s    r   	num_itemszSyndicationFeed.num_items   s    4::r   c                     i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        rI   r7   s    r   root_attributeszSyndicationFeed.root_attributes   s	    
 	r   c                      y)zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        NrI   r3   handlers     r   add_root_elementsz!SyndicationFeed.add_root_elements   s    
 	r   c                      y)zE
        Add stylesheet(s) to the feed. Called from write().
        NrI   r{   s     r   add_stylesheetszSyndicationFeed.add_stylesheets        	r   c                     i S )zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        rI   )r3   items     r   item_attributeszSyndicationFeed.item_attributes   s	     	r   c                      y)zF
        Add elements on each item (i.e. item/entry) element.
        NrI   r3   r|   r   s      r   add_item_elementsz!SyndicationFeed.add_item_elements   r   r   c                     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)r3   outfileencodings      r   writezSyndicationFeed.write   s    
 "I
 	
r   c                 Z    t               }| j                  ||       |j                         S )zD
        Return the feed in the given encoding as a string.
        )r   r   getvalue)r3   r   rQ   s      r   writeStringzSyndicationFeed.writeString  s%     J

1hzz|r   c                     d}d}| j                   D ]'  }|D ]   }|j                  |      }|s|||kD  s|}" ) |xs8 t        j                  j                  t        j                  j
                        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)rn   rm   )tz)rg   getr
   nowtimezoneutc)r3   latest_date	date_keysr   date_key	item_dates         r   latest_post_datez SyndicationFeed.latest_post_date	  s    
 .	JJ 	0D% 0 HHX.	"*i+.E&/	0	0 Mh//33x7H7H7L7L3MMr   )NNNNNNNNNNN)NNNNNNNrI   NNNN)rD   rE   rF   rG   r5   rt   rw   ry   r}   r   r   r   r   r   r   rI   r   r   rK   rK   l   s    M 2r #!0
d
Nr   rK   c                       e Zd ZdZd Zy)	EnclosurezAn RSS enclosurec                 B    ||c| _         | _        t        |      | _        y)z#All args are expected to be stringsN)length	mime_typer   r"   )r3   r"   r   r   s       r   r5   zEnclosure.__init__  s    &,i#T^c?r   N)rD   rE   rF   rG   r5   rI   r   r   r   r     s
    #r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	RssFeedz"application/rss+xml; charset=utf-8c                 j   t        ||d      }|j                          | j                  |       |j                  d| j	                                |j                  d| j                                | j                  |       | j                  |       | j                  |       |j                  d       y )NTshort_empty_elementsrsschannel)
r   startDocumentr   startElementrss_attributesry   r}   write_itemsendChannelElement
endElementr3   r   r   r|   s       r   r   zRssFeed.write'  s    %gxdS 	W%UD$7$7$9:Y(<(<(>?w'!w'5!r   c                      | j                   ddS )Nhttp://www.w3.org/2005/Atom)versionz
xmlns:atom)_versionr7   s    r   r   zRssFeed.rss_attributes4  s    }}7
 	
r   c                     | j                   D ]F  }|j                  d| j                  |             | j                  ||       |j	                  d       H y )Nr   rg   r   r   r   r   r   s      r   r   zRssFeed.write_items:  sN    JJ 	'D  )=)=d)CD""7D1v&	'r   c                 X    | j                   d   xs g D ]  }|j                  d|        y )Nrc   zxml-stylesheet)rf   processingInstruction)r3   r|   
stylesheets      r   r   zRssFeed.add_stylesheets@  s2    ))M28b 	HJ))*:JG	Hr   c                    |j                  d| j                  d          |j                  d| j                  d          |j                  d| j                  d          | j                  d   #|j                  dd d| j                  d   d       | j                  d   |j                  d| j                  d          | j                  d	   D ]  }|j                  d
|        | j                  d   |j                  d| j                  d          |j                  dt        | j                                      | j                  d    |j                  d| j                  d          y y )NrV   rW   rX   r_   z	atom:linkr3   relhrefrY   r^   categoryr`   	copyrightlastBuildDaterb   )addQuickElementrf   r   r   r3   r|   cats      r   r}   zRssFeed.add_root_elementsD  sF   7);<		&(9:tyy/GH99Z ,##T6499Z;P#Q 99Z ,##J		*0EF99\* 	5C##J4	599%&2##K;K1LMd>S>S>U1VW99U'##E499U+;< (r   c                 &    |j                  d       y )Nr   )r   r{   s     r   r   zRssFeed.endChannelElementV  s    9%r   N)
rD   rE   rF   content_typer   r   r   r   r}   r   rI   r   r   r   r   $  s&    7L"
'H=$&r   r   c                       e Zd ZdZd Zy)RssUserland091Feedz0.91c                     |j                  d|d          |j                  d|d          |d   |j                  d|d          y y )NrV   rW   rX   )r   r   s      r   r   z$RssUserland091Feed.add_item_elements]  sO    g7V5*##M43FG +r   NrD   rE   rF   r   r   rI   r   r   r   r   Z  s    HHr   r   c                       e Zd ZdZd Zy)Rss201rev2Feedz2.0c                    |j                  d|d          |j                  d|d          |d   |j                  d|d          |d   r$|d   r|j                  d|d   d|d   d       n8|d   r|j                  d|d          n|d   r|j                  d	|d   d
di       |d   |j                  dt        |d                |d   |j                  d|d          |d   Vi }t        |j                  d      t              rt        |d         j                         |d<   |j                  d|d   |       |d   |j                  d|d          |d   rat        |d         }t        |      dkD  rt        d      |d   }|j                  dd|j                  |j                  |j                  d       |d   D ]  }|j                  d|        y )NrV   rW   rX   r[   rZ   authorz ()z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/rm   pubDatero   rp   rq   isPermaLinkguidrb   rr      zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   	enclosurer   )r"   r   typer^   r   )r   r   r	   r   boolrO   lowerlistrv   
ValueErrorr"   r   r   )r3   r|   r   
guid_attrsrr   r   r   s          r   r   z Rss201rev2Feed.add_item_elementsh  s   g7V5*##M43FG 4#7##tN';T-=PQ .!##Hd>.BC- ##]#?@ 	?&##I|DO/LM
'##JZ0@A(J$((#;<dC,/5M0N,O,U,U,W
=)##FD,=zJ;"##E4;7 d<01J:" Y  #1I##$=='..%// % 	5C##J4	5r   Nr   rI   r   r   r   r   d  s    H55r   r   c                   2    e Zd ZdZdZd Zd Zd Zd Zd Z	y)		Atom1Feedz#application/atom+xml; charset=utf-8r   c                     t        ||d      }|j                          |j                  d| j                                | j	                  |       | j                  |       |j                  d       y )NTr   rf   )r   r   r   ry   r}   r   r   r   s       r   r   zAtom1Feed.write  s_    %gxdSVT%9%9%;<w'!6"r   c                 t    | j                   d   | j                  | j                   d   dS d| j                  iS )NrY   )xmlnszxml:langr   )rf   nsr7   s    r   ry   zAtom1Feed.root_attributes  s8    99Z ,!WW$))J2GHHTWW%%r   c                    |j                  d| j                  d          |j                  ddd| j                  d   d       | j                  d   #|j                  ddd| j                  d   d       |j                  d| j                  d          |j                  d	t        | j                                      | j                  d
   |j	                  di        |j                  d| j                  d
          | j                  d   |j                  d| j                  d          | j                  d   |j                  d| j                  d          |j                  d       | j                  d   |j                  d| j                  d          | j                  d   D ]  }|j                  ddd|i        | j                  d    |j                  d| j                  d          y y )NrV   rW   r   	alternater   r_   r3   ra   updatedr[   r   namerZ   r   r\   urir]   r^   r   termr`   rights)r   rf   r   r   r   r   r   s      r   r}   zAtom1Feed.add_root_elements  s   7);<BTYYv5FG	
 99Z ,##FDIIj4IJ 	diio6	<8M8M8O+PQ99]#/  2.##FDIIm,DEyy(4''>1JKyy'3''tyy/GHx(99Z ,##J		*0EF99\* 	CC##JVSMB	C99%&2##Hdii8H.IJ 3r   c                     | j                   D ]F  }|j                  d| j                  |             | j                  ||       |j	                  d       H y )Nentryr   r   s      r   r   zAtom1Feed.write_items  sN    JJ 	(D  $*>*>t*DE""7D1w'	(r   c           
      Z   |j                  d|d          |j                  dd|d   dd       |d   |j                  dt        |d                |d   |j                  d	t        |d                |d
   l|j                  di        |j                  d|d
          |d   |j                  d|d          |d   |j                  d|d          |j                  d       |d   |d   }nt	        |d   |d         }|j                  d|       |d   |j                  d|d   ddi       |d   D ]8  }|j                  ddd|j
                  |j                  |j                  d       : |d   D ]  }|j                  ddd|i        |d   |j                  d|d          y y )NrV   rW   r   r   )r   r   rm   	publishedrn   r   r[   r   r   rZ   r   r\   r   rp   ra   rX   summaryr   htmlrr   r   )r   r   r   r   r^   r   r   rs   r   )r   r   r   r   r%   r"   r   r   )r3   r|   r   rp   r   r   s         r   r   zAtom1Feed.add_item_elements  s   g7T&\+,VW	?&##Kd9o1NO*##I|D<O/PQ *  2.##FD,?@N#/''n1EFM".''tM/BCx( ([)I#DL$y/BIi0 *##ItM/BVVDTU l+ 
	I##&%MM'..%//		
	 % 	CC##JVSMB	C  !-##Hd3C.DE .r   N)
rD   rE   rF   r   r   r   ry   r}   r   r   rI   r   r   r   r     s'    8L	&B#&K4(2Fr   r   )rG   r
   r   r'   ior   urllib.parser   django.utils.encodingr   django.utils.xmlutilsr   r   r   r%   r,   r.   rK   r   r   r   r   r   DefaultFeedrI   r   r   <module>r      s   0     ! , 5-HK&; ;@lN lN^# #3&o 3&lH H95W 95xeF eFT r   