Exemple #1
0
    def importContacts(self, f, llSender):
        mail_re = re.compile(r'^(item.*)?' + self.MAIL_INIT_STRING + r'(.*)$',
                             re.MULTILINE)
        name_re = re.compile(r'^' + self.NAME_INIT_STRING + r'(.*)$',
                             re.MULTILINE)
        group_re = re.compile(r'^' + self.GROUPS_INIT_STRING + r'(.*)$',
                              re.MULTILINE)

        Helper.cleanContacts(llSender)
        m_all = EmailTargetGroup.objects.get(name='all', user=llSender)
        imported = 0
        failed = []

        buf = f.read()
        re_vcard = re.compile(r'BEGIN:VCARD.*?END:VCARD', re.DOTALL)
        vcards = re_vcard.findall(buf)
        for vcard in vcards:
            emails = []
            if mail_re.search(vcard) != None:
                for email_tmp in mail_re.findall(vcard):
                    email_tmp = email_tmp[1]
                    email = email_tmp[email_tmp.find(":") +
                                      1:len(email_tmp)].strip()
                    emails.append(email)
                if name_re.search(vcard) != None:
                    tmp_name = name_re.search(vcard).group(1).replace(
                        ';', ' ').strip()
                else:
                    tmp_name = ''
                add_groups = []
                if group_re.search(vcard) != None:
                    groups_strs = group_re.search(vcard).group(1).split(',')
                    for group_str in groups_strs:
                        group_str = group_str.strip()
                        add_group, created = EmailTargetGroup.objects.get_or_create(
                            name=group_str, user=llSender)
                        add_groups.append(add_group)
                user = EmailTarget(name=tmp_name,
                                   address=emails[0],
                                   user=llSender)
                user.save()
                user.groups.add(m_all)
                if len(add_groups) > 0:
                    for add_group in add_groups:
                        user.groups.add(add_group)
                user.save()
                imported += 1
            else:
                failed.append(vcard)
        return imported, failed
 def importContacts(self, f, llSender):
     mail_re = re.compile(r'^(item.*)?' + self.MAIL_INIT_STRING + r'(.*)$', re.MULTILINE)
     name_re = re.compile(r'^' + self.NAME_INIT_STRING + r'(.*)$', re.MULTILINE)
     group_re = re.compile(r'^' + self.GROUPS_INIT_STRING + r'(.*)$', re.MULTILINE)
             
     Helper.cleanContacts(llSender)
     m_all = EmailTargetGroup.objects.get(name='all', user = llSender)
     imported = 0
     failed = []
     
     buf = f.read()
     re_vcard = re.compile(r'BEGIN:VCARD.*?END:VCARD', re.DOTALL)
     vcards = re_vcard.findall(buf)
     for vcard in vcards:
         emails = []
         if mail_re.search(vcard) != None:
             for email_tmp in mail_re.findall(vcard):
                 email_tmp = email_tmp[1]
                 email = email_tmp[email_tmp.find(":")+1:len(email_tmp)].strip()
                 emails.append(email)
             if name_re.search(vcard) != None:
                 tmp_name = name_re.search(vcard).group(1).replace(';', ' ').strip()
             else:
                 tmp_name = ''
             add_groups = []
             if group_re.search(vcard) != None:
                 groups_strs = group_re.search(vcard).group(1).split(',')
                 for group_str in groups_strs:
                     group_str = group_str.strip()
                     add_group, created = EmailTargetGroup.objects.get_or_create(name = group_str, user = llSender)
                     add_groups.append(add_group)
             user = EmailTarget(name = tmp_name, address = emails[0], user = llSender)
             user.save()
             user.groups.add(m_all)
             if len(add_groups) > 0:
                 for add_group in add_groups:
                     user.groups.add(add_group)
             user.save()
             imported += 1
         else:
             failed.append(vcard)
     return imported, failed
