예제 #1
0
def approve(request, id):
    config = get_object_or_404(Config, pk=id)
    if config.locked:
        error = ("This configuration is locked. Only admins can unlock "
                 "it.")
        return details(request, config.id, error=error)
    old_status = config.status
    message = ''
    if request.method == 'POST':  # If the form has been submitted...
        data = request.POST
        if data.get('approved', False):
            # check if domains and domains requests are null
            if not config.domains.all() and not config.domainrequests.all():
                error = """Can't approve this configuration. There is no
                        correlated domain."""
                return details(request, id, error=error)
            # check if domain names already exist
            for domain in config.domainrequests.all():
                if Domain.objects.filter(name=domain).exclude(
                        Q(config__status='deleted') |
                        Q(config__status='invalid')):
                    error = """Can't approve this configuration. Domain is
                            already used by another approved configuration."""
                    return details(request, id, error=error)
            config.status = 'approved'
            for domain in config.domainrequests.all():
                exists = Domain.objects.filter(name=domain)
                if exists:
                    claimed = exists[0]
                    claimed.config = config
                else:
                    claimed = Domain(name=domain.name,
                                     config=config)
                claimed.save()
                domain.delete()
        elif data.get('denied', False):
            # Check mandatory comment when invalidating
            if data['comment'] == 'Other - invalid':
                if not data['commenttext']:
                    error = "Enter a comment."
                    return details(request, id, error=error)
                message = data['commenttext']
            else:
                message = data['comment']
            config.status = 'invalid'
        else:
            raise ValueError("shouldn't get here")
        config.save()
        comment = Comment(user_name='ISPDB System',
                          site_id=settings.SITE_ID)
        c = "<ul><li><b>Status</b> changed from <b><i>%s</i></b> to \
             <b><i>%s</i></b> by %s</li></ul>\n %s" % (old_status,
            config.status, request.user.email, message)
        comment.comment = c
        comment.content_type = ContentType.objects.get_for_model(Config)
        comment.object_pk = config.pk
        comment.save()

    return HttpResponseRedirect('/details/' + id)  # Redirect after POST
예제 #2
0
파일: views.py 프로젝트: bwinton/ispdb
def add(request, domain=None):
    DomainFormSet = formset_factory(DomainForm, extra=0, max_num=10)
    InlineFormSet = inlineformset_factory(Config, Domain, can_delete=False)

    #from nose.tools import set_trace;set_trace()
    if request.method == 'POST': # If the form has been submitted...
        # is this a domain request, or a full configuration?
        data = request.POST
        domains = []
        num_domains = int(data['form-TOTAL_FORMS'])
        for i in range(num_domains):
            domains.append(data['form-' + unicode(i) + '-name'])
        # did the user fill in a full form, or are they just asking for some
        # domains to be registered
        if data['asking_or_adding'] == 'asking':
            # we'll create (unclaimed) domains if they don't exist, otherwise
            # register the vote
            for domain in domains:
                exists = Domain.objects.filter(name=domain) or \
                         UnclaimedDomain.objects.filter(name=domain)
                if exists:
                    d = exists[0]
                    d.votes += 1
                else:
                    d = UnclaimedDomain(name=domain,
                                        status='requested',
                                        votes=1)
                d.save()
            return HttpResponseRedirect('/') # Redirect after POST
        else:
            config = Config()
            # A form bound to the POST data
            config_form = ConfigForm(request.POST,
                                     request.FILES,
                                     instance=config)
            formset = DomainFormSet(request.POST,request.FILES)
            # All validation rules pass
            if config_form.is_valid() and formset.is_valid():
                config_form.save()
                created_domains = []
                for domain in domains:
                    unclaimed = UnclaimedDomain.objects.filter(name=domain)
                    if unclaimed:
                        d = unclaimed[0]
                        claimed = Domain(name=domain,
                                         votes=d.votes,
                                         config=config)
                        d.delete()
                    else:
                        claimed = Domain(name=domain, votes=1, config=config)
                    claimed.save()
                return HttpResponseRedirect('/add/') # Redirect after POST

    else:
        config_form = ConfigForm()
        formset = DomainFormSet(initial=[{'name': domain}])

    return render_to_response('config/enter_config.html', {
        'formset': formset,
        'config_form': config_form,
    }, context_instance=RequestContext(request))
