Commits

Dominique Guardiola  committed 27f97a6

encoding fix

  • Participants
  • Parent commits 9ea9868

Comments (0)

Files changed (2)

File django_mailman/admin.py

+# -*- coding:utf-8 -*-
+from django.contrib import admin
+from django import forms
+from django_mailman.models import List
+
+admin.site.register(List)

File django_mailman/models.py

 
 from webcall import MultipartPostHandler
 
+def my_containsAny(str, set):
+    for c in set:
+        if c in str: return True;
+    return False;
+
+
 # Mailman-Messages for a successfull subscription
 SUBSCRIBE_MSG = (
     u'Erfolgreich eingetragen', # de
     u'Successfully subscribed', # en
-    u'Abonnement r\xe9ussi', # fr
+    u'Abonnement réussi', #fr
+)
+
+ALREADY_MSG = (
+    u'Already a member', #en
+    u'Déjà abonné', #fr
 )
 
 # Mailman-Messages for successfully remove from a list
     u'Erfolgreich beendete Abonnements', # de
     u'Successfully Removed', # en
     u'Successfully Unsubscribed', # also en
-    u'R\xe9siliation r\xe9ussie', # fr
+    u'Résiliation réussie',#fr
+    u'Abonnement résilié avec succès', #also fr
 )
 
 # Mailman-Messages for a failed remove from a list
 NON_MEMBER_MSG = (
     u'Nichtmitglieder können nicht aus der Mailingliste ausgetragen werden', # de
     u'Cannot unsubscribe non-members', # en
-    u"Ne peut r\xe9silier l'abonnement de non-abonn\xe9s ", # fr
+    u'Ne peut résilier l\'abonnement de non-abonnés', #fr
 )
 
