def generate_script_sieve(request, group_id): group = get_object_or_404(Group, id=group_id) if request.method == 'POST': if group.always_data_id: # There is always_data mail id from mechanize import ParseResponse, urlopen, urljoin response = urlopen("https://admin.alwaysdata.com/login/") forms = ParseResponse(response, backwards_compat=False) login_form = forms[0] if settings.DEBUG: print login_form login_form["email"] = settings.ALWAYS_DATA_ID login_form["password"] = settings.ALWAYS_DATA_PASSWORD response = urlopen(login_form.click()) url = 'https://admin.alwaysdata.com/email/%d/' % group.always_data_id response = urlopen(url) forms = ParseResponse(response, backwards_compat=False) if settings.DEBUG: for form in forms: print form try: email_form = forms[1] except IndexError: messages.warning(request, _(u'%(group)s is not bind to alwaysdata yet (wrong password)' % {'group': group})) return HttpResponseRedirect(reverse("group-detail", args=[group.pk])) email_form['sieve_filter'] = request.POST['filter_sieve'].encode('utf-8') req = email_form.click() req.add_header("Referer", url) response = urlopen(req) messages.success(request, _(u'Alwaysdata has been updated')) else: messages.warning(request, _(u'%(group)s is not bind to alwaysdata yet' % {'group': group})) return HttpResponseRedirect(reverse("group-detail", args=[group.pk])) else: filter_sieve = export_sieve_configuration(group.contacts.all()) context = get_global_context_data(Group, Group._meta.app_label) context['object_list'] = Group.objects.all() context['object'] = group context['filter_sieve'] = filter_sieve return render_to_response('contact/contact-sieve.html', context, context_instance=RequestContext(request))
def add_contacts_in_group(request, group_id): group = get_object_or_404(Group, id=group_id) if request.method == 'POST': emails = request.POST['emails'].replace(',', '\n').split('\n') emails = filter(None, [x.strip() for x in emails]) valid_emails = [] invalid_emails = [] for email in emails: if validate_email(email): valid_emails.append(email) else: invalid_emails.append(email) for email in valid_emails: contact, create = Contact.objects.get_or_create(email=email) contact.group.add(group) messages.success(request, _(u'%(contact)s added to %(group)s' % {'contact': contact, 'group': group})) for email in invalid_emails: m = PREFIXED_EMAIL_REGEX.match(email) if m: name = m.group(1) email = m.group(2) contact, create = Contact.objects.get_or_create(email=email, defaults={'name': name}) contact.group.add(group) messages.success(request, _(u'%(contact)s added to %(group)s' % {'contact': contact, 'group': group})) else: print email return HttpResponseRedirect(reverse("group-detail", args=[group.pk])) else: context = get_global_context_data(Group, Group._meta.app_label) context['object_list'] = Group.objects.all() context['object'] = group return render_to_response('contact/contact-batch.html', context, context_instance=RequestContext(request))