|
1 | | -from kivy.uix.screenmanager import Screen |
| 1 | +from kivy.factory import Factory |
| 2 | +from kivy.app import App |
| 3 | +from kivy.properties import StringProperty |
2 | 4 | from kivy.lang import Builder |
3 | | -# from kivy.app import App |
| 5 | +import csv |
| 6 | +import os |
| 7 | +import json |
4 | 8 |
|
5 | 9 |
|
6 | | -class ScreenRegister(Screen): |
| 10 | +app = App.get_running_app() |
| 11 | + |
| 12 | + |
| 13 | +class ScreenRegister(Factory.Screen): |
| 14 | + |
| 15 | + _data = {} |
| 16 | + '''Holds the data in :attr:`data_file_dir` was processed or not. |
| 17 | + :attr:`_data` is a :type:`String`, defaults to False. |
| 18 | + ''' |
| 19 | + |
| 20 | + data_file_dir = StringProperty('data/registeration') |
| 21 | + '''This is the data dir where the registeration data is stored. |
| 22 | + All csv files should be present in this folder. |
| 23 | +
|
| 24 | + :attr:`data_file_dir` is a :class:`~kivy.properties.StringProperty` |
| 25 | +
|
| 26 | + defaults to `data/registeration/` |
| 27 | + ''' |
| 28 | + |
7 | 29 | Builder.load_string(''' |
| 30 | +#:import Factory kivy.factory.Factory |
| 31 | +<RSpinner@Spinner> |
| 32 | + background_normal: 'atlas://data/default/spinner' |
| 33 | + background_down: 'atlas://data/default/spinner' |
| 34 | + color: app.base_active_color |
| 35 | + background_color: app.base_active_color if self.state == 'normal' else (1, 1, 1, 1) |
| 36 | + option_cls: Factory.ActiveButton |
| 37 | +
|
8 | 38 | <ScreenRegister> |
9 | 39 | name: 'ScreenRegister' |
10 | | - data_file_dir: 'data/registeration' |
11 | | - GridLayout |
12 | | - size_hint: .5, None |
13 | | - height: self.minimum_height |
14 | | - pos_hint: {'center_x': .5, 'center_y': .5} |
| 40 | + on_enter: self._row = '' |
| 41 | + BoxLayout: |
| 42 | + orientation: 'vertical' |
| 43 | + pos_hint: {'center_x': .5} |
| 44 | + padding: dp(9), dp(9) |
15 | 45 | spacing: dp(9) |
16 | | - cols: 1 |
| 46 | + GridLayout |
| 47 | + size_hint: 1, None |
| 48 | + height: self.minimum_height |
| 49 | + spacing: dp(9) |
| 50 | + cols: 1 |
| 51 | + BackLabel |
| 52 | + text: 'Load Registeration Data' |
| 53 | + backcolor: app.base_active_color[:3] + [.5] |
| 54 | + BoxLayout |
| 55 | + size_hint_y: None |
| 56 | + height: dp(30) |
| 57 | + spacing: dp(9) |
| 58 | + BackLabel |
| 59 | + text: root.data_file_dir |
| 60 | + pos_hint: {'center_x': .5, 'center_y': .5} |
| 61 | + ActiveButton: |
| 62 | + height: dp(30) |
| 63 | + text: 'Change...' |
| 64 | + size_hint_x: None |
| 65 | + pos_hint: {'center_y': .5} |
| 66 | + on_release: root.show_file_chooser() |
| 67 | + # BackLabel |
| 68 | + # text: 'Restrict attendence from' |
| 69 | + # backcolor: app.base_active_color[:3] + [.5] |
| 70 | + # BoxLayout |
| 71 | + # size_hint_y: None |
| 72 | + # height: dp(30) |
| 73 | + # spacing: dp(9) |
| 74 | + # RSpinner: |
| 75 | + # id: rs1 |
| 76 | + # text: 'A' |
| 77 | + # values: [chr(x) for x in range(65,91)] |
| 78 | + # on_text: |
| 79 | + # if ord(args[1]) > ord(rs2.text):\ |
| 80 | + # self.text = 'A'; rs2.text = 'Z' |
| 81 | + # RSpinner: |
| 82 | + # id: rs2 |
| 83 | + # text: 'z' |
| 84 | + # values: [chr(x) for x in range(65,91)] |
| 85 | + # on_text: |
| 86 | + # if ord(args[1]) < ord(rs1.text):\ |
| 87 | + # self.text = 'Z'; rs1.text = 'A' |
17 | 88 | BoxLayout |
18 | 89 | size_hint_y: None |
19 | | - height: dp(45) |
20 | | - BackLabel |
21 | | - text: 'data file dir' |
| 90 | + height: dp(30) |
| 91 | + spacing: dp(9) |
| 92 | + TextInput: |
| 93 | + id: ti |
| 94 | + write_tab: False |
| 95 | + multiline: False |
| 96 | + background_active: 'atlas://data/default/ti_white' |
| 97 | + background_normal: 'atlas://data/default/ti_white' |
| 98 | + background_color: app.base_active_color |
| 99 | + hint_text: 'Registeration Number' |
| 100 | + input_type: 'number' |
| 101 | + input_filter: 'int' |
| 102 | + on_text_validate: bt_check.trigger_action() |
22 | 103 | ActiveButton: |
23 | | - text: root.data_file_dir |
24 | | - on_release: pass |
25 | | -
|
| 104 | + id: bt_check |
| 105 | + height: dp(30) |
| 106 | + text: "Check..." |
| 107 | + size_hint_x: None |
| 108 | + pos_hint: {'center_y': .5} |
| 109 | + on_release: root.check_attendee(ti.text) |
| 110 | + BackLabel |
| 111 | + text: 'Registeration Details' |
| 112 | + backcolor: app.base_active_color[:3] + [.5] |
| 113 | + ScrollView |
| 114 | + GridLayout |
| 115 | + padding: dp(9) |
| 116 | + size_hint: 1, None |
| 117 | + height: self.minimum_height |
| 118 | + spacing: dp(9) |
| 119 | + cols: 1 |
| 120 | + BackLabel |
| 121 | + id: lbl_name |
| 122 | + text: 'Name: ' |
| 123 | + BackLabel |
| 124 | + id: lbl_tshirt |
| 125 | + text: 'TShirt Size: ' |
| 126 | + BackLabel |
| 127 | + id: lbl_category |
| 128 | + text: 'Category: ' |
| 129 | + BackLabel |
| 130 | + id: lbl_tickettype |
| 131 | + text: 'Ticket Type: ' |
| 132 | + BoxLayout |
| 133 | + size_hint_y: None |
| 134 | + height: lbl_category.height |
| 135 | + BackLabel |
| 136 | + text: 'Registered ?' |
| 137 | + ImBut |
| 138 | + id: but_registered |
| 139 | + size_hint_x: None |
| 140 | + width: dp(54) |
| 141 | + color: 1, 1, 1, 1 |
| 142 | + source: 'atlas://data/default/close' |
| 143 | + on_release: if root._row: root.register_user(self) |
| 144 | + BoxLayout |
| 145 | + size_hint_y: None |
| 146 | + height: lbl_category.height |
| 147 | + BackLabel |
| 148 | + text: 'Swag Distributed ?' |
| 149 | + ImBut |
| 150 | + id: but_swag |
| 151 | + size_hint_x: None |
| 152 | + width: dp(54) |
| 153 | + color: 1, 1, 1, 1 |
| 154 | + source: 'atlas://data/default/close' |
| 155 | + on_release: if root._row: root.distribute_swag(self) |
| 156 | + BoxLayout |
| 157 | + size_hint_y: None |
| 158 | + height: lbl_category.height |
| 159 | + BackLabel |
| 160 | + text: 'T-Shirt Distributed ?' |
| 161 | + ImBut |
| 162 | + id: but_tshirt |
| 163 | + size_hint_x: None |
| 164 | + width: dp(54) |
| 165 | + color: 1, 1, 1, 1 |
| 166 | + source: 'atlas://data/default/close' |
| 167 | + on_release: if root._row: root.distribute_tshirt(self) |
| 168 | + ActiveButton |
| 169 | + text: 'Start Scanner' |
| 170 | + height: dp(30) |
| 171 | + on_release: |
| 172 | + from utils import scan_qr |
| 173 | + scan_qr(on_complete=root.check_attendee) |
26 | 174 | ''') |
27 | 175 |
|
28 | 176 | def on_enter(self): |
| 177 | + if not self._data: |
| 178 | + self.on_data_file_dir(self, self.data_file_dir) |
| 179 | + |
| 180 | + def show_file_chooser(self): |
| 181 | + # update data_file_dir here |
| 182 | + pop = Builder.load_string(''' |
| 183 | +Popup |
| 184 | + title: 'Select data flie dir' |
| 185 | + BoxLayout |
| 186 | + padding: dp(9) |
| 187 | + spacing: dp(20) |
| 188 | + orientation: 'vertical' |
| 189 | + FileChooserIconView |
| 190 | + id: fl |
| 191 | + filters: ['*.'] |
| 192 | + dirselect: True |
| 193 | + #filter_dirs: True |
| 194 | + BoxLayout |
| 195 | + spacing: dp(9) |
| 196 | + size_hint_y: None |
| 197 | + height: dp(36) |
| 198 | + ActiveButton |
| 199 | + text: 'Select' |
| 200 | + on_release: |
| 201 | + app.screenregister.data_file_dir = fl.selection[0] |
| 202 | + root.dismiss() |
| 203 | + ActiveButton |
| 204 | + text: 'Cancel' |
| 205 | + on_release: root.dismiss() |
| 206 | +''') |
| 207 | + pop.open() |
| 208 | + |
| 209 | + def on_data_file_dir(self, instance, data_file_dir): |
29 | 210 | # read csv file here |
30 | | - # import csv |
31 | | - # with open('/Users/quanon/Downloads/buyers-172227.csv.xls', 'rb') as csvfile: |
32 | | - # spamreader = csv.reader(csvfile, dialect='excel') |
33 | | - # for row in spamreader: |
34 | | - # print(','.join(row)) |
35 | | - # break |
36 | | - pass |
37 | | - |
38 | | - def register_attendee(self): |
39 | | - pass |
40 | | - |
41 | | - def scan_qrcode(self): |
42 | | - pass |
| 211 | + _data = {} |
| 212 | + for fl in os.listdir(data_file_dir): |
| 213 | + if not fl.endswith('.csv'): |
| 214 | + continue |
| 215 | + with open(os.sep.join([data_file_dir, fl]), 'rb') as csvfile: |
| 216 | + first_row = True |
| 217 | + spamreader = csv.reader(csvfile, dialect='excel') |
| 218 | + ridx = 0 |
| 219 | + for row in spamreader: |
| 220 | + if first_row: |
| 221 | + first_row = False |
| 222 | + idx = row.index('Registration No') |
| 223 | + continue |
| 224 | + ridx = row[idx] |
| 225 | + _data[ridx] = {} |
| 226 | + _data[ridx]['data'] = row |
| 227 | + self._data = _data |
| 228 | + |
| 229 | + def check_attendee(self, attendee_data): |
| 230 | + self.ids.ti.text = attendee_data |
| 231 | + _data = self._data |
| 232 | + if attendee_data not in _data: |
| 233 | + self._record_not_found() |
| 234 | + return |
| 235 | + # Records Found |
| 236 | + self._show_record(_data[attendee_data]) |
| 237 | + |
| 238 | + def _record_not_found(self): |
| 239 | + # clear all data |
| 240 | + self.ids.lbl_name.text = 'Record not found' |
| 241 | + self.ids.lbl_name.backcolor = app.base_active_bright[:3] + [.3] |
| 242 | + self.ids.lbl_name.text = 'Name: ' |
| 243 | + self.ids.lbl_tshirt.text = 'T-Shirt Size: ' |
| 244 | + self.ids.lbl_category.text = 'Category: ' |
| 245 | + self.ids.lbl_tickettype.text = 'Ticket Type: ' |
| 246 | + self.ids.but_swag.source = 'atlas://data/default/close' |
| 247 | + self.ids.but_registered.source = 'atlas://data/default/close' |
| 248 | + self.ids.but_tshirt.source = 'atlas://data/default/close' |
| 249 | + self._row = '' |
| 250 | + |
| 251 | + def _show_record(self, record): |
| 252 | + # display the data in record on screen |
| 253 | + self.ids.lbl_name.backcolor = app.base_inactive_color[:3] + [.3] |
| 254 | + self._row = row = record['data'] |
| 255 | + self.ids.lbl_name.text = 'Name: {}'.format(row[3]) |
| 256 | + self.ids.lbl_tshirt.text = 'T-Shirt Size: {}'.format(row[10]) |
| 257 | + self.ids.lbl_category.text = 'Category: {}'.format(row[16]) |
| 258 | + self.ids.lbl_tickettype.text = 'Ticket Type: {}'.format(row[6]) |
| 259 | + # Check and display if record is registered? |
| 260 | + self._check_registeration(row[15]) |
| 261 | + |
| 262 | + def _check_registeration(self, reg_no): |
| 263 | + self.ids.but_swag.source = 'atlas://data/default/close' |
| 264 | + self.ids.but_registered.source = 'atlas://data/default/close' |
| 265 | + self.ids.but_tshirt.source = 'atlas://data/default/close' |
| 266 | + |
| 267 | + # check if file exists |
| 268 | + fname = self.data_file_dir + '/final_registerations.json' |
| 269 | + if not os.path.isfile(fname): |
| 270 | + return |
| 271 | + |
| 272 | + with open(fname, 'rb') as fn: |
| 273 | + data = fn.read() |
| 274 | + if not data: |
| 275 | + return |
| 276 | + jdata = json.loads(data) |
| 277 | + reg_data = jdata.get(reg_no, None) |
| 278 | + if not reg_data: |
| 279 | + return |
| 280 | + # record exists, update registeration details now |
| 281 | + if 'registered' in reg_data: |
| 282 | + self.ids.but_registered.source = 'atlas://data/default/' +\ |
| 283 | + ('tick' if reg_data['registered'] else 'close') |
| 284 | + if 'swag' in reg_data: |
| 285 | + self.ids.but_swag.source = 'atlas://data/default/' +\ |
| 286 | + ('tick' if reg_data['swag'] else 'close') |
| 287 | + if 'tshirt' in reg_data: |
| 288 | + self.ids.but_tshirt.source = 'atlas://data/default/' +\ |
| 289 | + ('tick' if reg_data['tshirt'] else 'close') |
| 290 | + |
| 291 | + def _register(self, but, data): |
| 292 | + row = self._row |
| 293 | + if not row: |
| 294 | + return |
| 295 | + if but.source.endswith('tick'): |
| 296 | + return |
| 297 | + |
| 298 | + but.source = 'atlas://data/default/tick' |
| 299 | + fname = self.data_file_dir + '/final_registerations.json' |
| 300 | + jdata = {} |
| 301 | + r5 = row[15] |
| 302 | + with open(fname, 'rb+' if os.path.isfile(fname) else 'wb+') as fn: |
| 303 | + try: |
| 304 | + jdata = json.load(fn) |
| 305 | + except ValueError: |
| 306 | + pass |
| 307 | + if not jdata.get(r5, None): |
| 308 | + jdata[r5] = {} |
| 309 | + jdata[r5][data] = True |
| 310 | + with open(fname, 'wb') as fn: |
| 311 | + json.dump(jdata, fn) |
| 312 | + |
| 313 | + def register_user(self, but): |
| 314 | + self._register(but, 'registered') |
| 315 | + |
| 316 | + def distribute_swag(self, but): |
| 317 | + self._register(but, 'swag') |
| 318 | + |
| 319 | + def distribute_tshirt(self, but): |
| 320 | + self._register(but, 'tshirt') |
0 commit comments