1 | # -*- coding: utf-8 -*- |
---|
2 | ################################################################################# |
---|
3 | # @file import_users.py |
---|
4 | # @brief Module that manage info about client (remote PCs), from Opengnsys API Rest |
---|
5 | # @warning None |
---|
6 | # @note Use: None |
---|
7 | # @license GNU GPLv3+ |
---|
8 | # @author David Fuertes, EUPT, University of Zaragoza. |
---|
9 | # Opengnsys Api Rest support provided by Juan Carlos García, EUPT, University of Zaragoza. |
---|
10 | # @version 1.1.0 - First version |
---|
11 | # @date 2019-15-11 |
---|
12 | ################################################################################# |
---|
13 | |
---|
14 | from ados import adoDB_users |
---|
15 | |
---|
16 | def insert(file_stream, db): |
---|
17 | datos = {} |
---|
18 | error = False |
---|
19 | info_already_insert = '' |
---|
20 | count_insert = 0 |
---|
21 | for line in file_stream.decode("utf-8").split('\n'): |
---|
22 | if ',' in line: |
---|
23 | separator_char = ',' |
---|
24 | if ';' in line: |
---|
25 | separator_char = ';' |
---|
26 | |
---|
27 | if '@' in line: |
---|
28 | line = line.strip().strip('\n').strip('"') |
---|
29 | datos['first_name'] = line.split(separator_char)[0] |
---|
30 | datos['last_name'] = line.split(separator_char)[1] |
---|
31 | datos['email'] = line.split(separator_char)[2] |
---|
32 | if '@' not in datos['email']: |
---|
33 | error = True |
---|
34 | info_insert = 'Error, usuario ' + datos['name'] + 'formato de email incorrecto' |
---|
35 | break |
---|
36 | |
---|
37 | datos['username'] = line.split(separator_char)[3] |
---|
38 | |
---|
39 | found = db(db.auth_user.username == datos['username']).count() |
---|
40 | #check user not exits |
---|
41 | if found == 0: |
---|
42 | adoDB_users.insert_user(db, datos['first_name'], datos['last_name'], |
---|
43 | datos['email'], datos['username']) |
---|
44 | count_insert = 1 + count_insert |
---|
45 | else: |
---|
46 | info_already_insert = '\n Algunos usuarios no se han importado porque ya existia su username.' |
---|
47 | |
---|
48 | info_insert = " " + str(count_insert) + ' usuarios insertados' + info_already_insert |
---|
49 | |
---|
50 | if error: |
---|
51 | db.rollback() |
---|
52 | return info_insert |
---|
53 | else: |
---|
54 | db.commit() |
---|
55 | return 'Importación realizada con exito.' + info_insert |
---|