
    JAh.                         d dl mZ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mZ d dlmZmZ d d	lmZ d d
lZd dlmZ ddZddZd Zd Zd Zed        Zed        Zy
)    )renderget_object_or_404   )PropertyPropertyGallery)category)Location)staff_member_required)CountAvgQSum)now	timedelta)JsonResponseN)sitec                    d }t         j                  j                  dd      }|r#t        t        |      }|j                  |      }t        j                  j                  dd      j                         }|j                         }t        |j                  dd            }|rt        |      nd }|rt        |      nd }|||||d	}	t        | d
|	      S )NsaleTpurposeis_availablesluglocationproperty__purposeproperty__is_availablepriceflat
properties	locationsproperty_countprecio_maximoprecio_minimozagency/comprar.htmlr   objectsfilterr   r	   distinctcountlistvalues_listmaxminr   
requestlocation_slugr   r#   r$   r%   preciosr&   r'   contexts
             ;C:\Users\User\Documents\GitHub\espacioelite\agency\views.pycomprarr7      s    H!!((d(KJ$XMB&&&9
   '' # (  hj 
  %%'N :))')=>G$+CLM$+CLM !(&&G '0'::    c                    d }t         j                  j                  dd      }|r#t        t        |      }|j                  |      }t        j                  j                  dd      j                         }|j                         }t        |j                  dd            }|rt        |      nd }|rt        |      nd }|||||d	}	t        | d
|	      S )NrentTr   r   r   r   r   r    r"   zagency/rentar.htmlr(   r1   s
             r6   rentarr;   *   s    H!!((d(KJ$XMB&&&9
   '' # (  hj 
  %%'N :))')=>G$+CLM$+CLM !(&&G '/99r8   c                 r   	 t         j                  j                  ||      }t        j                  j                  |j                        }t        t        |j                              }d|j                  j                  dd      z   dz   |z   dz   }|||d}t        | d	|      S # t        $ r}|d }~ww xY w)
N)category__slugr   )property_id' z%20z'%20por%20valor%20de%20$%20u4   %20COP,%20por%20favor%20darme%20más%20información.)single_propertyproperty_gallerymensajezagency/property_detail.html)r   r)   get	Exceptionr   r*   idint_a_moneda_strintr   property_namereplacer   )	r2   category_slugproperty_slugrA   erB   preciorC   r5   s	            r6   property_detailrO   H   s     ((,,M},]
 '..55/BTBT5UC 5 567F--55c5AAB__`ff  h^  ^G)+G
 '7@@  s   !B& &	B6/B11B6c                     t        | d      S )Nzagency/inicio.html)r   )r2   s    r6   iniciorQ   Y   s    './/r8   c                     t        |       }g }|r|j                  |dd         |d d }|rdj                  t        |            }|S )N.)strappendjoinreversed)numero
numero_strgruposnumero_formateados       r6   rG   rG   \   sS    VJ F
jo&_
 
 &!12r8   c                    t         j                  j                         }t         j                  j                  d      j                         }t         j                  j                  d      j                         }t         j                  j                  d      j                         }t         j                  j                  d      j                         }t         j                  j                  d      j                         }t         j                  j	                  t        d            d	   xs d
}t         j                  j	                  t        d            d   xs d
}t         j                  j	                  t        d            d   xs d
}	t         j                  j	                  t        d            d   xs d
}
t        j                  j                  t        d      t        dt        d            t        d            j                  d      }t        j                  j                  t        d      t        dt        d            t        d            j                  d      }t               t        d      z
  }t         j                  j                  |      j                         }t         j                  j                  |      j                         }i }i }i }i }t         j                  D ]e  \  }}t         j                  j                  t        d| d      t        d| d      z        j                         }|d
kD  sX||t        |      <   g i }t         j                   D ]e  \  }}t         j                  j                  t        d| d      t        d| d      z        j                         }|d
kD  sX||t        |      <   g i }t         j"                  D ]e  \  }}t         j                  j                  t        d| d      t        d| d      z        j                         }|d
kD  sX||t        |      <   g t         j                  j                  d       j                         }t         j                  j                  d!       j                         }t         j                  j                  d"#      j                         t         j                  j                  d"d$%      j                         t         j                  j                  d$d&%      j                         t         j                  j                  d&d'%      j                         t         j                  j                  d'(      j                         d)}t         j                  j                  d*+      j                         t         j                  j                  d*d,-      j                         t         j                  j                  d,d.-      j                         t         j                  j                  d.d/-      j                         t         j                  j                  d/0      j                         d1}t$        j                  j                         }t         j                  j                  d!2      j'                         j                         }t)        j*                  |       } i d3|d4|d5|d6|d7|d8|d9t-        |d
      d:t-        |d;      d<t-        |	d;      d=t-        |
