Example #1
0
def bot(request, asset_type, asset_id):
    ctx = assets.get_asset_edit_context(request, asset_type, asset_id)
    user_asset = ctx['user_asset']
    bot = user_asset.asset
    bot.syncFromServer()

    form = formtools.MultiForm(request.POST)
    form.validate(EditBotForm, bot, post_defaults={
        # Checkboxes must be explicitly defaulted to zero here, since
        # an unchecked box will not show up in our POST data.
        'show_project_names': 0,
        })
    form.validate(assets.EditAssetForm, user_asset)

    if request.POST and form.is_valid():
        cset = models.AssetChangeset.objects.begin(request, bot)
        form.EditAssetForm.apply(cset, request, user_asset)
        form.EditBotForm.apply(cset)
        cset.finish()

        if form.EditAssetForm.should_delete():
            return form.EditAssetForm.delete(request, user_asset)

    ctx.update({
        'form': form,
        'network_host': bot.network.getHost('irc'),
        'channel': get_channel_from_location(bot.location),
        })
    return render_to_response('accounts/bot_edit.html', RequestContext(request, ctx))
Example #2
0
def add_stats_asset(request, asset_type, prefix, template, name=None):
    """Generic form for adding stats-based assets"""
    model = get_asset_by_type(asset_type)
    ctx = get_asset_add_context(request, asset_type)
    form = formtools.MultiForm(request.POST)
    form.validate(AddStatsAssetForm)

    if request.POST:
        if form.is_valid():
            name = form.cleaned_data['name']
    else:
        # Don't show errors if the form hasn't been submitted once
        form.errors = None

    # Names may be supplied via the form or via the URL.
    #
    # XXX: Generally it's a bad idea for GET requests to have side-effects
    #      like this. Our excuse is that the operation requires login and
    #      that the result is idempotent. If this turns out to be a problem
    #      anyway, we could redirect the user if the asset exists but only
    #      pre-fill the form if it doesn't yet exist.
    #
    if name:
        # Get/create the stats target
        target = StatsTarget.objects.get_or_create(path=prefix + name)[0]
        target.enforce_defaults()

        # Now get/create the matching asset
        asset, created_asset = model.objects.get_or_create(target=target)
        cset = models.AssetChangeset.objects.begin(request, asset)
        if created_asset:
            cset.set_meta('_created')

        # Finally, create a new UserAsset.
        user_asset = models.UserAsset.objects.get_or_create_if_allowed(
            request.user, asset, cset)

        cset.finish()

        # Redirect either to the new UserAsset or to a conflict resolution page
        if user_asset is None:
            return HttpResponseRedirect("/account/conflict/%s/%s/" %
                                        (asset_type, asset.id))
        else:
            return HttpResponseRedirect("/account/%s/%s/" %
                                        (asset_type, user_asset.id))

    ctx.update({
        'form': form,
    })
    return render_to_response(template, RequestContext(request, ctx))
Example #3
0
def add_bot(request, asset_type):
    form = formtools.MultiForm(request.POST)

    if request.POST:
        form.validate(AddBotForm)
        if form.AddBotForm.is_other_network():
            form.validate(AddNetworkForm)

        if form.is_valid():
            # Get/create the network, now that we know all forms validated
            network = form.cleaned_data['network'] or form.AddNetworkForm.get_or_create(request)
            location = normalize_channel_to_location(form.cleaned_data['channel'])

            # Now look up a matching bot. We might have to create this too.
            defaults = dict(
                network = network,
                location = location,
                )
            bot, created_bot = models.Bot.objects.get_or_create(
                network = network,
                location__iexact = location,
                defaults = defaults,
                )
            cset = models.AssetChangeset.objects.begin(request, bot)
            if created_bot:
                cset.set_meta('_created')
                cset.set_field_dict(defaults)

            # Finally, create a new UserAsset.
            user_asset = models.UserAsset.objects.get_or_create_if_allowed(request.user, bot, cset)

            cset.finish()

            # Redirect either to the new UserAsset or to a conflict resolution page
            if user_asset is None:
                return HttpResponseRedirect("/account/conflict/%s/%s/" % (asset_type, bot.id))
            else:
                return HttpResponseRedirect("/account/%s/%s/" % (asset_type, user_asset.id))

    # Automatically initialize the Network list, if it's empty.
    allNetworks = list(models.Network.objects.all().order_by('-is_popular', 'description'))
    if not allNetworks:
        models.Network.objects.importNetworks()
        allNetworks = list(models.Network.objects.all())

    ctx = assets.get_asset_add_context(request, asset_type)
    ctx.update({
        'networks': allNetworks,
        'form': form,
        })
    return render_to_response('accounts/bot_add.html', RequestContext(request, ctx))
