Exemplo n.º 1
0
 def clean_address(self):
     address = self.cleaned_data.get('address')
     if not address:
         raise forms.ValidationError(_(u"请填写邮件地址。", ))
     if not pure_email_regex(address):
         address = u'{}@{}'.format(address, self.listobj.domain)
         if not pure_email_regex(address):
             raise forms.ValidationError(_(u"输入的邮件地址不合法。", ))
     if ExtListMember.objects.exclude(id=self.instance.id).filter(
             address=address, list_id=self.list_id).exists():
         raise forms.ValidationError(_(u"邮件地址已存在列表中。", ))
     if self.listobj.is_everyone:
         if not Mailbox.objects.filter(mailbox=address).exists():
             raise forms.ValidationError(_(u"邮件地址不在邮件账号中。", ))
     return address
Exemplo n.º 2
0
 def clean_address(self):
     address = self.cleaned_data.get('address')
     if not address:
         self.error_notify = _(u"请填写邮件地址。")
         raise forms.ValidationError(self.error_notify)
     if not pure_email_regex(address):
         address = u'{}@{}'.format(address, self.extlist.domain)
         if not pure_email_regex(address):
             self.error_notify = _(u"输入的邮件地址不合法")
             raise forms.ValidationError(self.error_notify)
     if ExtListMember.objects.exclude(id=self.instance.id).filter(
             address=address, extlist=self.extlist).exists():
         self.error_notify = _(u"邮件地址已存在列表中")
         raise forms.ValidationError(self.error_notify)
     if self.extlist.is_everyone:
         if not Mailbox.objects.filter(username=address).exists():
             self.error_notify = _(u"邮件地址不在邮件账号中")
             raise forms.ValidationError(self.error_notify)
     return address
Exemplo n.º 3
0
 def clean_address(self):
     address = self.cleaned_data.get('address')
     if not address:
         raise forms.ValidationError(_(u"请输入邮箱。", ))
     if not pure_email_regex(address):
         raise forms.ValidationError(_(u"输入的邮箱不合法。", ))
     if not Mailbox.objects.filter(mailbox=address).exists():
         raise forms.ValidationError(_(u"邮件地址不在邮件账号中。", ))
     if CoreGroupMember.objects.exclude(id=self.instance.id).filter(
             group=self.group, address=address).exists():
         raise forms.ValidationError(_(u"组成员已存在列表中。", ))
     return address
Exemplo n.º 4
0
 def clean_address(self):
     if self.address:
         return self.address
     address = self.cleaned_data.get('address')
     address = address and address.strip() or None
     if not address:
         raise forms.ValidationError(_(u"请填写邮件列表地址。", ))
     if not self.is_import:
         address = u"{}@{}".format(address, self.domain)
     if not pure_email_regex(address):
         raise forms.ValidationError(_(u"输入的邮件列表地址不合法。", ))
     if ExtList.objects.exclude(id=self.instance.id).filter(
             address=address, domain_id=self.domain_id).exists():
         raise forms.ValidationError(_(u"邮件列表地址已存在列表中。", ))
     return address
Exemplo n.º 5
0
 def clean_src_mailbox(self):
     account = self.cleaned_data.get('src_mailbox')
     account = account.strip()
     if not account:
         self.error_notify = _(u"远程帐号为空")
         raise forms.ValidationError(_(u"请输入远程帐号"))
     if not pure_email_regex(account):
         self.error_notify = _(u"输入的远程账号不合法。")
         raise forms.ValidationError(_(self.error_notify))
     mailbox_id = self.cleaned_data.get('mailbox_id')
     if IMAPMoving.objects.exclude(id=self.instance.id).filter(
             mailbox_id=mailbox_id, src_mailbox=account).exists():
         self.error_notify = _(u"该用户已经在“邮箱搬家”设置了相同的迁移任务。")
         raise forms.ValidationError(_(self.error_notify))
     if POP3Moving.objects.filter(mailbox_id=mailbox_id,
                                  src_mailbox=account).exists():
         self.error_notify = _(u"该用户已经在“邮箱搬家”设置了相同的迁移任务。")
         raise forms.ValidationError(_(self.error_notify))
     return account
