示例#1
0
def sms_campaign_add(request):
    """Add a new sms campaign for the logged in user

    **Attributes**:

        * ``form`` - SMSCampaignForm
        * ``template`` - frontend/sms_campaign/change.html

    **Logic Description**:

        * Before adding a sms campaign, check dialer setting limit if
          applicable to the user.
        * Add the new sms campaign which will belong to the logged in user
          via SMSCampaignForm & get redirected to sms campaign list
    """
    # If dialer setting is not attached with user, redirect to sms campaign list
    if not user_dialer_setting(request.user):
        request.session['error_msg'] = \
            _("in order to add a sms campaign, you need to have your \
               settings configured properly, please contact the admin.")
        return HttpResponseRedirect(redirect_url_to_smscampaign_list)

    # Check dialer setting limit
    if request.user and request.method != 'POST':
        # check Max Number of running campaign
        if check_sms_dialer_setting(request, check_for="smscampaign"):
            msg = _("you have too many sms campaigns. Max allowed %(limit)s")\
                % {'limit': dialer_setting_limit(request, limit_for="smscampaign")}
            request.session['msg'] = msg

            # sms campaign limit reached
            frontend_send_notification(
                request, SMS_NOTIFICATION_NAME.sms_campaign_limit_reached)
            return HttpResponseRedirect(redirect_url_to_smscampaign_list)

    form = SMSCampaignForm(request.user)
    # Add sms campaign
    if request.method == 'POST':
        form = SMSCampaignForm(request.user, request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.user = User.objects.get(username=request.user)
            obj.save()
            form.save_m2m()

            request.session["msg"] = _('"%(name)s" is added.') %\
                {'name': request.POST['name']}
            return HttpResponseRedirect(redirect_url_to_smscampaign_list)

    template = 'frontend/sms_campaign/change.html'
    data = {
        'form': form,
        'action': 'add',
        'dialer_setting_msg': user_dialer_setting_msg(request.user),
    }
    return render_to_response(
        template, data, context_instance=RequestContext(request))
示例#2
0
def sms_campaign_change(request, object_id):
    """Update/Delete sms campaign for the logged in user

    **Attributes**:

        * ``object_id`` - Selected campaign object
        * ``form`` - SMSCampaignForm
        * ``template`` - frontend/sms_campaign/change.html

    **Logic Description**:

        * Update/delete selected sms campaign from the sms campaign list
          via SMSCampaignForm & get redirected to the sms campaign list
    """
    # If dialer setting is not attached with user, redirect to sms campaign list
    if not user_dialer_setting(request.user):
        return HttpResponseRedirect(redirect_url_to_smscampaign_list)

    sms_campaign = get_object_or_404(SMSCampaign, pk=object_id, user=request.user)
    form = SMSCampaignForm(request.user, instance=sms_campaign)
    if request.method == 'POST':
        # Delete sms campaign
        if request.POST.get('delete'):
            sms_campaign_del(request, object_id)
            return HttpResponseRedirect(redirect_url_to_smscampaign_list)
        else:
            # Update sms campaign
            form = SMSCampaignForm(
                request.user, request.POST, instance=sms_campaign)

            if form.is_valid():
                obj = form.save()
                obj.save()
                request.session["msg"] = _('"%(name)s" is updated.') \
                    % {'name': request.POST['name']}
                return HttpResponseRedirect(redirect_url_to_smscampaign_list)

    template = 'frontend/sms_campaign/change.html'
    data = {
        'form': form,
        'action': 'update',
        'dialer_setting_msg': user_dialer_setting_msg(request.user),
    }
    return render_to_response(
        template, data, context_instance=RequestContext(request))