Wednesday, November 17, 2010

Django deployment problem

Actually, there are several problems:

  1. Upload to the server - usually solved via SCM like git, mercurial, subversion etc – Solved
  2. Copy application files to proper destinations, reboot application - bash/fabric etc. – Solved
  3. Copy user-related media files – usually solved via ignore files, symlinks etc. but there is NO STANDARD or SOLUTION - Unsolved
  4. Migrate database, maybe it's possible to solve it via some migration tool like South but actually I don't know and at this point it's still - Unsolved
  5. Fallback to previous version (=any version?) – unsolved and almost impossible to keep data in actual state trough migrations - Unsolved 

So, what I think about it.


Database Migrations
I guess it's possible to solve via South. Another way is to write my own migrations with specific format - SQL with bash/python etc. I will dedicate some time to learn more about migration.

Copy user-related media files
I saw something about it in latest version of django. But sometimes I have to work with old releases (1.1.1) of django I need to keep it independent from django - I'll create configuration file which will consist from list of directories where User's data will be stored. Then some playing with symlinks and it will be solved.

And, at least
Fallback to previous version
There is no rocket science to change symlinks. But I still can't decide what I can do with database at this point.

How to handle variations with Javascript within Satchmo's product template

My goal was to list a set of variations on product page and redirect user to related product page if variation was changed. For example we have two variations:

  1. Size
  2. Colour
And values of variatons:
  1. Colour: red, black, white
  2. Size: M (middle), L (large), S (small)
So, first of all I display this variations on product page and when user change something (colour or size) then redirect to product with selected colour and size respectively.

1. Show dropdowns with variations:
{% for option_group in options %}

{% endfor %}

2. Store all possible variations for javascript to find related product if some of variations was changed on the page:
// list of available variations of current product
    var variations = [
    {% for product_variation in product.configurableproduct.productvariation_set.all %}{
        'url': '{{ product_variation.product.get_absolute_url }}',
        {% for option in product_variation.options.all %}
        'group-{{ option.option_group.id }}': '{{ option.value }}'{% if not forloop.last %},{% endif %}
        {% endfor %}
    }{% if not forloop.last %},{% endif %}{% endfor %}];

3. Add JavaScript code which will handle variations changes:
  /**
   * Select & go to variation which have been selected via select box.
   * This functionality require a set of available variations of the product
   * which consost from something like that: [
   * {'url': 'url_to_product', 'group-': '',..}
   * ]
   */
  var any = function(arr){
       for(var i = 0; i < arr.length; i++){
           if(arr[i] != true){
               return false;
           }
       }
       return true;
  }
  $('select.priced').bind('change', function(){
      var groups = {};
      $('select.priced').each(function(){
          if($(':selected', $(this))){
              groups[$(this).attr('id')] = $(this).val();
          }
      });

      $(variations).each(function(i, vr){
          var flags = [];
          
          for(var grp in groups){
              if(vr[grp] == groups[grp]){
                  flags.push(true)
              }else{
                  flags.push(false);
              }
          }
          
          if(any(flags)){
              location = vr['url'];
          }
      });
  });