<commit_before>from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

import mysite.base.decorators
import mysite.customs.forms
import mysite.customs.models

# Lists all the stored trackers of a selected type (Bugzilla, Trac etc.)
@mysite.base.decorators.view
def list_trackers(request, tracker_types_form=None):
    data = {}
    if request.POST:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm(
                request.POST, prefix='list_trackers')
        if tracker_types_form.is_valid():
            tracker_type = tracker_types_form.cleaned_data['tracker_type']
            if tracker_type == 'bugzilla':
                trackers = mysite.customs.models.BugzillaTracker.all_trackers.all()
            else:
                trackers = []
            data['tracker_type'] = tracker_type
            data['trackers'] = trackers
    notification_id = request.GET.get('notification_id', None)
    if notification_id == 'add-success':
        data['customs_notification'] = 'Bugtracker successfully added! Bugs from this tracker should start appearing within 24 hours.'
    elif notification_id == 'edit-success':
        data['customs_notification'] = 'Bugtracker successfully edited! New settings should take effect within 24 hours.'
    elif notification_id == 'delete-success':
        data['customs_notification'] = 'Bugtracker successfully deleted!'
    elif notification_id == 'tracker-existence-fail':
        data['customs_notification'] = 'Hmm, could not find the requested tracker.'
    elif notification_id == 'tracker-url-existence-fail':
        data['customs_notification'] = 'Hmm, could not find the requested tracker URL.'
    if tracker_types_form is None:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm()
    data['tracker_types_form'] = tracker_types_form
    return (request, 'customs/list_trackers.html', data)

@mysite.base.decorators.view
def add_tracker(request, tracker_type=None, tracker_form=None):
    data = {}
    if tracker_type == 'bugzilla':
        data['action_url'] = reverse('add_tracker_specific_do', args=[tracker_type])
        if tracker_form is None:
            tracker_form = mysite.customs.forms.BugzillaTrackerForm()
    else:
        # Wrong or no tracker type
        data['action_url'] = reverse('add_tracker_choose_type_do')
        if tracker_form is None:
            tracker_form = mysite.customs.forms.TrackerTypesForm()
    data['tracker_form'] = tracker_form
    return (request, 'customs/add_tracker.html', data)

def add_tracker_do(request, tracker_type=None):
    if tracker_type == 'bugzilla':
        bugzilla_tracker_form = mysite.customs.forms.BugzillaTrackerForm(
                request.POST, prefix='add_tracker')
        if bugzilla_tracker_form.is_valid():
            bugzilla_tracker_form.save()
            return HttpResponseRedirect(reverse(add_tracker_url, args=[tracker_type]) +
                                        '?project_name=' +
                                        bugzilla_tracker_form.cleaned_data['project_name'])
        else:
            return add_tracker(request,
                    tracker_type=tracker_type,
                    tracker_form=bugzilla_tracker_form)
    else:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm(
                request.POST, prefix='add_tracker')
        if tracker_types_form.is_valid():
            tracker_type = tracker_types_form.cleaned_data['tracker_type']
            return HttpResponseRedirect(reverse('add_tracker_specific', args=[tracker_type]))
        else:
            return add_tracker(request,
                    tracker_form=tracker_types_form)

@mysite.base.decorators.view
def add_tracker_url(request, tracker_type, url_form=None):
    data = {}
    project_name = request.GET.get('project_name', None)
    if tracker_type == 'bugzilla':
        if url_form is None:
            if project_name:
                try:
                    bugzilla_tracker = mysite.customs.models.BugzillaTracker.all_trackers.get(
                            project_name=project_name)
                    bugzilla_url = mysite.customs.models.BugzillaUrl(
                            bugzilla_tracker=bugzilla_tracker)
                    url_form = mysite.customs.forms.BugzillaUrlForm(
                            instance=bugzilla_url)
                except mysite.customs.models.BugzillaTracker.DoesNotExist:
                    url_form = mysite.customs.forms.BugzillaUrlForm()
            else:
                url_form = mysite.customs.forms.BugzillaUrlForm()
        data['url_form'] = url_form
    data['action_url'] = reverse(add_tracker_url_do, args=[tracker_type])
    data['finish_url'] = reverse(add_tracker_url_do, args=[tracker_type])
    if project_name:
        data['action_url'] += '?project_name=%s' % project_name
        data['finish_url'] += '?project_name=%s&finished' % project_name
    else:
        data['finish_url'] += '?finished'
    return (request, 'customs/add_tracker_url.html', data)

def add_tracker_url_do(request, tracker_type):
    url_form = None
    if tracker_type == 'bugzilla':
        url_form = mysite.customs.forms.BugzillaUrlForm(
                request.POST, prefix='add_tracker_url')
    if url_form:
        if url_form.is_valid():
            url_form.save()
            if request.GET.get('finished', None):
                return HttpResponseRedirect(reverse(list_trackers) +
                                        '?notification_id=add-success')
            else:
                return add_tracker_url(request,
                        tracker_type=tracker_type)
        else:
            return add_tracker_url(request,
                    tracker_type=tracker_type,
                    url_form=url_form)
    else:
        # Shouldn't get here. Just go back to base.
        return HttpResponseRedirect(reverse(list_trackers))

@mysite.base.decorators.view
def edit_tracker(request, tracker_type, tracker_form=None):
    data = {}
    project_name = request.GET.get('project_name', None)
    if tracker_type == 'bugzilla':
        if tracker_form is None:
            if project_name:<commit_msg>Fixed a bug caused by empty strings being logically equivalent to None.<commit_after>from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

import mysite.base.decorators
import mysite.customs.forms
import mysite.customs.models