Exemplo n.º 6
0
 def clean_address(self):
     if self.address:
         return self.address
     address = self.cleaned_data.get('address')
     address = address and address.strip() or None
     if not address:
         self.error_notify = _(u"请填写邮件列表地址。")
         raise forms.ValidationError(_(self.error_notify))
     if not self.is_import:
         address = u"{}@{}".format(address, self.domain)
     if not pure_email_regex(address):
         self.error_notify = _(u"输入的邮件列表地址不合法")
         raise forms.ValidationError(_(self.error_notify))
     if ExtList.objects.exclude(id=self.instance.id).filter(
             address=address, domain_id=self.domain_id).exists():
         self.error_notify = _(u"邮件列表地址已存在列表中")
         raise forms.ValidationError(_(self.error_notify))
     if Mailbox.objects.filter(username=address).exists():
         self.error_notify = _(u"邮件列表地址不能是已存在的邮箱帐号")
         raise forms.ValidationError(_(self.error_notify))
     if CoreAlias.objects.filter(source=address).exists():
         self.error_notify = _(u"邮件列表地址不能是已存在的邮箱别名")
         raise forms.ValidationError(_(self.error_notify))
     return address
Exemplo n.º 7
0
def groups_mem_import(request, group_id):
    gobj = CoreGroup.objects.get(id=group_id)
    form = CoreGroupMemberImportForm()
    if request.method == "POST":
        form = CoreGroupMemberImportForm(data=request.POST,
                                         files=request.FILES)
        if form.is_valid():
            success, fail = 0, 0
            fail_list = []
            file_ext = form.file_ext
            if file_ext == 'txt':
                lines = form.file_obj.readlines()
                for line in lines:
                    line = line.replace('\n',
                                        '').replace('\r',
                                                    '').replace('\000', '')
                    elem = line.strip().replace(u',', '\t').replace(
                        ',', '\t').replace(u';',
                                           '\t').replace(';', '\t').split('\t')
                    length = len(elem)
                    address = elem[0] if length >= 1 else ''
                    if not pure_email_regex(address):
                        fail += 1
                        fail_list.append(u"( %s,%s)" % (address, _(u"格式不正确")))
                        continue
                    o = Mailbox.objects.filter(username=address).first()
                    if not o or o.domain_id != gobj.domain_id:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, _(u"邮箱不存在于该域名下")))
                        continue
                    form = CoreGroupMemberForm(gobj, o, {
                        'group': gobj,
                        'mailbox': o and o.id or 0,
                    })
                    if form.is_valid():
                        form.save()
                        success += 1
                    else:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, form.error_message))
            elif file_ext == 'csv':
                import csv
                lines = list(csv.reader(form.file_obj))
                for elem in lines:
                    length = len(elem)
                    address = elem[0] if length > 1 else ''
                    if not pure_email_regex(address):
                        fail += 1
                        fail_list.append(u"( %s,%s)" % (address, _(u"格式不正确")))
                        continue
                    o = Mailbox.objects.filter(username=address).first()
                    if not o or o.domain_id != gobj.domain_id:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, _(u"邮箱不存在于该域名下")))
                        continue
                    form = CoreGroupMemberForm(gobj, o, {
                        'group': gobj,
                        'mailbox': o and o.id or 0,
                    })
                    if form.is_valid():
                        form.save()
                        success += 1
                    else:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, form.error_message))
            elif file_ext in ('xls', 'xlsx'):
                import xlrd
                content = form.file_obj.read()
                workbook = xlrd.open_workbook(filename=None,
                                              file_contents=content)
                table = workbook.sheets()[0]
                for line in xrange(table.nrows):
                    #前两行跳过
                    # if line in (0,1):
                    #     continue
                    elem = table.row_values(line)
                    address = elem[0] if elem else ''
                    if not pure_email_regex(address):
                        fail += 1
                        fail_list.append(u"( %s,%s)" % (address, _(u"格式不正确")))
                        continue
                    o = Mailbox.objects.filter(username=address).first()
                    if not o or o.domain_id != gobj.domain_id:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, _(u"邮箱不存在于该域名下")))
                        continue
                    form = CoreGroupMemberForm(gobj, o, {
                        'group': gobj,
                        'mailbox': o and o.id or 0,
                    })
                    if form.is_valid():
                        form.save()
                        success += 1
                    else:
                        fail += 1
                        fail_list.append(u"( %s,%s)" %
                                         (address, form.error_message))
            messages.add_message(
                request, messages.SUCCESS,
                _(u'批量添加成功%(success)s个, 失败%(fail)s个') % {
                    "success": success,
                    "fail": fail
                })
            if fail_list:
                messages.add_message(
                    request, messages.ERROR,
                    _(u'失败详情 : %(fail)s') % {"fail": u','.join(fail_list)})
            return HttpResponseRedirect(
                reverse('core_group_member', args=(group_id, )))
    return render(request, "group/groups_mem_import.html", {
        'obj': gobj,
        'group_id': group_id,
        "form": form
    })
Exemplo n.º 8
0
 def clean_email(self):
     email = self.cleaned_data.get('email')
     if email and not pure_email_regex(email):
         raise forms.ValidationError(_(u"输入的E-Mail不合法。"))
     return email