-# To control user form unsubscription
-UNSUBSCRIBE_BUTTON = {
-    'fr' : 'Résilier',
-}
-
 # Definition from the Mailman-Source ../Mailman/Default.py
 LANGUAGES = (
     ('utf-8',       _('Arabic')),
 class List(models.Model):
     name = models.CharField(max_length=50, unique=True)
     password = models.CharField(max_length=50)
-    email = models.EmailField(unique=True)
-    main_url = models.URLField(verify_exists=False)
+    email = models.EmailField()
+    main_url = models.URLField()
     encoding = models.CharField(max_length=20, choices=LANGUAGES)
 
     class Meta:
         else:
             raise Exception('Could not find member-information')
 
-        msg = msg.decode(self.encoding)
-        member = member.decode(self.encoding)
         return (msg, member)
+        #return (unicode(msg), unicode(member)) #All has to be in unicode
 
     def __parse_member_content(self, content, encoding='iso-8859-1'):
         if not content:
             raise Exception('No valid Content!')
+
         members = []
-        letters = re.findall('letter=\w{1}', content)
-        chunks = re.findall('chunk=\d+', content)
+        letters = re.findall('\?letter=[a-z]', content)
         input = re.findall('name=".+_realname" type="TEXT" value=".*" size="[0-9]+" >', content)
         for member in input:
             info = member.split('" ')
             email = re.search('(?<=name=").+(?=_realname)', info[0]).group(0)
             realname = re.search('(?<=value=").*', info[2]).group(0)
+            email = email.replace("%40", "@") #ajout ici #All has to be in unicode
             email = unicode(email, encoding)
             realname = unicode(realname, encoding)
             members.append([realname, email])
-        letters = set(letters)
-        return (letters, members, chunks)
+        return (letters, members)
 
     def get_admin_moderation_url(self):
         return '%s/admindb/%s/?adminpw=%s' % (self.main_url, self.name,
         opener = urllib2.build_opener(MultipartPostHandler(self.encoding, True))
         content = opener.open(url, SUBSCRIBE_DATA).read()
 
-        (msg, member) = self.__parse_status_content(unicode(content, self.encoding))
-        if (msg not in SUBSCRIBE_MSG):
+        (msg, member) = self.__parse_status_content(unicode(content, self.encoding)) #All has to be in unicode
+        #(msg, member) = self.__parse_status_content(content)
+        if((msg not in SUBSCRIBE_MSG) and ('Error subscribing' in msg and not my_containsAny(member,ALREADY_MSG))):
             error = u'%s: %s' % (msg, member)
             raise Exception(error.encode(self.encoding))
 
         email = check_encoding(email, self.encoding)
         UNSUBSCRIBE_DATA['adminpw'] = self.password
         UNSUBSCRIBE_DATA['unsubscribees_upload'] = email
-        opener = urllib2.build_opener(MultipartPostHandler(self.encoding))
+        opener = urllib2.build_opener(MultipartPostHandler(self.encoding, True))
         content = opener.open(url, UNSUBSCRIBE_DATA).read()
 
-        (msg, member) = self.__parse_status_content(content)
+        (msg, member) = self.__parse_status_content(unicode(content, self.encoding))
+        #import pdb; pdb.set_trace()
+        
         if (msg not in UNSUBSCRIBE_MSG) and (msg not in NON_MEMBER_MSG):
             error = u'%s: %s' % (msg, member)
             raise Exception(error.encode(self.encoding))
 
         all_members = []
         content = opener.open(url, data).read()
-        (letters, members, chunks) = self.__parse_member_content(content, self.encoding)
+        (letters, members) = self.__parse_member_content(content, self.encoding)
         all_members.extend(members)
-        for letter in letters:
-            url_letter = u"%s?%s" %(url, letter)
-            content = opener.open(url_letter, data).read()
-            (letters, members, chunks) = self.__parse_member_content(content, self.encoding)
+        for letter in letters[1:]:
+            content = opener.open(url + letter, data).read()
+            (letters, members) = self.__parse_member_content(content, self.encoding)
             all_members.extend(members)
-            for chunk in chunks[1:]:
-                url_letter_chunk = "%s?%s&%s" %(url, letter, chunk)
-                content = opener.open(url_letter_chunk, data).read()
-                (letters, members, chunks) = self.__parse_member_content(content, self.encoding)
-                all_members.extend(members)
-
-        members = {}
-        for m in all_members:
-            email = m[1].replace(u"%40", u"@")
-            members[email] = m[0]
-        all_members = [(email, name) for email, name in members.items()]
         all_members.sort()
         return all_members
-
-    def user_subscribe(self, email, password, language='fr', first_name=u'', last_name=u''):
-
-        url = '%s/subscribe/%s' % (self.main_url, self.name)
-
-        password = check_encoding(password, self.encoding)
-        email = check_encoding(email, self.encoding)
-        first_name = check_encoding(first_name, self.encoding)
-        last_name = check_encoding(last_name, self.encoding)
-        name = '%s %s' % (first_name, last_name)
-
-        SUBSCRIBE_DATA['email'] = email
-        SUBSCRIBE_DATA['pw'] = password
-        SUBSCRIBE_DATA['pw-conf'] = password
-        SUBSCRIBE_DATA['fullname'] = name
-        SUBSCRIBE_DATA['language'] = language
-        opener = urllib2.build_opener(MultipartPostHandler(self.encoding, True))
-        request = opener.open(url, SUBSCRIBE_DATA)
-        content = request.read()
-        for status in SUBSCRIBE_MSG:
-            if len(re.findall(status, content)) > 0:
-                return True
-        raise Exception(content)
-
-    def user_subscribe(self, email, password, language='fr', first_name=u'', last_name=u''):
-
-        url = '%s/subscribe/%s' % (self.main_url, self.name)
-
-        password = check_encoding(password, self.encoding)
-        email = check_encoding(email, self.encoding)
-        first_name = check_encoding(first_name, self.encoding)
-        last_name = check_encoding(last_name, self.encoding)
-        name = '%s %s' % (first_name, last_name)
-
-        SUBSCRIBE_DATA['email'] = email
-        SUBSCRIBE_DATA['pw'] = password
-        SUBSCRIBE_DATA['pw-conf'] = password
-        SUBSCRIBE_DATA['fullname'] = name
-        SUBSCRIBE_DATA['language'] = language
-        opener = urllib2.build_opener(MultipartPostHandler(self.encoding, True))
-        request = opener.open(url, SUBSCRIBE_DATA)
-        content = request.read()
-        # no error code to process
-
-    def user_unsubscribe(self, email, language='fr'):
-
-        url = '%s/options/%s/%s' % (self.main_url, self.name, email)
-
-        email = check_encoding(email, self.encoding)
-
-        UNSUBSCRIBE_DATA['email'] = email
-        UNSUBSCRIBE_DATA['language'] = language
-        UNSUBSCRIBE_DATA['login-unsub'] = UNSUBSCRIBE_BUTTON[language]
-        
-        opener = urllib2.build_opener(MultipartPostHandler(self.encoding, True))
-        request = opener.open(url, UNSUBSCRIBE_DATA)
-        content = request.read()
-        # no error code to process