Browse Source

import the founders

Christoph Scheid 5 years ago
parent
commit
152a1b953b
4 changed files with 111 additions and 159 deletions
  1. 105
    159
      c3smembership/import_datasets.py
  2. 1
    0
      development.ini
  3. 4
    0
      import/test_founders.csv
  4. 1
    0
      production.ini

+ 105
- 159
c3smembership/import_datasets.py View File

@@ -21,170 +21,116 @@ from types import NoneType
21 21
 import unicodecsv
22 22
 
23 23
 
24
-# @view_config(permission='manage',
25
-#              route_name='import_founders')
26
-# def import_founders(request):
27
-#     '''
28
-#     import the list of founders to the membership database
29
-#     '''
30
-#     try:  # check if the file exists
31
-#         with open('import/test_founders.csv', 'r') as f:
32
-#             # store contents in tempfile
33
-#             content = tempfile.NamedTemporaryFile()
34
-#             content.write(f.read())
35
-#             content.seek(0)  # rewind to beginning
36
-
37
-#     except IOError, ioe:
38
-#         print ioe
39
-#         request.session.flash("file not found.", 'messages')
40
-#         return HTTPFound(request.route_url('dashboard_only'))
41
-
42
-#     # make sure we have a dummy entry in the db
43
-#     test = Membership.get_by_id(1)
44
-#     if isinstance(test, NoneType):
45
-#         print "need to get a dummy into the DB"
46
-#         from membership_utils import make_dummy_entries
47
-#         make_dummy_entries()
48
-#     else:
49
-#         print "ok, found someone."
50
-#         pass
51
-
52
-#     # reader for CSV files
53
-#     r = unicodecsv.reader(content.file, delimiter=',',
54
-#                           encoding='utf-8',
55
-#                           ##encoding='utf-8',
56
-#                           #encoding='iso-8859-2',
57
-#                           quoting=unicodecsv.QUOTE_ALL
58
-#                           )
59
-#     header = r.next()  # first line is the header.
60
-#     print("the header: %s" % header)
61
-#     #the header:  [u'Reihenfolge', u'Vorname', u'Nachname', u'Email',
62
-#     #              u'Adresse', u'Adresse 2', u'PLZ', u'Ort',
63
-#     #              u'Bundesland etc', u'Land', u'Anteile']
64
-#     # check it for compatibility
65
-#     try:
66
-#         assert header == [
67
-#             u'Reihenfolge', u'Vorname', u'Nachname',  # 0, 1, 2
68
-#             u'Email', u'Adresse',  # 3, 4
69
-#             u'Adresse 2', u'PLZ', u'Ort', u'Bundesland etc', u'Land',  # 5-9
70
-#             u'Anteile',  # 10
71
-#             u'Geburtsdatum',  # 11
72
-#         ]
73
-#     except AssertionError, ae:
74
-#         print ae
75
-#         print "the header of the CSV does not match what we expect"
76
-#         request.session.flash(
77
-#             "Import: header fields mismatch. NOT importing",
78
-#             'message_to_staff')
79
-#         return HTTPFound(request.route_url('toolbox'))
24
+@view_config(permission='manage',
25
+             route_name='import_founders')
26
+def import_founders(request):
27
+    '''
28
+    import the list of founders
29
+    '''
30
+    try:  # check if the file exists
31
+        with open(request.registry.settings['founders_importfile'], 'r') as f:
32
+            # store contents in tempfile
33
+            content = tempfile.NamedTemporaryFile()
34
+            content.write(f.read())
35
+            content.seek(0)  # rewind to beginning
80 36
 
81
-#     # count the datasets
82
-#     counter = 0
37
+    except IOError, ioe:
38
+        print ioe
39
+        request.session.flash("file not found.", 'messages')
40
+        return HTTPFound(request.route_url('dashboard_only'))
83 41
 
