Commits

cheater committed 69d3dba

fix: #1303 ProductVariation cannot be edited, PV and ConfigurableProduct disabled fields code is crappy + patch diff

Comments (0)

Files changed (5)

satchmo/apps/product/modules/configurable/admin.py

 from product.modules.configurable.models import ConfigurableProduct, ProductVariation
 
 class ProductVariationOptions(admin.ModelAdmin):
+    '''
+    Makes the fields product and parent read-only.
+
+    Letting the user change the parent on an existing ProductVariation
+    will just confuse things, and probably never makes sense.
+
+    If the product_id has been assigned externally (usually by a link
+    from said product), then we want it to not be user changeable so it
+    doesn't get changed by mistake.
+    '''
+    readonly_fields = ['product', 'parent',]
+    fields = ['product', 'parent', 'options',]
     filter_horizontal = ('options',)
 
-admin.site.register(ConfigurableProduct)
+class ConfigurableProductAdmin(admin.ModelAdmin):
+    '''
+    Makes the field product read-only.
+
+    If the product_id has been assigned externally (usually by a link
+    from said product), then we want it to not be user changeable so it
+    doesn't get changed by mistake.
+    '''
+    readonly_fields = ['product',]
+    fields = ['product', 'create_subs', 'option_group',]
+
+admin.site.register(ConfigurableProduct, ConfigurableProductAdmin)
 admin.site.register(ProductVariation, ProductVariationOptions)
 
 

satchmo/apps/product/templates/admin/configurable/configurableproduct/change_form.html

 
 {% block extrahead %}
 {{ block.super }}
-{% comment %}
-jquery script should've already been added in admin/product/change_form.html, so don't add it again here
-{% endcomment %}
 {% endblock %}
 
 {% block after_related_objects %}

satchmo/apps/product/templates/admin/configurable/productvariation/change_form.html

 {% load i18n %}
 {% block extrahead %}
 {{block.super}}
-{% comment %}
-jquery script should've already been added in admin/base_site.html, so don't add it again here
-{% endcomment %}
-<script type="text/javascript">
-function setOptions(data){
-    options_tag = $("select#id_options");
-    options_tag.html(data);
-    // at this point all the options end-up being selected,
-
-    //So, unselect them all
-    for (opt_tag in options_tag.children()){
-        opt_tag = options_tag.children()[opt_tag];
-        opt_tag.selected = false;
-    }
-    //Then if any were specified as selected, reselect them
-    selected_options = new Array({{form.data.options|join:","}});
-    for (sel_opt in selected_options){
-        sel_opt = selected_options[sel_opt];
-        for (opt_tag in options_tag.children()){
-            opt_tag = options_tag.children()[opt_tag];
-            if (opt_tag.value == sel_opt){
-                opt_tag.selected = true;
-            }
-        }
-    }
-}
-function getOptions(){
-    parent_tag = $("*#id_parent");
-    parent_id = parent_tag.attr("value");
-	if (parent_id) {
-		var url = "{% url satchmo_admin_configurableproduct 999999999 %}";
-		url = url.replace('/999999999/', '/' + parent_id + '/');
-	    //$.get("../../configurableproduct/" + parent_id + "/getoptions/", setOptions );
-	    $.get(url, setOptions );
-	}
-}
-
-$(document).ready(function(){
-    parent_tag = $("*#id_parent");
-    parent_tag.change(getOptions);
-
-    parent_value = parent_tag.attr("value");
-    if (parent_value){
-        getOptions();
-    }
-
-{% if original %}
-    //letting the user change the parent on an existing ProductVariation
-    //will just confuse things, and probably never makes sense.
-    $("*#id_parent").attr("disabled", "true");
-{% endif %}
-
-});
-
-</script>
 {% endblock %}
 
 {% block after_related_objects %}

satchmo/apps/product/templates/admin/product/change_form.html

 {% load satchmo_product_admin %}
 
 {% block extrahead %}{{ block.super }}
-    <script type="text/javascript">
-{% comment %}
-if the product_id has been assigned externally (usually by a link from said product), then we want it to not be user changeable so it doesn't get changed by mistake.
-{% endcomment %}
-$(document).ready(function(){
-    {%js_make_select_readonly '$("select#id_product")'%}
-    $("a#add_id_product").remove();
-    
-});
-    </script>
 {% endblock %}

satchmo/apps/product/templatetags/satchmo_product_admin.py

 
 register = template.Library()
 
-def js_make_select_readonly(select):
-    #This is really just a mini-template
-    #select must be a jquery object
-    return """
-    select = %(select)s;
-    value = select.attr("value");
-    if (value){
-        text = "";
-        for (c in select.children()){
-          if (select.children()[c].value == value){
-            text = select.children()[c].innerHTML;
-            break;
-          }
-        }
-        select.before("<strong>" + text + "</strong><input type=\\"hidden\\" name=\\"" + select.attr("name") + "\\" value=\\"" + value + "\\" \>");
-        select.remove();
-    }
-    """ % {'select':select}
-
-register.simple_tag(js_make_select_readonly)
-
 def edit_subtypes(product):
     output = '<ul>'
     subtypes = product.get_subtypes()