Commits

Diego Búrigo Zacarão committed d0a9483

Made branch required only for some VCSs

- Mercurial, Bazaar and Subversion do not require branch
anymore.
- Git and CVS still require it.
- Fixed VCS form validation (inside the Component form). It also
prevents to add branch for VCSs that do not accept it
- Added JQuery toggle for show/hide the branch field as needed

  • Participants
  • Parent commits ee7a6ca

Comments (0)

Files changed (5)

transifex/projects/views.py

         'allow_subform': allow_subform,
         'project' : project,
         'component': component,
+        'branch_support': settings.BRANCH_SUPPORT,
     }, context_instance=RequestContext(request))
 
 

transifex/settings/60-vcs.conf

                        }
 SUBMISSION_CHOICES = {'hg': SUBMISSION_VCS_TYPES,}
 
+# This setting identifies if the CODEBASES support branches or not
+BRANCH_SUPPORT = {'hg': False}
+
 # Default submit message format for centralized VCSs. Backends can override
 # this.
 CVCS_SUBMIT_MSG = """%(date)s  %(userinfo)s

transifex/settings/80-vcs-extras.conf

                            'cvs': SUBMISSION_VCS_TYPES,
                            'git': SUBMISSION_VCS_TYPES,
                            'svn': SUBMISSION_VCS_TYPES,})
+
+BRANCH_SUPPORT.update({'bzr': False,
+                       'cvs': True,
+                       'git': True,
+                       'svn': False})

transifex/templates/projects/component_form.html

 {% endfor %}{% endfor %}
 prefaces = new Array({% for subform in unit_subforms %}"sub_{{ subform.id }}"{% if not forloop.last %}, {% endif %}{% endfor %});
 
+// Create a array based on settings.BRANCH_SUPPORT
+branch_support = new Object();
+{% for entry, value in branch_support.items %}
+branch_support["{{ entry }}"] = {{value|lower}};
+{% endfor %}
+
 function on_change_type(obj)
 {
     on = triggers[obj.attr("value")];
         $("[id^='" + off + "']").hide();
     }
     $("[id^='" + on + "']").show();
+
+    // Show/hide the branch field based on the settings.BRANCH_SUPPORT
+    if (branch_support[obj.attr("value")]){
+        $("input[id='branch']").parents('tr:eq(0)').show();
+    }else{
+        $("input[id='branch']").parents('tr:eq(0)').hide();
+        $("input[id='branch']").attr('value','');
+    }
 }
 
 function toggle_submission_status() {

transifex/vcs/forms.py

+from django.conf import settings
 from django import forms
 from django.utils.translation import ugettext_lazy as _
 from django.forms.util import ErrorList
         exclude = ('name',)
 
 class VcsUnitSubForm(forms.ModelForm):
+    type = forms.CharField(widget=forms.HiddenInput, required=False)
+    
+    def __init__(self, *args, **kwargs):
+        codebase_type = None
+        instance = kwargs.get('instance', None)
+        # If editing an existent codebase
+        if instance:
+            codebase_type = instance.type
+        else:
+            # If a request for saving a new codebase
+            try:
+                codebase_type = args[0]['unit-type']
+            except TypeError:
+                pass
+        super(VcsUnitSubForm, self).__init__(*args, **kwargs)
+        # Check it the codebase_type has branch support
+        if codebase_type and codebase_type in settings.BRANCH_SUPPORT \
+            and not settings.BRANCH_SUPPORT[codebase_type]:
+            self.fields['branch'].required = False
+        # Set the attr id of the branch field
+        self.fields['branch'].widget.attrs['id']='branch' 
+
+
     class Meta:
         model = VcsUnit
         exclude = tuple(
             field.name 
             for model in VcsUnit.__bases__
-            for field in inclusive_fields(model, except_fields=['root'])
+            for field in inclusive_fields(model, except_fields=['root', 'type'])
         )
 
-    # TODO: Make validation flexible for VCSs that does not need a branch
-    # We are validating it here (in the Form) because we will need to handle
-    # more then one field to make the TODO above.
     def clean(self):
         cleaned_data = self.cleaned_data
         branch = cleaned_data.get("branch")
-        if not branch:
-            msg = _(u"This type of repository needs a branch")
-            self._errors["branch"] = ErrorList([msg])
+        codebase_type = cleaned_data.get("type")
+        if branch and not self.fields['branch'].required \
+            and not settings.BRANCH_SUPPORT[codebase_type]:
+            msg = _(u"This type of repository does not accept branch")
+            raise forms.ValidationError(msg)
         return cleaned_data