# Lists all the stored trackers of a selected type (Bugzilla, Trac etc.)
@mysite.base.decorators.view
def list_trackers(request, tracker_types_form=None):
    data = {}
    if request.POST:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm(
                request.POST, prefix='list_trackers')
        if tracker_types_form.is_valid():
            tracker_type = tracker_types_form.cleaned_data['tracker_type']
            if tracker_type == 'bugzilla':
                trackers = mysite.customs.models.BugzillaTracker.all_trackers.all()
            else:
                trackers = []
            data['tracker_type'] = tracker_type
            data['trackers'] = trackers
    notification_id = request.GET.get('notification_id', None)
    if notification_id == 'add-success':
        data['customs_notification'] = 'Bugtracker successfully added! Bugs from this tracker should start appearing within 24 hours.'
    elif notification_id == 'edit-success':
        data['customs_notification'] = 'Bugtracker successfully edited! New settings should take effect within 24 hours.'
    elif notification_id == 'delete-success':
        data['customs_notification'] = 'Bugtracker successfully deleted!'
    elif notification_id == 'tracker-existence-fail':
        data['customs_notification'] = 'Hmm, could not find the requested tracker.'
    elif notification_id == 'tracker-url-existence-fail':
        data['customs_notification'] = 'Hmm, could not find the requested tracker URL.'
    if tracker_types_form is None:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm()
    data['tracker_types_form'] = tracker_types_form
    return (request, 'customs/list_trackers.html', data)

@mysite.base.decorators.view
def add_tracker(request, tracker_type=None, tracker_form=None):
    data = {}
    if tracker_type == 'bugzilla':
        data['action_url'] = reverse('add_tracker_specific_do', args=[tracker_type])
        if tracker_form is None:
            tracker_form = mysite.customs.forms.BugzillaTrackerForm()
    else:
        # Wrong or no tracker type
        data['action_url'] = reverse('add_tracker_choose_type_do')
        if tracker_form is None:
            tracker_form = mysite.customs.forms.TrackerTypesForm()
    data['tracker_form'] = tracker_form
    return (request, 'customs/add_tracker.html', data)

def add_tracker_do(request, tracker_type=None):
    if tracker_type == 'bugzilla':
        bugzilla_tracker_form = mysite.customs.forms.BugzillaTrackerForm(
                request.POST, prefix='add_tracker')
        if bugzilla_tracker_form.is_valid():
            bugzilla_tracker_form.save()
            return HttpResponseRedirect(reverse(add_tracker_url, args=[tracker_type]) +
                                        '?project_name=' +
                                        bugzilla_tracker_form.cleaned_data['project_name'])
        else:
            return add_tracker(request,
                    tracker_type=tracker_type,
                    tracker_form=bugzilla_tracker_form)
    else:
        tracker_types_form = mysite.customs.forms.TrackerTypesForm(
                request.POST, prefix='add_tracker')
        if tracker_types_form.is_valid():
            tracker_type = tracker_types_form.cleaned_data['tracker_type']
            return HttpResponseRedirect(reverse('add_tracker_specific', args=[tracker_type]))
        else:
            return add_tracker(request,
                    tracker_form=tracker_types_form)

@mysite.base.decorators.view
def add_tracker_url(request, tracker_type, url_form=None):
    data = {}
    project_name = request.GET.get('project_name', None)
    if tracker_type == 'bugzilla':
        if url_form is None:
            if project_name:
                try:
                    bugzilla_tracker = mysite.customs.models.BugzillaTracker.all_trackers.get(
                            project_name=project_name)
                    bugzilla_url = mysite.customs.models.BugzillaUrl(
                            bugzilla_tracker=bugzilla_tracker)
                    url_form = mysite.customs.forms.BugzillaUrlForm(
                            instance=bugzilla_url)
                except mysite.customs.models.BugzillaTracker.DoesNotExist:
                    url_form = mysite.customs.forms.BugzillaUrlForm()
            else:
                url_form = mysite.customs.forms.BugzillaUrlForm()
        data['url_form'] = url_form
    data['action_url'] = reverse(add_tracker_url_do, args=[tracker_type])
    data['finish_url'] = reverse(add_tracker_url_do, args=[tracker_type])
    if project_name:
        data['action_url'] += '?project_name=%s' % project_name
        data['finish_url'] += '?project_name=%s&finished=true' % project_name
    else:
        data['finish_url'] += '?finished=true'
    return (request, 'customs/add_tracker_url.html', data)

def add_tracker_url_do(request, tracker_type):
    url_form = None
    if tracker_type == 'bugzilla':
        url_form = mysite.customs.forms.BugzillaUrlForm(
                request.POST, prefix='add_tracker_url')
    if url_form:
        if url_form.is_valid():
            url_form.save()
            if request.GET.get('finished', None) == 'true':
                return HttpResponseRedirect(reverse(list_trackers) +
                                        '?notification_id=add-success')
            else:
                return add_tracker_url(request,
                        tracker_type=tracker_type)
        else:
            return add_tracker_url(request,
                    tracker_type=tracker_type,
                    url_form=url_form)
    else:
        # Shouldn't get here. Just go back to base.
        return HttpResponseRedirect(reverse(list_trackers))

@mysite.base.decorators.view
def edit_tracker(request, tracker_type, tracker_form=None):
    data = {}
    project_name = request.GET.get('project_name', None)
    if tracker_type == 'bugzilla':
        if tracker_form is None:
            if project_name:
{'fraction_non_alphanumeric': 0.09413183279742765, 'fraction_numerical': 0.0006430868167202572, 'mean_word_length': 2.6345311130587206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7185383', 'n_tokens_mistral': 3725, 'n_tokens_neox': 3519, 'n_words': 661}