
͝Mc        #   @   s  d  d k  Z  d  d k Z d  d k Z d  d k 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 l Z d  d k
 l Z d  d k l Z d  d	 k l Z l Z d  d
 k l Z d  d k l Z 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( l) Z) l* Z* d  d k+ l, Z, d  d k- l. Z. d  d k/ l0 Z0 d  d k1 l2 Z2 d  d k3 l4 Z5 d  d k6 l7 Z7 d  d k8 l9 Z9 l: Z: d  d k; l< Z< l= Z= d  d k> l? Z? d  d k@ lA ZA lB ZB lC ZC lD ZD d  d kE lF ZF lG ZG d  d kH lI ZI d   ZJ d    ZK d!   ZL d"   ZM d#   ZN d$   ZO d%   ZP d&   ZQ d'   ZR d d(  ZT e" d)    ZU d*   ZV d+   ZW d,   ZX d S(-   iN(   t   PoXliffFile(   t   data(   t   settings(   t   get_object_or_404(   t   HttpResponset   Http404(   t   render_to_response(   t   RequestContext(   t	   to_localet   ugettext(   t   parse_accept_lang_header(   t   PermissionDeniedt   ObjectDoesNotExist(   t   cache(   t
   simplejson(   t   escape(   t   never_cache(   t
   iri_to_uri(   t   redirect(   t   get_matching_permissionst   check_permissiont   check_profile_permission(   t   paginate(   t   get_profile(   t
   SearchForm(   t
   Submission(   t
   Suggestion(   t   ensure_target_dir_exists(   t   Storet   Unit(   t   unit_form_factoryt   highlight_whitespace(   t   highlight_diffs(   t   UNTRANSLATEDt   FUZZYt
   TRANSLATEDt   absolute_real_path(   t   factory_classest   is_monolingual(   t   translation_submittedc         C   so  | d d j o d | } n t  t d | } t i i | i  \ } } t i i d | t i i d  } t |  } t	 d |  } t
 i |  } | o# | | i   j o t i i |  p t |  | i t  }	 |	 i | i d t t i d | i d	 d
  \ }
 } t i |
  |	 i |  t i | |  t
 i | | i   t i  n t d |  S(   s2   export given file to xliff for offline translationi    t   /t   pootle_patht   POOTLE_EXPORTt   xlfs   %s:export_as_xlifft   createifmissingt   prefixt   suffixs   .xlfs   /export/(   R   R   t   ost   patht   splitextt	   real_patht   joint   extsepR$   R   R   t   gett	   get_mtimet   isfileR   t   convertR    t
   switchfilet   namet   Truet   tempfilet   mkstempt   closet   savefilet   shutilt   movet   setR   t   OBJECT_CACHE_TIMEOUTR   (   t   requestR)   t   storeR0   t   extt   export_patht   abs_export_patht   keyt   last_exportt   outputstoret   fdt	   tempstore(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   export_as_xliff;   s$    #-
! c         C   s  | d d j o d | } n t  t d | } t i | d	  } | p t |  p | i |  o
 t  n t i	 i
 | i  \ } } t i	 i d | t i	 i |  } t |  } t d | | f  }	 t i |	  }
 |
 o# |
 | i   j o t i	 i |  p t |  | i |  } t i d | i d t i	 i |  \ } } t i |  | i |  t i | |  t i |	 | i   t i  n t d |  S(
   s2   export given file to xliff for offline translationi    R(   R)   R*   s   %s:export_as_%sR-   R.   s   /export/N(    R   R   R%   R5   t   NoneR&   t   endswitht
   ValueErrorR/   R0   R1   R2   R3   R4   R$   R   R   R6   R7   R   R8   R<   R=   R:   R>   R?   R@   RA   RB   R   RC   R   (   RD   R)   t   filetypeRE   t   klassR0   RF   RG   RH   RI   RJ   RK   RL   RM   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   export_as_typeR   s(    %
#-
+ c         C   sR   | d d j o d | } n t  t d | } | i d t  t d | i  S(   Ni    R(   R)   t   update_translations   /export/(   R   R   t   syncR;   R   R2   (   RD   R)   RE   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   downloadk   s
    c         C   sX  | i  } | i } | i } | i i d | i | i f  i d |  } | i i   p d d k l	 } |  i
 i d d  } x t |  D] \ }	 }
 |	 d j o q n t t i t i |	    } t |	  } | d d	 | i | i f j p | d d	 | i | i f j o q n | i i d
 | | f d |  } | i   o Pq q Wn | S(   Nt   id__int   translationproject__projecti(   t   Languaget   HTTP_ACCEPT_LANGUAGEt    t   *t   ent   en_USt   code__in(   t   languaget   projectt   source_languaget   alt_src_langst   excludet   idt   filtert   countt   pootle_language.modelsRZ   t   METAR5   R
   R   R   t   normalize_codet   simplify_to_commont   codet   objects(   RD   t   profilet   translation_projectRa   Rb   Rc   t   langsRZ   t   acceptt   accept_langt   unusedt
   normalizedRm   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   get_alt_src_langst   s(    			- !c      
   C   sq  | } |  i  d i   } | i   } d |  i  d j o7 | } x  | D] } | i d |  } qF W| | B} n d |  i  d j o7 | } x  | D] } | i d |  } q W| | B} n d |  i  d j oS | } | } x2 | D]* } | i d |  } | i d	 |  } q W| | B| B} n d
 |  i  d j o7 | } x  | D] } | i d |  } qCW| | B} n | S(   Nt   searcht   sourcet   sfieldst   source_f__icontainst   targett   target_f__icontainst   notest   translator_comment__icontainst   developer_comment__icontainst	   locationst   locations__icontains(   t   cleaned_datat   splitt   noneRg   (   t   formt   units_querysett   resultt   wordst	   subresultt   wordt   translator_subresultt   developer_subresult(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt!   get_non_indexed_search_step_query   s<        c         C   s  |  i  d j o t i d |   t | |  St i d |  i  i |   g  } | i d i   } | i d } | i   i	 d d t
 i   } g  } | i   D] } | d | f q ~ }	 d t t t |	  |  i   t |  t |  f   }
 t i |
  } | d j o g  } xW | D]O } g  } | D] } | | | f q/~ } |  i  i | t  } | i |  qW|  i  i |	 t  } | i |  |  i  i | t
  } |  i  i | d	 g  } g  } | d
  D] } | t | d	 d  q~ } t i |
 | t i  n | i d |  S(   s8   Narrows down units query to units matching search stringu(   No indexer for %s, using database searchu-   Found %s indexer for %s, using indexed searchRw   Ry   t   store__pootle_patht   flatt
   pofilenames	   search:%st   dbidi  i    RX   N(   t   indexerRO   t   loggingt   debugR   t   INDEX_DIRECTORY_NAMER   R   t   order_byt   values_listR;   t   distinctt   iteratort   strt   hasht   reprR6   R   R5   t
   make_queryt   Falset   appendRw   t   intRB   R   RC   Rg   (   Rp   R   R   t   word_querylistR   t   fieldst   pathst   _[1]R)   t   path_querylistt	   cache_keyt   dbidst   searchpartsR   t   _[2]t   fieldt	   textqueryt	   pathqueryt   limitedqueryR   t   _[3]t   item(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   get_search_step_query   s4    	!-: '3c         C   s  d |  i  j p d |  i  j o | Sd |  i  j o |  i  d i d  } | o | i   } x | D]{ } | d j o | | i d t  B} qe | d j o | | i d t  B} qe | d j o | | i d t  B} qe qe W| } q n d	 |  i  j o |  i  d	 i d  } | om | i   } d
 | j o# | i d d  } | i	 d
  n | o  | | i d t
 d |  B} n | } qn | S(   sD   Narrows down unit query to units matching conditions in GET and POSTt   unitt   paget
   unitstatest   ,t   untranslatedt   statet
   translatedt   fuzzyt
   matchnamest   hassuggestiont
   suggestiont   qualitycheck__false_positivet   qualitycheck__name__inN(   t   GETR   R   Rg   R!   R#   R"   Re   RO   t   removeR   (   RD   R   R   t   state_querysett	   unitstateR   t   match_queryset(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   get_step_query   s6      c      
   C   s  d } d } d } d |  i j oL y) t |  i d  } | i d |  } Wqat i t f j
 o qaXnd |  i j oG t |  i  } | i	   } t
 |  | d | d d } | i d } nd |  i j od |  i j o|t |  i d  }	 t |  i d  }
 |  i d } |  i i d	 t  } | o8 | i d
 | d |
  | i d |  Bi d d  } n5 | i d
 | d |
  | i d |  Bi d
 d  } x | d  i   D] } | d j o | d j	 o | } Pn | i |	 j o
 | } q| i |
 j p | oL | i |
 j  o< t i d | |	 f  | } t i i d
 | d |	  } PqqWn | d j oQ | d j	 o | o
 | } qqy | d d !d } Wqt j
 o qXn | | | f S(   sG   returns current active unit, and in case of POST previously active unitR   Rf   R   t   itemst   orphansi    t   indexR)   t   backR   t
   index__ltet   store__pootle_path__lts   -store__pootle_paths   -indext
   index__gtet   store__pootle_path__gti@   u8   submitting to a unit no longer part of step query, %s:%di   N(   RO   R   R   R5   R   t   DoesNotExistRQ   R   t   usert   get_unit_rowsR   t   object_listt   POSTR   Rg   R   R   Rf   R   R   R   Rn   t
   IndexError(   RD   t   step_querysetR   t	   edit_unitt	   prev_unitt   pagert   edit_idRo   t	   unit_rowst   prev_idt
   prev_indexR)   R   t   querysetR   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   get_current_units   s^    	  
'	c         C   s   d |  i  j } |  i o' | o t d  } qc t d  } n$ | o t d  } n t d  } d |  i  j o# d |  i  j o t |  i   } n
 t   } h | d 6| d	 6| i d
 6| i d 6| i d 6| d 6| d 6} t d | d t |   S(   s=   render a message at end of review, translate or search actionR   s#   No more matching strings to review.s&   No more matching strings to translate.s   No matching strings to review.s!   No matching strings to translate.Rw   Ry   t
   endmessageRp   Ra   Rb   t	   directoryt   search_formt   checkss   store/translate_end.htmlt   context_instance(	   R   R   t   _R   Ra   Rb   R   R   R   (   RD   Rp   R   t   messageR   t   context(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   translate_end1  s(    
 	



c   )      C   s  t  d |   p t t d    n t  d |   } t  d |   } t  d |   } |  i } | i } t |  i  } d  }	 d  }
 d |  i j oI d |  i j o9 t	 |  i  }
 |
 i
   o t |  i |
 |  }	 q n
 t	   }
 |	 d  j o t |  |  }	 n t |  |	 |  \ } } } d  } | d  j	 od |  i j p d |  i j od |  i j o | p d |  i j o | o
 t  n | i   o t | i i  } n d  } t | |  } | |  i d	 | } | i
   o=| o d |  i j o | i i p | i i p | i i oU | i   t i d
 | d | i d |  t d | d t |  i   } | i   qq'| o d |  i j o | i i oz t i i d | i  } | i  | i! d t |  i   } | o5 t" i i# d | d t |  i  d d d | i  qqq'q+| } n | d  j o t$ |  |  S| d  j p | i
   oK | i   o t | i i  } n d  } t | |  } | d	 |  } n | d  j oe | i% } | } | i& d | d | i'  | i& d | i(  Bi)   } | i* i& d | i'  i)   } n+ | i* } | i& d | i'  i)   } | } | i+   } | d  j o3 | | d } t, |  | d | d | d d } n | d d } | | j oW| | } | i-   | i.   d } | | j  oY | i/   o | i* | } n
 | i* } | | } t, |  | d | d | d d i0 } q| | j  oM | | | } | i* | } | | } t, |  | d | d | d d i0 } q| | | j oU | | | d } | i* | } | | d } t, |  | d | d | d d i0 } q| i0 } n | i* |  } g  } xV |  i i1   D]E \ } }  | d: j o, x) |  D] }! | i2 d | |! f  qHWq(q(Wg  }" xf |  i i d d  i3 d   D]F }# |# p qn t4 |#  }$ d! |$ |$ f }% |" i2 t d" |%   qWt5 |  | |  }& |& i6 d# d$ t7 }' h | d% 6|& d& 6|' d' 6| d( 6| d) 6| d* 6| d+ 6|
 d, 6| d- 6| d 6| d. 6| d/ 6| d0 6| d 6| i8 d1 6| d 6| i8 i9 d2 6| i: d3 6d4 i; |  d5 6|" d6 6t< i= d7 6}( t> d8 |( d9 t? |   S(;   Nt   views:   You do not have rights to access this translation project.t	   translatet   suggestt   reviewRw   Ry   t   submitt   instancet   senderR   Ro   Rp   t	   submitterRf   t   target_ft	   suggesterR   t   pendingRE   t	   index__ltR   i   R   R   R   i    i   s   %s=%sR   R\   R   s`   <a href="http://translate.sourceforge.net/wiki/toolkit/pofilter_tests#%s" target="_blank">%s</a>s   checking %sRm   R   R   Rd   t   alt_src_codest   cantranslatet
   cansuggestt	   canreviewR   R   R   R   t   unitsRa   Rb   Rc   R   t   &t	   GET_stateR   t   MT_BACKENDSs   store/translate.htmlR   (   R   R   (@   R   R   R   Rp   Ra   R   R   RO   R   R   t   is_validR   R   R   R   t	   haspluralt   lenRx   t   stringsR   R   t   _target_updatedt   _translator_comment_updatedt   _state_updatedt   saveR'   t   sendR   R   Rn   R5   Rf   t   add_suggestionR   t   SuggestionStatt   get_or_createR   RE   Rg   R   R)   Rh   R   R   R   t	   end_indext   start_indext   has_other_pagesR   t   listsR   R   R   Rv   R   R;   Rb   Rc   t   parentR3   R   R   R   R   ()   RD   R   RE   R   R   R   Rp   Ra   Ro   R   R   R   R   R   R   t	   snpluralst
   form_classt   subt   suggt   pager_queryt	   precedingt   store_precedingR   R   t   context_rowst   unit_positiont   page_lengtht   units_queryR   t   offsett   GET_varsRI   t   valuest   valueR   t   checkt
   safe_checkt   linkRd   R   R   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   translate_pageQ  s   		 	-

" 
	"	%
	
(
((  # 

c         C   s   | d d j o d | } n y% t  i i d d  i d |  } Wn t  i j
 o t  n X| i |  _ t t |  i	  |  i i
  |  _ t |  | i d | S(   Ni    R(   Rp   R	  R)   RE   (   R   Rn   t   select_relatedR5   R   R   Rp   R   R   R   R   t   permissionsR  R   (   RD   R)   RE   (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyR     s    %!c         C   s  t  t d | } | i i } | i i } h | i d 6| d 6} |  i i d  oy | i i d |  } Wn t	 j
 o d  } n Xt |  i  } t | d |  o2 |  i i   p | o | i | j o
 t  n | i |  | d <| d  j	 og | d o\ t i i d | d | i d	 d
 d | i  \ }	 }
 t |  i  |	 _ d |	 _ |	 i   qin t i |  } t | d d S(   NRf   t   udbidt   sugidt   rejectR   t   successRp   R   R   R   R   t   rejectedt   mimetypes   application/json(   R   R   RE   R	  Rp   Rf   R   R5   t   suggestion_setR   RO   R   R   R   t   is_authenticatedR   t   reject_suggestionR  Rn   R  t   reviewerR   R   R   t   dumpsR   (   RD   t   uidt   suggidR   R   Rp   t   responseR  Ro   t   suggstatt   created(    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyR'    s4    

(
		c         C   sW  t  t d | } | i i } | i i } t t |  i  d |  p
 t  n h | i	 d 6| d 6} |  i
 i d  oy | i i d |  } Wn t j
 o d  } n X| i |  | d <g  } | i i D] }	 | t |	  q ~ | d <h  | d <xe | i   D]W }
 g  } t |
 i i  D]& \ } }	 | t | i i | |	  q'~ | d |
 i	 <q