d;      d>|d?|d@|dA|dB|dC|dD|||||||t/        j0                  |D !cg c]  }!|!j2                  |!j4                  dE c}!      t/        j0                  |dFdG D "cg c]  }"|"j6                  |"j4                  dE c}"      t/        j0                  dH|dEdI|dEg      t/        j0                  |j9                         D #$cg c]  \  }#}$|$d
kD  r|#|$dJ c}$}#      dK
}%|%j;                  |        t=        | dL|%      S c c}!w c c}"w c c}$}#w )Mu.   Vista principal del dashboard de estadísticasT)r   )is_featured)is_verifiedr:   )r   r   r   
price__avgr   area	area__avgbedroomsbedrooms__avg	bathroomsbathrooms__avgproperty)r   )r*   property__price)total_propertiesavailable_properties	avg_pricez-total_properties   dayscreated_date__gte)modified_date__gte")features__icontainsr?   )services__icontains)rules__icontains)is_furnishedFi  )	price__lti@B )
price__gterw   i i@KL )rx   )zMenos de $500,000z$500,000 - $1,000,000z$1,000,000 - $2,000,000z$2,000,000 - $5,000,000u   Más de $5,000,0002   )area__ltd   )	area__gterz      i,  )r|   )u   Menos de 50 m²u
   50-100 m²u   100-200 m²u   200-300 m²u   Más de 300 m²)images__isnullri   rj   featured_propertiesverified_propertiesrent_propertiessale_propertiesrk   avg_arear   avg_bedroomsavg_bathroomsproperties_by_categoryproperties_by_locationrecent_propertiesrecent_modificationsfeatures_statsservices_statsrules_stats)namer,   N
   VentaRenta)ranger,   )
furnished_countunfurnished_countprice_rangesarea_rangestotal_imagesproperties_with_imagescategories_datalocations_datapurpose_dataprice_ranges_datazadmin/dashboard_stats.html)r   r)   r,   r*   	aggregater   r   annotater   r   order_byr	   r   r   FEATURESrU   SERVICESRULESr   r+   r   each_contextroundjsondumpscategory_nameri   location_nameitemsupdater   )&r2   ri   rj   r   r   r   r   rk   r   r   r   r   r   thirty_days_agor   r   r   r   r   feature_codefeature_namer,   service_codeservice_name	rule_code	rule_namer   r   r   r   r   r   admin_contextcatlockvr5   s&                                         r6   dashboard_statsr   k   s   
  ''--/#++222EKKM"**11d1CIIK"**11d1CIIK &&--f-=CCEO&&--f-=CCEO   **3w<8FK!I))#f+6{CHqH##--c*o>OTSTL$$..s;/?@AQRWVWM &--66z*":at6TU'( 7  h"#	  &--66z*":at6TU'( 7  h"#	  eiR00O ((////RXXZ#++22o2V\\^ NNK N&.&7&7"l  ''Al^1"56Al^1"567
 %' 	 1905N3|,- '8 N&.&7&7"l  ''Al^1"56Al^1"567
 %' 	 1905N3|,- '8 K (	9  ''9+Q/09+Q/01
 %' 	 19*/KI' !/ &&--4-@FFHO ((//U/CIIK &--44v4FLLN!)!1!1!8!8FV]!8!^!d!d!f#+#3#3#:#:gY`#:#a#g#g#i#+#3#3#:#:gY`#:#a#g#g#i&..555IOOQL $++22B2?EEG&&--S-IOOQ''..s.KQQS''..s.KQQS#++22S2AGGIK #**002L%--44E4JSSU[[]%%g.M<,< 	 4< 	2	<
 	2< 	?< 	?< 	U9a(< 	E(A&< 	lA.< 	}a0<  	!"8!<" 	!"8#<( 	.)<* 	 4+<0 	.1<2 	.3<4 	{5<: +. %" %"8  :: ,'- ,c %%))(
 ,'- .
 ** ,CR0&2 1c %%))'
 1&2 3
 

77$
 
 "ZZ #((*)5 +daa!e *
 +)5 6q<G| NN=!'7AA/'-
&2)5s   & a" aac           
         | j                   j                  dd      }|dk(  rddlm} t        j
                  j                  t               t        d      z
        j                   |d	      
      j                  d      j                  t        d            j                  d      }t        dt        |      i      S |dk(  rt        j
                  j                  t!        d      t        d            j                  d      j                  d      dd }t        |D cg c]  }|j"                   c}|D cg c]  }t%        |j&                  xs d       c}d      S t        ddi      S c c}w c c}w )u&   API para datos de gráficos dinámicostype monthly_propertiesr   )
TruncMonthim  rm   ro   created_date)monthr   rF   )r,   dataprice_by_locationrh   rg   )rk   r,   )	count__gtz
-avg_priceNr   )labelspriceserrorzInvalid chart type)GETrD   django.db.models.functionsr   r   r)   r*   r   r   r   valuesr   r   r   r-   r	   r   r   floatrk   )r2   
chart_typer   monthly_datalocation_pricesr   s         r6   api_chart_datar     sm    ,J))9''..!eiS&99 / 

(^,  

&/((+ # 

(7
 	 D&
  	 
*	*"**33+,
# 4 
 &1&
hh|4Sb:
 4CDOSs((OD<KLOSuS]]/a0OL
  	
 "6788	 ELs   #E2< E7
)N) django.shortcutsr   r   modelsr   r   category.modelsr   location.modelsr	   %django.contrib.admin.views.decoratorsr
   django.db.modelsr   r   r   r   django.utils.timezoner   r   django.httpr   r   django.contrib.adminr   r7   r;   rO   rQ   rG   r   r    r8   r6   <module>r      sm    6 - $ $ G / / 0 $  %;<:<A"0 eB eBN 9 9r8   