def add_to_list(request, template="lists/add.html"): """add_to_list""" error_msg = "" is_saved = False if not request.user.is_superuser: account_type = request.session["user_filter"]["account_type"] else: account_type = 1 if request.method == "GET": if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) elif request.method == "POST": if account_type == 1 or account_type == 2: form = AdminListAddForm(request, request.POST) else: form = ListAddForm(request, request.POST) if form.is_valid(): clean_data = form.cleaned_data if account_type == 1 or account_type == 2: user_part = clean_data["user_part"] to_address = clean_data["to_address"] if user_part is None or user_part == "": user_part = "any" if to_address is None or to_address == "": to_address = "any" if user_part != "any" and to_address != "any": toaddr = "%s@%s" % (force_escape(user_part), force_escape(to_address)) elif user_part == "any" and to_address != "any": toaddr = to_address else: toaddr = to_address else: toaddr = clean_data["to_address"] try: l = List( list_type=clean_data["list_type"], from_address=clean_data["from_address"], to_address=toaddr, user=request.user, ) l.save() is_saved = True if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) except IntegrityError: error_msg = "The list item already exists" except: error_msg = "Error occured saving the list item" if request.is_ajax(): response = simplejson.dumps({"success": is_saved, "error_msg": error_msg}) return HttpResponse(response, content_type="application/javascript; charset=utf-8") else: if request.is_ajax(): error_list = form.errors.values()[0] html = dict([(k, [unicode(e) for e in v]) for k, v in form.errors.items()]) # form.errors response = simplejson.dumps( {"success": False, "error_msg": unicode(error_list[0]), "form_field": html.keys()[0]} ) return HttpResponse(response, content_type="application/javascript; charset=utf-8") return render_to_response(template, {"form": form}, context_instance=RequestContext(request))
def add_to_list(request, template='lists/add.html'): """add_to_list""" error_msg = '' is_saved = False if not request.user.is_superuser: account_type = request.session['user_filter']['account_type'] else: account_type = 1 if request.method == 'GET': if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) elif request.method == 'POST': if account_type == 1 or account_type == 2: form = AdminListAddForm(request, request.POST) else: form = ListAddForm(request, request.POST) if form.is_valid(): clean_data = form.cleaned_data if account_type == 1 or account_type == 2: user_part = clean_data['user_part'] to_address = clean_data['to_address'] if user_part is None or user_part == '': user_part = 'any' if to_address is None or to_address == '': to_address = 'any' if user_part != 'any' and to_address != 'any': toaddr = "%s@%s" % (force_escape(user_part), force_escape(to_address)) elif user_part == 'any' and to_address != 'any': toaddr = to_address else: toaddr = to_address else: toaddr = clean_data['to_address'] try: l = List(list_type=clean_data['list_type'], from_address=clean_data['from_address'], to_address=toaddr, user=request.user) l.save() is_saved = True if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) except IntegrityError: error_msg = _('The list item already exists') if request.is_ajax(): response = anyjson.dumps({ 'success': is_saved, 'error_msg': error_msg }) return HttpResponse( response, content_type='application/javascript; charset=utf-8') else: if request.is_ajax(): error_list = form.errors.values()[0] html = dict([(k, [unicode(e) for e in v]) for k, v in form.errors.items()]) response = anyjson.dumps({ 'success': False, 'error_msg': unicode(error_list[0]), 'form_field': html.keys()[0] }) return HttpResponse( response, content_type='application/javascript; charset=utf-8') return render_to_response(template, {'form': form}, context_instance=RequestContext(request))
def add_to_list(request, template = 'lists/add.html'): """add_to_list""" error_msg = '' is_saved = False if not request.user.is_superuser: account_type = request.session['user_filter']['account_type'] else: account_type = 1 if request.method == 'GET': if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) elif request.method == 'POST': if account_type == 1 or account_type == 2: form = AdminListAddForm(request, request.POST) else: form = ListAddForm(request, request.POST) if form.is_valid(): clean_data = form.cleaned_data if account_type == 1 or account_type == 2: user_part = clean_data['user_part'] to_address = clean_data['to_address'] if user_part is None or user_part == '': user_part = 'any' if to_address is None or to_address == '': to_address = 'any' if user_part != 'any' and to_address != 'any': toaddr = "%s@%s" % ( force_escape(user_part), force_escape(to_address)) elif user_part == 'any' and to_address != 'any': toaddr = to_address else: toaddr = to_address else: toaddr = clean_data['to_address'] try: l = List(list_type=clean_data['list_type'], from_address=clean_data['from_address'], to_address=toaddr, user=request.user) l.save() is_saved = True if account_type == 1 or account_type == 2: form = AdminListAddForm(request) else: form = ListAddForm(request) except IntegrityError: error_msg = _('The list item already exists') if request.is_ajax(): response = anyjson.dumps({'success': is_saved, 'error_msg': error_msg}) return HttpResponse(response, content_type='application/javascript; charset=utf-8') else: if request.is_ajax(): error_list = form.errors.values()[0] html = dict([(k, [unicode(e) for e in v]) for k, v in form.errors.items()]) response = anyjson.dumps({'success': False, 'error_msg': unicode(error_list[0]), 'form_field': html.keys()[0]}) return HttpResponse(response, content_type='application/javascript; charset=utf-8') return render_to_response(template, {'form': form}, context_instance=RequestContext(request))