W| d  j	 o | d o | i o# t i d	 | d
 | d | i  n t i i d | d | i d d d
 | i	  \ } } t |  i  | _ d | _ | i   t d | d t |  i  d |  } | i   q8n t i  |  } t! | d d S(   NRf   R   R  R   Rr   R"  t
   newtargetst   newdiffsR   R   Ro   Rp   R   R   R   t   acceptedR   t   from_suggestionR$  s   application/json("   R   R   RE   R	  Rp   R   R   R   R   Rf   R   R5   R%  R   RO   t   accept_suggestionR{   R   R   t   get_suggestionst	   enumerateR    R'   R  R  Rn   R  R(  R   R   R   R   R)  R   (   RD   R*  R+  R   R   Rp   R,  R   R   R{   R  R   t   iR-  R.  R  (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyR3  3  sH    


1
 N
#		
	c         C   s   t  t d | } | i i } t t |  i  d |  p
 t  n h | i d 6| d 6} |  i	 i
 d  oj y@ | i i
 d |  } t | _ | i   | i   t | d <Wq t j
 o d  } t | d <q Xn t i |  } t | d d S(	   NRf   R   R  t   checkidR!  R"  R$  s   application/json(   R   R   RE   R	  R   R   R   R   Rf   R   R5   t   qualitycheck_setR;   t   false_positiveR   R   RO   R   R   R)  R   (   RD   R*  R7  R   R   R,  R  (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   reject_qualitycheckb  s&    


	

(Y   R/   R   R<   R@   t   translate.storage.poxliffR    t   translate.langR   t   django.confR   t   django.shortcutsR   t   django.httpR   R   R   t   django.templateR   t   django.utils.translationR   R	   R   t#   django.utils.translation.trans_realR
   t   django.core.exceptionsR   R   t   django.core.cacheR   t   django.utilsR   t   django.utils.htmlR   t   django.views.decorators.cacheR   t   django.utils.encodingR   t   pootle_misc.baseurlR   t   pootle_app.models.permissionsR   R   R   t   pootle_misc.utilR   t   pootle_profile.modelsR   t   pootle_translationproject.formsR   t   pootle_statistics.modelsR   t   pootle_app.modelsR   R  t   pootle_app.project_treeR   t   pootle_store.modelsR   R   t   pootle_store.formsR   R   t$   pootle_store.templatetags.store_tagsR    t   pootle_store.utilR!   R"   R#   R$   t   pootle_store.filetypesR%   R&   t   pootle_store.signalsR'   RN   RT   RW   Rv   R   R   R   R   R   RO   R  R   R'  R3  R:  (    (    (    s0   /var/www/Pootle/local_apps/pootle_store/views.pyt   <module>   s\   "						!	&	 	?	 	#	/