Exemple #3
0
def addressdetail(request, contact_id):

    mode = 'initial'
    email = None
    emailTargetId = None

    #############
    # find mode #
    #############
    try:
        emailTargetId = request.POST['id']
    except Exception as e:
        # no post data
        mode = 'display'
        emailTargetId = contact_id

    if mode != 'display':
        if emailTargetId == 'None':
            try:
                if request.POST['_save_goto_contacts'] != None:
                    mode = 'create__goto_contacts'
            except Exception as e:
                mode = 'create'
            emailTargetId = 0
        else:
            try:
                if request.POST['_save_goto_contacts'] != None:
                    mode = 'change__goto_contacts'
            except Exception as e:
                mode = 'change'


    #######################################
    # check if user is allowed to display #
    #######################################
    if int(emailTargetId) != 0:
        if EmailTarget.objects.get(id=emailTargetId).user.user.username != request.user.username:
            raise PermissionDenied

    ###################
    # execute request #
    ###################
    if mode == 'display':
        try:
            email = EmailTarget.objects.get(id=int(emailTargetId))
        except EmailTarget.DoesNotExist as e:
            email = EmailTarget()
    elif mode.startswith('create') or mode.startswith('change'):
        if mode.startswith('create'):
            email = EmailTarget()
        elif mode.startswith('change'):
            email = EmailTarget.objects.get(id=emailTargetId)
        email.name = request.POST['name']
        email.address = request.POST['address']
        email.user = ListletterSender.objects.filter(user__username=request.user.username)[0]
        email.save()

        liste = request.POST.getlist('groups')
        email.groups.clear()
        for group_id in liste:
            group = EmailTargetGroup.objects.get(id=group_id,user__user__username=request.user.username)
            email.groups.add(group)
        email.save()

    if mode.endswith('goto_contacts'):
        return render_to_response('listletter/addressindex.html', {
            'email_target_list': EmailTarget.objects.filter(user__user__username=request.user.username),
            'isContacts':True,
             'sidebar':True,
            })
    else:
        return render_to_response('listletter/addressdetail.html', {
            'email': email,
            'groups': EmailTargetGroup.objects.filter(user__user__username=request.user.username),
            'isContacts':True,
             'mode': mode
            })
Exemple #4
0
def addressdetail(request, contact_id):

    mode = 'initial'
    email = None
    emailTargetId = None

    #############
    # find mode #
    #############
    try:
        emailTargetId = request.POST['id']
    except Exception as e:
        # no post data
        mode = 'display'
        emailTargetId = contact_id

    if mode != 'display':
        if emailTargetId == 'None':
            try:
                if request.POST['_save_goto_contacts'] != None:
                    mode = 'create__goto_contacts'
            except Exception as e:
                mode = 'create'
            emailTargetId = 0
        else:
            try:
                if request.POST['_save_goto_contacts'] != None:
                    mode = 'change__goto_contacts'
            except Exception as e:
                mode = 'change'

    #######################################
    # check if user is allowed to display #
    #######################################
    if int(emailTargetId) != 0:
        if EmailTarget.objects.get(
                id=emailTargetId).user.user.username != request.user.username:
            raise PermissionDenied

    ###################
    # execute request #
    ###################
    if mode == 'display':
        try:
            email = EmailTarget.objects.get(id=int(emailTargetId))
        except EmailTarget.DoesNotExist as e:
            email = EmailTarget()
    elif mode.startswith('create') or mode.startswith('change'):
        if mode.startswith('create'):
            email = EmailTarget()
        elif mode.startswith('change'):
            email = EmailTarget.objects.get(id=emailTargetId)
        email.name = request.POST['name']
        email.address = request.POST['address']
        email.user = ListletterSender.objects.filter(
            user__username=request.user.username)[0]
        email.save()

        liste = request.POST.getlist('groups')
        email.groups.clear()
        for group_id in liste:
            group = EmailTargetGroup.objects.get(
                id=group_id, user__user__username=request.user.username)
            email.groups.add(group)
        email.save()

    if mode.endswith('goto_contacts'):
        return render_to_response(
            'listletter/addressindex.html', {
                'email_target_list':
                EmailTarget.objects.filter(
                    user__user__username=request.user.username),
                'isContacts':
                True,
                'sidebar':
                True,
            })
    else:
        return render_to_response(
            'listletter/addressdetail.html', {
                'email':
                email,
                'groups':
                EmailTargetGroup.objects.filter(
                    user__user__username=request.user.username),
                'isContacts':
                True,
                'mode':
                mode
            })