84
-#     while True:
85
-#         try:
86
-#             row = r.next()
87
-#         except:
88
-#             break
89
-#         counter += 1
90
-#         #print("=== row %s: %s" % (counter, row))
91
-#         #print("=== DEBUG: row[12] is: %s " % row[12])
92
-#         #print("=== DEBUG: row[15] is: %s " % row[15])
93
-#         # DEBUGGING (show datasets):
94
-#         #for i in range(row.__len__()):
95
-#         #    print('%s header: %s row: %s' % (i, header[i], row[i]))
96
-# # (Pdb) for i in range(row.__len__()): print('%s header: %s row: %s' % (i, header[i], row[i]))
97
-#         #import pdb
98
-#         #pdb.set_trace()
99
-#         #print(row[12] is True)
42
+    # reader for CSV files
43
+    r = unicodecsv.reader(content.file, delimiter=',',
44
+                          encoding='utf-8',
45
+                          #encoding='iso-8859-2',
46
+                          quoting=unicodecsv.QUOTE_ALL
47
+                          )
48
+    header = r.next()  # first line is the header.
49
+    print("the header: %s" % header)
50
+    try:
51
+        assert header == [
52
+            u'Reihenfolge', u'Vorname', u'Nachname',  # 0, 1, 2
53
+            u'Email', u'Adresse',  # 3, 4
54
+            u'Adresse 2', u'PLZ', u'Ort', u'Bundesland etc', u'Land',  # 5-9
55
+            u'Anteile',  # 10
56
+            u'Geburtsdatum',  # 11
57
+        ]
58
+    except AssertionError, ae:
59
+        print ae
60
+        print "the header of the CSV does not match what we expect"
61
+        request.session.flash(
62
+            "Import: header fields mismatch. NOT importing",
63
+            'message_to_staff')
64
+        return HTTPFound(request.route_url('toolbox'))
100 65
 
101
-#         import_member = Membership(
102
-#             firstname=row[1],
103
-#             lastname=row[2],
104
-#             email=row[3],
105
-#             password='None',
106
-#             address1=row[4],
107
-#             address2=row[5],
108
-#             postcode=row[6],
109
-#             city=row[7],
110
-#             #bundesland=row[8], drop it?
111
-#             country=row[9],
112
-#             locale=u'DE',
113
-#             date_of_birth=datetime.datetime.strptime(row[11], '%Y-%m-%d'),
114
-#             # (1970, 1, 1)
115
-#             #date_of_birth=datetime.datetime(1980, 1, 1),  # XXX
116
-#             #email_is_confirmed=True if (row[12] == 'True') else False,
117
-#             email_is_confirmed=True,
118
-#             #email_confirm_code=row[13],
119
-#             #date_of_submission=row[15],
120
-#             membership_type=u'normal',
121
-#             #member_of_colsoc=True if (row[17] == 'True') else False,
122
-#             member_of_colsoc=False,
123
-#             #name_of_colsoc=row[18],
124
-#             name_of_colsoc=u'',
125
-#             #num_shares=row[14],
126
-#             date_of_membership=datetime.datetime(2013, 9, 25),
127
-#         )
128
-#         shares = Shares(
129
-#             number=row[10],
130
-#             date_of_acquisition=datetime.datetime(2013, 9, 25),
131
-#             reference_code=u'GruendungHH'+str(counter),
132
-#             signature_received=True,
133
-#             signature_received_date=datetime.datetime(2013, 9, 25),
134
-#             payment_received=True,
135
-#             payment_received_date=datetime.datetime(2013, 9, 25),
136
-#         )
137
-#         import_member.shares += [shares]
138
-#         try:
139
-#             dbsession = DBSession
140
-#             dbsession.add(import_member)
141
-#             #dbsession.flush()
142
-#             request.session.flash(
143
-#                 "imported dataset %s" % (import_member.email_confirm_code),
144
-#                 'messages')
145
-#             #log.info(
146
-#             #    "%s imported dataset %s" % (
147
-#             #        authenticated_userid(request),
148
-#             #        import_member.email_confirm_code))
149
-#             #print('done with %s!' % counter)
150
-#         except ResourceClosedError, rce:
151
-#             # XXX can't catch this exception,
152
-#             # because it happens somwhere else, later, deeper !?!
153
-#             print "transaction was aborted/resource closed"
154
-#             print rce
155
-#             return {
156
-#                 'message': "tried import of dataset(s) with existing confirm code. ABORTED!"}
157
-#         except IntegrityError, ie:
158
-#             print "integrity error"
159
-#             dbsession.rollback()
160
-#             print ie
161
-#             if 'column email_confirm_code is not unique' in ie.message:
162
-#                 print("import of dataset %s failed, because the confirmation"
163
-#                       "code already existed" % counter)
164
-#                 return {
165
-#                     'message': "tried import of dataset(s) with existing confirm code. ABORTED!"}
166
-#         except StopIteration, si:
167
-#             print "stop iteration reached"
168
-#             print si
169
-#             return {'message': "file found, StopIteration reached."}
170
-#         #except:
171
-#         #    print "passing"
172
-#         #    pass
66
+    # count the datasets
67
+    counter = 0
173 68
 
174
-#     #print("done with all import steps, successful or not!")
175
-#     return HTTPFound(
176
-#         request.route_url(
177
-#             'membership_listing', number=0, orderby='id', order='asc'))
178
-#                 # _codes.append(row[13])
179
-#                 # print("the codes: %s" % str(_codes))
69
+    while True:
70
+        try:
71
+            row = r.next()
72
+        except:
73
+            break
74
+        counter += 1
180 75
 
