Ñò
¤.Jc           @   s  d  Z  d d k l Z d d k l Z d d k l Z d d k l Z d d k	 l
 Z
 d d k	 l Z d d k l Z d d	 k l Z d d
 k l Z d d k l Z d d k l Z d d d d d „ Z e e ƒ Z d d d d d „ Z e e ƒ Z d d d d „ Z d d d „ Z d S(   sG   
Views for creating, editing and viewing site-specific user profiles.

iÿÿÿÿ(   t   login_required(   t   User(   t   ObjectDoesNotExist(   t   reverse(   t   Http404(   t   HttpResponseRedirect(   t   get_object_or_404(   t   render_to_response(   t   RequestContext(   t   object_list(   t   utilss   profiles/create_profile.htmlc   
   
   C   s—  y# |  i  i ƒ  } t t d ƒ ƒ SWn t j
 o n X| d j o# t d d h |  i  i d 6ƒ} n | d j o t i ƒ  } n |  i	 d j o} | d |  i
 d |  i ƒ } | i ƒ  oQ | i d t ƒ } |  i  | _  | i ƒ  t | d	 ƒ o | i ƒ  n t | ƒ Sn
 | ƒ  } | d j o
 h  } n t |  ƒ } x; | i ƒ  D]- \ } }	 t |	 ƒ o
 |	 ƒ  p |	 | | <qLWt | h | d
 6d | ƒS(   se  
    Create a profile for the current user, if one doesn't already
    exist.
    
    If the user already has a profile, as determined by
    ``request.user.get_profile()``, a redirect will be issued to the
    :view:`profiles.views.edit_profile` view. If no profile model has
    been specified in the ``AUTH_PROFILE_MODULE`` setting,
    ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
    raised.
    
    **Optional arguments:**
    
    ``extra_context``
        A dictionary of variables to add to the template context. Any
        callable object in this dictionary will be called to produce
        the end result which appears in the context.

    ``form_class``
        The form class to use for validating and creating the user
        profile. This form class must define a method named
        ``save()``, implementing the same argument signature as the
        ``save()`` method of a standard Django ``ModelForm`` (this
        view will call ``save(commit=False)`` to obtain the profile
        object, and fill in the user before the final save). If the
        profile object includes many-to-many relations, the convention
        established by ``ModelForm`` of using a method named
        ``save_m2m()`` will be used, and so your form class should
        also define this method.
        
        If this argument is not supplied, this view will use a
        ``ModelForm`` automatically generated from the model specified
        by ``AUTH_PROFILE_MODULE``.
    
    ``success_url``
        The URL to redirect to after successful profile creation. If
        this argument is not supplied, this will default to the URL of
        :view:`profiles.views.profile_detail` for the newly-created
        profile object.
    
    ``template_name``
        The template to use when displaying the profile-creation
        form. If not supplied, this will default to
        :template:`profiles/create_profile.html`.
    
    **Context:**
    
    ``form``
        The profile-creation form.
    
    **Template:**
    
    ``template_name`` keyword argument, or
    :template:`profiles/create_profile.html`.
    
    t   profiles_edit_profilet   profiles_profile_detailt   kwargst   usernamet   POSTt   datat   filest   committ   save_m2mt   formt   context_instanceN(   t   usert   get_profileR   R   R   t   NoneR   R
   t   get_profile_formt   methodR   t   FILESt   is_validt   savet   Falset   hasattrR   R   t   itemst   callableR   (
   t   requestt
   form_classt   success_urlt   template_namet   extra_contextt   profile_objR   t   contextt   keyt   value(    (    s/   /var/www/Pootle/external_apps/profiles/views.pyt   create_profile   s:    ;	
	
 %s   profiles/edit_profile.htmlc   
   
   C   sk  y |  i  i ƒ  } Wn  t j
 o t t d ƒ ƒ SX| d j o# t d d h |  i  i d 6ƒ} n | d j o t i ƒ  } n |  i	 d j oG | d |  i
 d |  i d | ƒ } | i ƒ  o | i ƒ  t | ƒ Sn | d | ƒ } | d j o
 h  } n t |  ƒ } x; | i ƒ  D]- \ } }	 t |	 ƒ o
 |	 ƒ  p |	 | | <qWt | h | d	 6| d
 6d | ƒS(   sz  
    Edit the current user's profile.
    
    If the user does not already have a profile (as determined by
    ``User.get_profile()``), a redirect will be issued to the
    :view:`profiles.views.create_profile` view; if no profile model
    has been specified in the ``AUTH_PROFILE_MODULE`` setting,
    ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
    raised.
    
    **Optional arguments:**
    
    ``extra_context``
        A dictionary of variables to add to the template context. Any
        callable object in this dictionary will be called to produce
        the end result which appears in the context.

    ``form_class``
        The form class to use for validating and editing the user
        profile. This form class must operate similarly to a standard
        Django ``ModelForm`` in that it must accept an instance of the
        object to be edited as the keyword argument ``instance`` to
        its constructor, and it must implement a method named
        ``save()`` which will save the updates to the object. If this
        argument is not specified, this view will use a ``ModelForm``
        generated from the model specified in the
        ``AUTH_PROFILE_MODULE`` setting.
    
    ``success_url``
        The URL to redirect to following a successful edit. If not
        specified, this will default to the URL of
        :view:`profiles.views.profile_detail` for the profile object
        being edited.
    
    ``template_name``
        The template to use when displaying the profile-editing
        form. If not specified, this will default to
        :template:`profiles/edit_profile.html`.
    
    **Context:**
    
    ``form``
        The form for editing the profile.
        
    ``profile``
         The user's current profile.
    
    **Template:**
    
    ``template_name`` keyword argument or
    :template:`profiles/edit_profile.html`.
    
    t   profiles_create_profileR   R   R   R   R   R   t   instanceR   t   profileR   N(   R   R   R   R   R   R   R   R
   R   R   R   R   R   R   R   R    R!   R   (
   R"   R#   R$   R%   R&   R'   R   R(   R)   R*   (    (    s/   /var/www/Pootle/external_apps/profiles/views.pyt   edit_profilez   s2    8	!

 %

s   profiles/profile_detail.htmlc   
   
   C   sá   t  t d | ƒ} y | i ƒ  } Wn t j
 o t ‚ n X| d j	 o t | | ƒ o
 d } n | d j o
 h  } n t |  ƒ } x; | i ƒ  D]- \ } }	 t	 |	 ƒ o
 |	 ƒ  p |	 | | <q– Wt
 | h | d 6d | ƒS(   sj  
    Detail view of a user's profile.
    
    If no profile model has been specified in the
    ``AUTH_PROFILE_MODULE`` setting,
    ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
    raised.
    
    If the user has not yet created a profile, ``Http404`` will be
    raised.
    
    **Required arguments:**
    
    ``username``
        The username of the user whose profile is being displayed.
    
    **Optional arguments:**

    ``extra_context``
        A dictionary of variables to add to the template context. Any
        callable object in this dictionary will be called to produce
        the end result which appears in the context.

    ``public_profile_field``
        The name of a ``BooleanField`` on the profile model; if the
        value of that field on the user's profile is ``False``, the
        ``profile`` variable in the template will be ``None``. Use
        this feature to allow users to mark their profiles as not
        being publicly viewable.
        
        If this argument is not specified, it will be assumed that all
        users' profiles are publicly viewable.
    
    ``template_name``
        The name of the template to use for displaying the profile. If
        not specified, this will default to
        :template:`profiles/profile_detail.html`.
    
    **Context:**
    
    ``profile``
        The user's profile, or ``None`` if the user's profile is not
        publicly viewable (see the description of
        ``public_profile_field`` above).
    
    **Template:**
    
    ``template_name`` keyword argument or
    :template:`profiles/profile_detail.html`.
    
    R   R.   R   N(   R   R   R   R   R   R   t   getattrR   R    R!   R   (
   R"   R   t   public_profile_fieldR%   R&   R   R'   R(   R)   R*   (    (    s/   /var/www/Pootle/external_apps/profiles/views.pyt   profile_detailÖ   s"    6

 %s   profiles/profile_list.htmlc         K   s_   t  i ƒ  } | i i ƒ  } | d j	 o | i h t | 6  } n | | d <t |  d | | S(   sÉ  
    A list of user profiles.
    
    If no profile model has been specified in the
    ``AUTH_PROFILE_MODULE`` setting,
    ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
    raised.

    **Optional arguments:**

    ``public_profile_field``
        The name of a ``BooleanField`` on the profile model; if the
        value of that field on a user's profile is ``False``, that
        profile will be excluded from the list. Use this feature to
        allow users to mark their profiles as not being publicly
        viewable.
        
        If this argument is not specified, it will be assumed that all
        users' profiles are publicly viewable.
    
    ``template_name``
        The name of the template to use for displaying the profiles. If
        not specified, this will default to
        :template:`profiles/profile_list.html`.

    Additionally, all arguments accepted by the
    :view:`django.views.generic.list_detail.object_list` generic view
    will be accepted here, and applied in the same fashion, with one
    exception: ``queryset`` will always be the ``QuerySet`` of the
    model specified by the ``AUTH_PROFILE_MODULE`` setting, optionally
    filtered to remove non-publicly-viewable proiles.
    
    **Context:**
    
    Same as the :view:`django.views.generic.list_detail.object_list`
    generic view.
    
    **Template:**
    
    ``template_name`` keyword argument or
    :template:`profiles/profile_list.html`.
    
    t   querysetR%   N(   R
   t   get_profile_modelt   _default_managert   allR   t   filtert   TrueR	   (   R"   R1   R%   R   t   profile_modelR3   (    (    s/   /var/www/Pootle/external_apps/profiles/views.pyt   profile_list  s    -
N(   t   __doc__t   django.contrib.auth.decoratorsR    t   django.contrib.auth.modelsR   t   django.core.exceptionsR   t   django.core.urlresolversR   t   django.httpR   R   t   django.shortcutsR   R   t   django.templateR   t    django.views.generic.list_detailR	   t   profilesR
   R   R+   R/   R2   R:   (    (    (    s/   /var/www/Pootle/external_apps/profiles/views.pyt   <module>   s0   bXG