<s> import json <EOL> from django . contrib . auth . decorators import permission_required <EOL> from django . core . paginator import Paginator , EmptyPage <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Q <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . decorators . http import require_POST <EOL> from lfs . caching import invalidate_cache_group_id <EOL> import lfs . core . utils <EOL> from lfs . core . utils import LazyEncoder <EOL> from lfs . core . signals import property_type_changed <EOL> from lfs . catalog . models import Property <EOL> from lfs . catalog . models import PropertyOption <EOL> from lfs . catalog . models import FilterStep <EOL> from lfs . manage . property . forms import PropertyAddForm <EOL> from lfs . manage . property . forms import PropertyDataForm <EOL> from lfs . manage . property . forms import PropertyTypeForm <EOL> from lfs . manage . property . forms import StepTypeForm <EOL> from lfs . manage . property . forms import SelectFieldForm <EOL> from lfs . manage . property . forms import NumberFieldForm <EOL> from lfs . manage . property . forms import StepRangeForm <EOL> from lfs . manage . utils import get_current_page <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_properties ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> prop = Property . objects . filter ( local = False ) [ <NUM_LIT:0> ] <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : prop . pk } ) <EOL> except IndexError : <EOL> url = reverse ( "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def pages_inline ( request , page , paginator , property_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : page , <EOL> "<STR_LIT>" : paginator , <EOL> "<STR_LIT>" : property_id , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_property ( request , id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> prop = get_object_or_404 ( Property , pk = id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = PropertyDataForm ( instance = prop , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> _update_property_positions ( ) <EOL> return lfs . core . utils . set_message_cookie ( <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : prop . id } ) , <EOL> msg = _ ( u"<STR_LIT>" ) , <EOL> ) <EOL> else : <EOL> form = PropertyDataForm ( instance = prop ) <EOL> display_step_form = prop . is_number_field and prop . filterable <EOL> properties = _get_filtered_properties_for_property_view ( request ) <EOL> paginator = Paginator ( properties , <NUM_LIT> ) <EOL> page = get_current_page ( request , properties , prop , <NUM_LIT> ) <EOL> try : <EOL> page = paginator . page ( page ) <EOL> except EmptyPage : <EOL> page = paginator . page ( <NUM_LIT:1> ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : prop , <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : PropertyTypeForm ( instance = prop ) , <EOL> "<STR_LIT>" : int ( id ) , <EOL> "<STR_LIT>" : options_inline ( request , id ) , <EOL> "<STR_LIT>" : steps_inline ( request , id ) , <EOL> "<STR_LIT>" : number_field ( request , prop ) , <EOL> "<STR_LIT>" : select_field ( request , prop ) , <EOL> "<STR_LIT>" : display_step_form , <EOL> "<STR_LIT>" : selectable_properties_inline ( request , page , paginator , id ) , <EOL> "<STR_LIT>" : properties , <EOL> "<STR_LIT>" : pages_inline ( request , page , paginator , id ) , <EOL> "<STR_LIT>" : request . session . get ( "<STR_LIT>" , { } ) . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> } ) ) <EOL> def _get_filtered_properties_for_property_view ( request ) : <EOL> """<STR_LIT>""" <EOL> properties = Property . objects . filter ( local = False ) <EOL> property_ordering = request . session . get ( "<STR_LIT>" , "<STR_LIT:name>" ) <EOL> property_ordering_order = request . session . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> property_filters = request . session . get ( "<STR_LIT>" , { } ) <EOL> name = property_filters . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if name != "<STR_LIT>" : <EOL> properties = properties . filter ( Q ( name__icontains = name ) | Q ( title__icontains = name ) ) <EOL> properties = properties . order_by ( "<STR_LIT>" % ( property_ordering_order , property_ordering ) ) <EOL> return properties <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def selectable_properties_inline ( request , page , paginator , property_id , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> prop = Property . objects . get ( pk = property_id ) <EOL> except Property . DoesNotExist : <EOL> return "<STR_LIT>" <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : paginator , <EOL> "<STR_LIT>" : page , <EOL> "<STR_LIT>" : prop <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def set_name_filter ( request ) : <EOL> """<STR_LIT>""" <EOL> req = request . POST if request . method == '<STR_LIT:POST>' else request . GET <EOL> property_filters = request . session . get ( "<STR_LIT>" , { } ) <EOL> if request . POST . get ( "<STR_LIT:name>" , "<STR_LIT>" ) != "<STR_LIT>" : <EOL> property_filters [ "<STR_LIT>" ] = request . POST . get ( "<STR_LIT:name>" ) <EOL> else : <EOL> if property_filters . get ( "<STR_LIT>" ) : <EOL> del property_filters [ "<STR_LIT>" ] <EOL> request . session [ "<STR_LIT>" ] = property_filters <EOL> properties = _get_filtered_properties_for_property_view ( request ) <EOL> paginator = Paginator ( properties , <NUM_LIT> ) <EOL> page = paginator . page ( req . get ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> property_id = req . get ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> html = ( <EOL> ( "<STR_LIT>" , selectable_properties_inline ( request , page , paginator , property_id ) ) , <EOL> ( "<STR_LIT>" , pages_inline ( request , page , paginator , property_id ) ) , <EOL> ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def set_properties_page ( request ) : <EOL> """<STR_LIT>""" <EOL> property_id = request . GET . get ( "<STR_LIT>" ) <EOL> properties = _get_filtered_properties_for_property_view ( request ) <EOL> amount = <NUM_LIT> <EOL> paginator = Paginator ( properties , amount ) <EOL> page = paginator . page ( ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> html = ( <EOL> ( "<STR_LIT>" , pages_inline ( request , page , paginator , property_id ) ) , <EOL> ( "<STR_LIT>" , selectable_properties_inline ( request , page , paginator , property_id ) ) , <EOL> ) <EOL> return HttpResponse ( <EOL> json . dumps ( { "<STR_LIT:html>" : html } , cls = LazyEncoder ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def no_properties ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return render_to_response ( template_name , RequestContext ( request , { } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def update_property_type ( request , id ) : <EOL> """<STR_LIT>""" <EOL> prop = get_object_or_404 ( Property , pk = id ) <EOL> old_type = prop . type <EOL> form = PropertyTypeForm ( instance = prop , data = request . POST ) <EOL> new_property = form . save ( ) <EOL> if old_type != new_property . type : <EOL> property_type_changed . send ( prop ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> return lfs . core . utils . set_message_cookie ( <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : prop . id } ) , <EOL> msg = _ ( u"<STR_LIT>" ) , <EOL> ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def select_field ( request , property , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> form = SelectFieldForm ( instance = property ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : property , <EOL> "<STR_LIT>" : form , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def save_select_field ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> form = SelectFieldForm ( instance = property , data = request . POST ) <EOL> property = form . save ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( request . META . get ( "<STR_LIT>" ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def number_field ( request , property , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> number_field_form = NumberFieldForm ( instance = property ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : property , <EOL> "<STR_LIT>" : number_field_form , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def save_number_field_validators ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> form = NumberFieldForm ( instance = property , data = request . POST ) <EOL> property = form . save ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> response = HttpResponseRedirect ( request . META . get ( "<STR_LIT>" ) ) <EOL> return lfs . core . utils . set_message_to ( response , _ ( u"<STR_LIT>" ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def steps_inline ( request , property_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> step_form = StepRangeForm ( instance = property ) <EOL> step_type_form = StepTypeForm ( instance = property ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : property , <EOL> "<STR_LIT>" : step_form , <EOL> "<STR_LIT>" : step_type_form , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def save_step_range ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> form = StepRangeForm ( instance = property , data = request . POST ) <EOL> property = form . save ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def save_step_type ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> form = StepTypeForm ( instance = property , data = request . POST ) <EOL> property = form . save ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> html = [ [ "<STR_LIT>" , steps_inline ( request , property_id ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def add_step ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> if request . POST . get ( "<STR_LIT:action>" ) == "<STR_LIT>" : <EOL> start = request . POST . get ( "<STR_LIT:start>" , "<STR_LIT>" ) <EOL> if start != "<STR_LIT>" : <EOL> FilterStep . objects . create ( start = start , property_id = property_id ) <EOL> message = _ ( u"<STR_LIT>" ) <EOL> else : <EOL> for step_id in request . POST . getlist ( "<STR_LIT>" ) : <EOL> try : <EOL> step = FilterStep . objects . get ( pk = step_id ) <EOL> except FilterStep . DoesNotExist : <EOL> pass <EOL> else : <EOL> step . start = request . POST . get ( "<STR_LIT>" % step_id , "<STR_LIT>" ) <EOL> step . save ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> message = _ ( u"<STR_LIT>" ) <EOL> html = [ [ "<STR_LIT>" , steps_inline ( request , property_id ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : message , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def delete_step ( request , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> step = FilterStep . objects . get ( pk = id ) <EOL> except FilterStep . DoesNotExist : <EOL> url = request . META . get ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> prop = step . property <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : prop . id } ) <EOL> step . delete ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> response = HttpResponseRedirect ( url ) <EOL> return lfs . core . utils . set_message_to ( response , _ ( u"<STR_LIT>" ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def options_inline ( request , property_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : property , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def add_property ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = PropertyAddForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> property = form . save ( ) <EOL> property . position = <NUM_LIT:1000> <EOL> property . title = property . name <EOL> property . save ( ) <EOL> _update_property_positions ( ) <EOL> return lfs . core . utils . set_message_cookie ( <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : property . id } ) , <EOL> msg = _ ( u"<STR_LIT>" ) , <EOL> ) <EOL> else : <EOL> form = PropertyAddForm ( ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : Property . objects . filter ( local = False ) , <EOL> "<STR_LIT>" : ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( "<STR_LIT>" , <EOL> reverse ( "<STR_LIT>" ) ) , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def delete_property ( request , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> property = Property . objects . get ( pk = id ) <EOL> except Property . DoesNotExist : <EOL> url = request . META . get ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> property . delete ( ) <EOL> _update_property_positions ( ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> url = reverse ( "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def add_option ( request , property_id ) : <EOL> """<STR_LIT>""" <EOL> property = get_object_or_404 ( Property , pk = property_id ) <EOL> if request . POST . get ( "<STR_LIT:action>" ) == "<STR_LIT>" : <EOL> name = request . POST . get ( "<STR_LIT:name>" , "<STR_LIT>" ) <EOL> price = request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> price = float ( price ) <EOL> except ValueError : <EOL> price = <NUM_LIT:0.0> <EOL> if name != "<STR_LIT>" : <EOL> option = PropertyOption . objects . create ( name = name , price = price , property_id = property_id ) <EOL> message = _ ( u"<STR_LIT>" ) <EOL> else : <EOL> message = _ ( u"<STR_LIT>" ) <EOL> else : <EOL> for option_id in request . POST . getlist ( "<STR_LIT>" ) : <EOL> try : <EOL> option = PropertyOption . objects . get ( pk = option_id ) <EOL> except PropertyOption . DoesNotExist : <EOL> pass <EOL> else : <EOL> try : <EOL> price = float ( request . POST . get ( "<STR_LIT>" % option_id , "<STR_LIT>" ) ) <EOL> except ValueError : <EOL> price = <NUM_LIT:0.0> <EOL> try : <EOL> position = int ( request . POST . get ( "<STR_LIT>" % option_id , <NUM_LIT> ) ) <EOL> except ValueError : <EOL> position = <NUM_LIT> <EOL> option . position = position <EOL> option . name = request . POST . get ( "<STR_LIT>" % option_id , "<STR_LIT>" ) <EOL> option . price = price <EOL> option . save ( ) <EOL> message = _ ( u"<STR_LIT>" ) <EOL> _update_positions ( property ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> html = [ [ "<STR_LIT>" , options_inline ( request , property_id ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : message <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def delete_option ( request , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> option = PropertyOption . objects . get ( pk = id ) <EOL> except option . DoesNotExist : <EOL> url = request . META . get ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> property = option . property <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:id>" : property . id } ) <EOL> option . delete ( ) <EOL> _update_positions ( property ) <EOL> invalidate_cache_group_id ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( url ) <EOL> def _update_property_positions ( ) : <EOL> """<STR_LIT>""" <EOL> for i , property in enumerate ( Property . objects . exclude ( local = True ) ) : <EOL> property . position = ( i + <NUM_LIT:1> ) * <NUM_LIT:10> <EOL> property . save ( ) <EOL> def _update_positions ( property ) : <EOL> """<STR_LIT>""" <EOL> for i , option in enumerate ( property . options . all ( ) ) : <EOL> option . position = ( i + <NUM_LIT:1> ) * <NUM_LIT:10> <EOL> option . save ( ) </s>