181
-#     # except StopIteration, si:
182
-#     #     print si
183
-#     #     print("counter: %s" % counter)
184
-#     #     return {'message': "file found. import complete",
185
-#     #             #'codes': _codes,
186
-#     #             }
187
-# #return
76
+        import_member = C3sMember(
77
+            firstname=row[1],
78
+            lastname=row[2],
79
+            email=row[3],
80
+            password='None',
81
+            address1=row[4],
82
+            address2=row[5],
83
+            postcode=row[6],
84
+            city=row[7],
85
+            country=row[9],
86
+            locale=u'de',
87
+            date_of_birth=datetime.datetime.strptime(row[11], '%Y-%m-%d'),
88
+            email_is_confirmed=True,
89
+            email_confirm_code=u'GründungHH_{}'.format(counter),
90
+            date_of_submission=datetime.datetime(2013, 9, 25),
91
+            membership_type=u'unknown',
92
+            member_of_colsoc=False,
93
+            name_of_colsoc=u'',
94
+            num_shares=row[10],
95
+        )
96
+        import_member.signature_received = True
97
+        import_member.signature_received_date = datetime.datetime.strptime(
98
+            '2013-09-25', '%Y-%m-%d')
99
+        import_member.signature_confirmed = True
100
+        import_member.signature_confirmed_date = datetime.datetime.strptime(
101
+            '2013-09-25', '%Y-%m-%d')
102
+        import_member.payment_received = True
103
+        import_member.payment_received_date = datetime.date(2013, 9, 25)
104
+        import_member.payment_confirmed = True
105
+        import_member.payment_confirmed_date = datetime.date(2013, 9, 25)
106
+        import_member.date_of_submission = datetime.date(2013, 9, 25)
107
+        try:
108
+            dbsession = DBSession
109
+            dbsession.add(import_member)
110
+            request.session.flash(
111
+                "imported dataset %s" % (import_member.email_confirm_code),
112
+                'messages')
113
+        except ResourceClosedError, rce:
114
+            print "transaction was aborted/resource closed"
115
+            print rce
116
+            return {
117
+                'message': "resource closed error. ABORTED!"}
118
+        except IntegrityError, ie:
119
+            print "integrity error"
120
+            dbsession.rollback()
121
+            print ie
122
+            if 'column email_confirm_code is not unique' in ie.message:
123
+                print("import of dataset %s failed, because the confirmation"
124
+                      "code already existed" % counter)
125
+                return {
126
+                    'message': "integrity error. ABORTED!"}
127
+        except StopIteration, si:
128
+            print "stop iteration reached"
129
+            print si
130
+            return {'message': "file found, StopIteration reached."}
131
+    return HTTPFound(
132
+        request.route_url(
133
+            'dashboard', number=0, orderby='id', order='asc'))
188 134
 
189 135
 
190 136
 @view_config(permission='manage',

+ 1
- 0
development.ini View File

@@ -23,6 +23,7 @@ c3smembership.adminlogin = berries
23 23
 c3smembership.url = http://0.0.0.0:6543
24 24
 available_languages = de en # da es fr
25 25
 startnext_importfile = /the/path/to/c3sMembership/import/test_startnext.csv
26
+founders_importfile = /path/to/c3sMembership/import/test_founders.csv
26 27
 [server:main]
27 28
 use = egg:waitress#main
28 29
 #Paste#http

+ 4
- 0
import/test_founders.csv View File

@@ -0,0 +1,4 @@
1
+Reihenfolge,Vorname,Nachname,Email,Adresse,Adresse 2,PLZ,Ort,Bundesland etc,Land,Anteile,Geburtsdatum
2
+1,Albert,Anders,yes@c3s.cc,Musterstr 1,,40239,Düsseldorf,Nordrhein-Westfalen,Deutschland,5,1970-01-01
3
+2,Bert,Bayer,yes@c3s.cc,Muster Str 2,,12345,Musterstadt,Hessen,Deutschland,10,1970-01-01
4
+3,Charlotte,Chance,yes@c3s.cc,Muster Str 3,,1245,Musterstadt,,France,10,1970-01-01

+ 1
- 0
production.ini View File

@@ -23,6 +23,7 @@ mail.default_sender = noreply@c3s.cc
23 23
 
24 24
 available_languages = da de en es fr
25 25
 startnext_importfile = /the/path/to/c3sMembership/import/test_startnext.csv
26
+founders_importfile = /the/path/to/c3sMembership/import/test_founders.csv
26 27
 
27 28
 [server:main]
28 29
 use = egg:waitress#main

Loading…
Cancel
Save