예제 #3
0
def show_issue(request, id):
    issue = get_object_or_404(Issue, pk=id)
    incoming = []
    outgoing = []
    base = []
    non_modified_domains = set()
    removed_domains = set()
    added_domains = set()
    error = ""
    new_docurl_formset = []
    new_enableurl_formset = []

    up_conf = issue.updated_config
    if up_conf:
        for field in issue.config._meta.fields:
            data = {'name': field.name,
                    'verbose_name': field.verbose_name,
                    'choices': field.choices,
                    'value': getattr(issue.config, field.name)}
            if up_conf and field in up_conf._meta.fields:
                new_value = getattr(up_conf, field.name)
                if data['value'] != new_value:
                    data['new_value'] = new_value
            if field.name in ('display_name', 'display_short_name'):
                base.append(data)
            elif field.name.startswith('incoming'):
                incoming.append(data)
            elif field.name.startswith('outgoing'):
                if field.name not in ('outgoing_add_this_server'
                                      'outgoing_use_global_preferred_server'):
                    outgoing.append(data)
        # Doc URLs
        DocURLFormSet = modelformset_factory(DocURL, extra=0,
                form=DocURLForm, formset=BaseDocURLFormSet)
        docurl_formset = DocURLFormSet(queryset=issue.config.docurl_set.all())
        new_docurl_formset = DocURLFormSet(queryset=up_conf.docurl_set.all())
        # Enable URLs
        EnableURLFormSet = modelformset_factory(EnableURL, extra=0,
                form=EnableURLForm, formset=BaseEnableURLFormSet)
        enableurl_formset = EnableURLFormSet(
                queryset=issue.config.enableurl_set.all())
        new_enableurl_formset = EnableURLFormSet(
                queryset=up_conf.enableurl_set.all())

        # get removed/added domains
        original_domains = []
        updated_domains = []
        for domain in (issue.config.domains.all() or
                       issue.config.domainrequets.all()):
            original_domains.append(domain.name)
        for domain in issue.updated_config.domainrequests.all():
            updated_domains.append(domain.name)
        d_set = set(original_domains)
        ud_set = set(updated_domains)
        non_modified_domains = d_set.intersection(updated_domains)
        removed_domains = d_set.difference(updated_domains)
        added_domains = ud_set.difference(original_domains)

    if request.method == 'POST':
        data = request.POST
        if not request.user.is_superuser:
            return HttpResponseRedirect(reverse('ispdb_login'))
        if data['action'] == 'close':
            issue.status = 'closed'
            issue.save()
        elif data['action'] == 'merge':
            if not issue.config.status == "approved":
                error = "Can't merge into non approved configurations."
            if issue.config.locked:
                error = ("This configuration is locked. Only admins can unlock"
                         " it.")
            elif issue.updated_config:
                # check if any of the added domains are already bound to some
                # approved configuration
                if added_domains:
                    for domain in added_domains:
                        if Domain.objects.filter(name=domain,
                                                 config__status="approved"):
                            error = """Can't approve this configuration.
                                    Domain %s is already used by another
                                    approved configuration.""" % (domain)
                            break
                if not error:
                    # update domains
                    for domain in removed_domains:
                        d = Domain.objects.filter(name=domain)[0]
                        d.delete()
                    for domain in added_domains:
                        exists = Domain.objects.filter(name=domain)
                        if exists:
                            exists[0].config = issue.config
                            exists[0].save()
                        else:
                            claimed = Domain(name=domain,
                                             config=issue.config)
                            claimed.save()
                    # update config fields
                    all_fields = base + incoming + outgoing
                    for field in all_fields:
                        if 'new_value' in field:
                            setattr(issue.config, field['name'],
                                    field['new_value'])
                    # update docurls
                    docurl_formset.delete()
                    for docurl in new_docurl_formset:
                        docurl.instance.config = issue.config
                        docurl.instance.save()
                    # update enableurls
                    enableurl_formset.delete()
                    for enableurl in new_enableurl_formset:
                        enableurl.instance.config = issue.config
                        enableurl.instance.save()
                    # update issue
                    issue.updated_config.status = 'deleted'
                    issue.updated_config.save()
                    issue.config.save()
                    issue.status = 'closed'
                    issue.save()
                    return HttpResponseRedirect(reverse('ispdb_details',
                        args=[issue.config.id]))

    return render_to_response("config/show_issue.html", {
            'issue': issue,
            'base': base,
            'incoming': incoming,
            'outgoing': outgoing,
            'non_modified_domains': non_modified_domains,
            'removed_domains': removed_domains,
            'added_domains': added_domains,
            'new_docurl_formset': new_docurl_formset,
            'new_enableurl_formset': new_enableurl_formset,
            'error': error,
        }, context_instance=RequestContext(request))