Example #4
0
def stats_asset(request, asset_type, asset_id):
    """Generic form for editing stats-based assets."""
    ctx = get_asset_edit_context(request, asset_type, asset_id)
    user_asset = ctx['user_asset']
    asset = user_asset.asset
    asset.target.enforce_defaults()

    form = formtools.MultiForm(request.POST)
    form.validate(EditAssetForm, user_asset)
    form.validate(StatsMetadataForm, asset.target)
    ctx['form'] = form

    if request.POST and form.is_valid():
        cset = models.AssetChangeset.objects.begin(request, asset)
        form.StatsMetadataForm.apply(cset)
        form.EditAssetForm.apply(cset, request, user_asset)
        cset.finish()

        if form.EditAssetForm.should_delete():
            return form.EditAssetForm.delete(request, user_asset)

    return render_to_response('accounts/stats_asset_edit.html',
                              RequestContext(request, ctx))
Example #5
0
def project(request, asset_type, asset_id):
    """Generic form for editing stats-based assets."""
    ctx = get_asset_edit_context(request, asset_type, asset_id)
    user_asset = ctx['user_asset']
    asset = user_asset.asset
    asset.target.enforce_defaults()

    # Non-top-level projects are currently only editable as generic stats items.
    if not asset.is_top_level():
        return stats_asset(request, asset_type, asset_id)

    form = formtools.MultiForm(request.POST)
    ctx['form'] = form
    form.validate(EditAssetForm, user_asset)
    form.validate(StatsMetadataForm, asset.target)
    form.validate(
        ProjectForm,
        post_defaults={'use_repository': False},
        defaults={'use_repository': asset.repos and asset.repos.is_active})

    use_repository = bool(form.ProjectForm.cleaned_data.get('use_repository'))
    if use_repository:
        repos = asset.repos
        if not repos:
            # Create a blank Repository immediately and bind it to the RepositoryForm,
            # but don't save it to the database until we know the submission is valid.
            # We need to do this because the process of probing a repository must occur
            # before we know the form is valid, but it changes model state which needs
            # to be saved if the form turns out to be valid.

            repos = Repository(
                created_by=request.user,
                project_name=asset.get_name(),
                pinger_name=Repository.objects.get_new_pinger_name(),
                is_active=False,
            )

        form.validate(RepositoryForm,
                      repos,
                      post_defaults={
                          'enable_polling': False,
                          'forward_pinger_mail': False
                      })
    else:
        # It's okay to use a blank one here, we're guaranteed not to save it.
        repos = asset.repos or Repository()
        form.add(RepositoryForm, repos)

    if request.POST and form.is_valid():
        cset = models.AssetChangeset.objects.begin(request, asset)
        form.StatsMetadataForm.apply(cset)
        form.EditAssetForm.apply(cset, request, user_asset)
        form.ProjectForm.apply(cset, request, repos)
        if use_repository:
            form.RepositoryForm.apply(cset)
        cset.finish()

        if form.EditAssetForm.should_delete():
            return form.EditAssetForm.delete(request, user_asset)

    return render_to_response('accounts/project_edit.html',
                              RequestContext(request, ctx))