ó
Š*Pc           @   s  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 m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z m Z m Z e e d d ƒ Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   forms(   t	   ErrorDict(   t   settings(   t   ContentType(   t   Comment(   t   salted_hmact   constant_time_compare(   t
   force_text(   t   get_text_list(   t   timezone(   t	   ungettextt   ugettextt   ugettext_lazyt   COMMENT_MAX_LENGTHi¸  t   CommentSecurityFormc           B   s³   e  Z d  Z e j d e j ƒ Z e j d e j ƒ Z e j d e j ƒ Z	 e j d d d d d e j ƒ Z
 d d d „ Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   sI   
    Handles the security aspects (anti-spoofing) for comment forms.
    t   widgett
   min_lengthi(   t
   max_lengthc         C   sT   | |  _  | d  k r i  } n  | j |  j ƒ  ƒ t t |  ƒ j d | d | ƒ d  S(   Nt   datat   initial(   t   target_objectt   Nonet   updatet   generate_security_datat   superR   t   __init__(   t   selfR   R   R   (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyR      s
    		c         C   sJ   t  ƒ  } x: d d d g D]) } | |  j k r |  j | | | <q q W| S(   s1   Return just those errors associated with securityt   honeypott	   timestampt   security_hash(   R   t   errors(   R   R   t   f(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   security_errors   s
    	c         C   s‰   i |  j  j d d ƒ d 6|  j  j d d ƒ d 6|  j  j d d ƒ d 6} |  j |   } |  j d } t | | ƒ s… t j d ƒ ‚ n  | S(   s   Check the security hash.t   content_typet    t	   object_pkR   R   s   Security hash check failed.(   R   t   gett   generate_security_hasht   cleaned_dataR   R    t   ValidationError(   R   t   security_hash_dictt   expected_hasht   actual_hash(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   clean_security_hash'   s    c         C   s9   |  j  d } t j ƒ  | d k r5 t j d ƒ ‚ n  | S(   s>   Make sure the timestamp isn't too far (> 2 hours) in the past.R   i   i<   s   Timestamp check failedix   i   (   R&   t   timeR    R'   (   R   t   ts(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   clean_timestamp4   s    c         C   sb   t  t j ƒ  ƒ } i t |  j j ƒ d 6t |  j j ƒ  ƒ d 6t | ƒ d 6|  j | ƒ d 6} | S(   s4   Generate a dict of security data for "initial" data.R!   R#   R   R   (   t   intR,   t   strR   t   _metat   _get_pk_valt   initial_security_hash(   R   R   t   security_dict(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyR   ;   s    c         C   sI   i t  |  j j ƒ d 6t  |  j j ƒ  ƒ d 6t  | ƒ d 6} |  j |   S(   sm   
        Generate the initial security hash from self.content_object
        and a (unix) timestamp.
        R!   R#   R   (   R0   R   R1   R2   R%   (   R   R   t   initial_security_dict(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyR3   F   s
    c         C   s7   | | | f } d } d j  | ƒ } t | | ƒ j ƒ  S(   sG   
        Generate a HMAC security hash from the provided info.
        s(   django.contrib.forms.CommentSecurityFormt   -(   t   joinR   t	   hexdigest(   R   R!   R#   R   t   infot   key_saltt   value(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyR%   S   s    N(   t   __name__t
   __module__t   __doc__R    t	   CharFieldt   HiddenInputR!   R#   t   IntegerFieldR   R   R   R   R    R+   R.   R   R3   R%   (    (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyR      s   !					t   CommentDetailsFormc           B   s¶   e  Z d  Z e j d e d ƒ d d ƒ Z e j d e d ƒ ƒ Z e j	 d e d ƒ d e
 ƒ Z e j d e d ƒ d	 e j d e ƒ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   sL   
    Handles the specific details of the comment (name, comment, etc.).
    t   labelt   NameR   i2   s   Email addresst   URLt   requiredR   R   c         C   sL   |  j  ƒ  s t d ƒ ‚ n  |  j ƒ  } | |  j ƒ    } |  j | ƒ } | S(   s6  
        Return a new (unsaved) comment object based on the information in this
        form. Assumes that the form is already validated and will throw a
        ValueError if not.

        Does not set any of the fields that would come from a Request object
        (i.e. ``user`` or ``ip_address``).
        s4   get_comment_object may only be called on valid forms(   t   is_validt
   ValueErrort   get_comment_modelt   get_comment_create_datat   check_for_duplicate_comment(   R   t   CommentModelt   new(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   get_comment_objectf   s    	c         C   s   t  S(   sì   
        Get the comment model to create with this form. Subclasses in custom
        comment apps should override this, get_comment_create_data, and perhaps
        check_for_duplicate_comment to provide custom comment models.
        (   R   (   R   (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyRI   x   s    c         C   s†   t  d t j j |  j ƒ d t |  j j ƒ  ƒ d |  j d d |  j d d |  j d d	 |  j d	 d
 t j	 ƒ  d t
 j d t d t ƒ 
S(   sæ   
        Returns the dict of data to be used to create a comment. Subclasses in
        custom comment apps that override get_comment_model can override this
        method to add extra fields onto a custom comment model.
        R!   R#   t	   user_namet   namet
   user_emailt   emailt   user_urlt   urlt   commentt   submit_datet   site_idt	   is_publict
   is_removed(   t   dictR   t   objectst   get_for_modelR   R   R2   R&   R	   t   nowR   t   SITE_IDt   Truet   False(   R   (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyRJ   €   s    	c         C   s   |  j  ƒ  j j |  j j j ƒ j d | j d | j d | j	 d | j
 d | j ƒ } xB | D]: } | j j ƒ  | j j ƒ  k r[ | j | j k r[ | Sq[ W| S(   s¼   
        Check that a submitted comment isn't a duplicate. This might be caused
        by someone posting a comment twice. If it is a dup, silently return the *previous* comment.
        R!   R#   RO   RQ   RS   (   RI   t   _default_managert   usingR   t   _statet   dbt   filterR!   R#   RO   RQ   RS   RV   t   dateRU   (   R   RM   t   possible_duplicatest   old(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyRK   “   s    				0c      
   C   sÄ   |  j  d } t j t k rÀ g  t j D] } | | j ƒ  k r& | ^ q& } | rÀ t j t d d t	 | ƒ ƒ t
 g  | D]/ } d | d d t	 | ƒ d | d f ^ qu t d	 ƒ ƒ ƒ ‚ qÀ n  | S(
   s†   
        If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn't
        contain anything in PROFANITIES_LIST.
        RU   s2   Watch your mouth! The word %s is not allowed here.s4   Watch your mouth! The words %s are not allowed here.s   "%s%s%s"i    R6   i   iÿÿÿÿt   and(   R&   R   t   COMMENTS_ALLOW_PROFANITIESR`   t   PROFANITIES_LISTt   lowerR    R'   R
   t   lenR   R   (   R   RU   t   wt	   bad_wordst   i(    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   clean_comment§   s    .	O(   R<   R=   R>   R    R?   t   _RP   t
   EmailFieldRR   t   URLFieldR`   RT   t   TextareaR   RU   RN   RI   RJ   RK   Rq   (    (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyRB   \   s   					t   CommentFormc           B   s/   e  Z e j d  e d e d ƒ ƒ Z d „  Z RS(   RF   RC   sH   If you enter anything in this field your comment will be treated as spamc         C   s3   |  j  d } | r/ t j |  j d j ƒ ‚ n  | S(   s4   Check that nothing's been entered into the honeypot.R   (   R&   R    R'   t   fieldsRC   (   R   R;   (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   clean_honeypot½   s    (   R<   R=   R    R?   R`   Rr   R   Rx   (    (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyRv   ¸   s   (   R,   t   djangoR    t   django.forms.utilR   t   django.confR   t"   django.contrib.contenttypes.modelsR   t   django.contrib.comments.modelsR   t   django.utils.cryptoR   R   t   django.utils.encodingR   t   django.utils.textR   t   django.utilsR	   t   django.utils.translationR
   R   R   Rr   t   getattrR   t   FormR   RB   Rv   (    (    (    s6   ../Django//lib/python/django/contrib/comments/forms.pyt   <module>   s   M\