Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
R
red-ci-cd
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
red-group-test
red-ci-cd
Commits
7a8bddaa
Commit
7a8bddaa
authored
Nov 08, 2019
by
John Red Medrano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added batch upload of users
parent
857fceef
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
214 additions
and
2 deletions
+214
-2
app/applicationlayer/management/account/views.py
app/applicationlayer/management/account/views.py
+151
-1
app/applicationlayer/master/user_type/views.py
app/applicationlayer/master/user_type/views.py
+1
-1
app/entities/migrations/0028_application_excel_code.py
app/entities/migrations/0028_application_excel_code.py
+19
-0
app/entities/migrations/0029_userhistory.py
app/entities/migrations/0029_userhistory.py
+31
-0
app/entities/models.py
app/entities/models.py
+12
-0
No files found.
app/applicationlayer/management/account/views.py
View file @
7a8bddaa
import
copy
import
copy
import
threading
import
threading
import
pandas
as
pd
import
csv
import
io
from
app.entities
import
enums
from
app.entities
import
enums
from
django.db
import
transaction
from
django.db
import
transaction
from
app.helper
import
decorators
from
app.helper
import
decorators
...
@@ -13,7 +16,8 @@ from rest_framework.response import Response
...
@@ -13,7 +16,8 @@ from rest_framework.response import Response
from
app.applicationlayer.utils
import
model_to_dict
from
app.applicationlayer.utils
import
model_to_dict
from
app.entities.models
import
(
from
app.entities.models
import
(
User
,
EntityLog
,
PasswordReset
,
Application
,
UserImage
,
User
,
EntityLog
,
PasswordReset
,
Application
,
UserImage
,
ChangeRequestFormHeader
,
ChangeRequestTemplateHeader
ChangeRequestFormHeader
,
ChangeRequestTemplateHeader
,
AllowedCompany
,
Company
,
Department
,
UserHistory
)
)
from
app.helper.decorators
import
rms
,
error_safe
from
app.helper.decorators
import
rms
,
error_safe
from
django.contrib.auth.hashers
import
make_password
from
django.contrib.auth.hashers
import
make_password
...
@@ -23,6 +27,7 @@ from rest_framework.filters import SearchFilter, OrderingFilter
...
@@ -23,6 +27,7 @@ from rest_framework.filters import SearchFilter, OrderingFilter
from
app.applicationlayer.management.account.table_filters
import
AccountFilterset
from
app.applicationlayer.management.account.table_filters
import
AccountFilterset
from
app.applicationlayer.management.account
import
serializer
from
app.applicationlayer.management.account
import
serializer
from
app.helper.file_manager
import
FileHelper
from
app.helper.file_manager
import
FileHelper
from
django.db
import
IntegrityError
from
app.applicationlayer.utils
import
(
from
app.applicationlayer.utils
import
(
CustomPagination
,
status_message_response
,
log_save
,
CustomPagination
,
status_message_response
,
log_save
,
main_threading
main_threading
...
@@ -228,6 +233,151 @@ class UserViewSet(viewsets.ModelViewSet):
...
@@ -228,6 +233,151 @@ class UserViewSet(viewsets.ModelViewSet):
)
)
# @rms.user_create
@
action
(
detail
=
False
,
methods
=
[
'put'
],
url_path
=
'batch-upload'
,
name
=
"upload User"
)
# @decorators.error_safe
@
transaction
.
atomic
def
BatchUpload
(
self
,
request
):
csv_file
=
request
.
FILES
[
'file'
]
df
=
pd
.
read_csv
(
csv_file
,
sep
=
','
,
skiprows
=
0
)
logged_user_type
=
request
.
user
.
user_type
logged_user_company
=
request
.
user
.
department
.
company
.
name
logged_user_department
=
request
.
user
.
department
.
name
for
data
,
keys
in
df
.
iterrows
():
if
logged_user_type
==
'CUA'
:
user_department
=
Department
.
objects
.
filter
(
Q
(
name__icontains
=
keys
[
'department'
])
&
Q
(
company__name__icontains
=
logged_user_company
)
)
.
first
()
if
user_department
==
None
:
msg
=
f
"company is not the same with the logged user at row {data + 2}"
return
Response
(
{
"message"
:
msg
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
elif
logged_user_type
==
'DUA'
:
user_department
=
Department
.
objects
.
get
(
name__icontains
=
logged_user_department
)
elif
logged_user_type
==
'USR'
:
return
Response
(
{
"message"
:
"Logged User is not allowed"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
else
:
user_department
=
Department
.
objects
.
get
(
name__icontains
=
keys
[
'department'
]
)
default_app
=
Application
.
objects
.
filter
(
excel_code
=
keys
[
'default_app'
]
)
.
first
()
# print(default_app)
try
:
users
=
{
"username"
:
keys
[
'username'
],
"name"
:
keys
[
'name'
],
"department"
:
user_department
,
"email"
:
keys
[
'email'
],
"contact_no"
:
keys
[
'contact_no'
],
"default_app"
:
default_app
}
current_user
=
User
.
objects
.
create
(
**
users
)
except
IntegrityError
as
e
:
return
Response
(
{
"message"
:
f
"Record already exist at row {data + 2}"
},
status
=
status
.
HTTP_201_CREATED
)
password
=
User
.
objects
.
make_random_password
(
length
=
10
)
password_hash
=
make_password
(
password
)
current_user
.
password
=
password_hash
current_user
.
save
()
app
=
Application
.
objects
.
filter
(
excel_code__in
=
keys
[
'application'
]
.
split
(
','
)
)
update_user
=
current_user
.
application
.
set
(
app
)
for
instance
in
keys
[
'privilege'
]
.
split
(
';'
):
privilege_list
=
instance
.
split
(
','
)
this_company
=
Company
.
objects
.
filter
(
name__icontains
=
privilege_list
[
0
]
)
.
first
()
this_department
=
Department
.
objects
.
filter
(
name__icontains
=
privilege_list
[
1
]
)
.
first
()
if
privilege_list
[
2
]
==
0
:
privilege_list
[
2
]
=
False
else
:
privilege_list
[
2
]
=
True
if
privilege_list
[
3
]
==
0
:
privilege_list
[
3
]
=
False
else
:
privilege_list
[
3
]
=
True
if
privilege_list
[
4
]
==
0
:
privilege_list
[
4
]
=
False
else
:
privilege_list
[
4
]
=
True
current_user
=
User
.
objects
.
get
(
id
=
current_user
.
id
)
try
:
privilege_object
=
{
"id_number"
:
current_user
,
"company_pivot"
:
this_company
,
"group_pivots"
:
this_department
,
"create_change_request"
:
privilege_list
[
2
],
"create_change_request_template"
:
privilege_list
[
3
],
"view_all_change_request"
:
privilege_list
[
4
]
}
AllowedCompany
.
objects
.
create
(
**
privilege_object
)
except
IntegrityError
as
e
:
return
Response
(
{
"message"
:
f
"Duplicate user privilege at row {data + 2}"
},
status
=
status
.
HTTP_201_CREATED
)
del
users
[
'department'
]
del
users
[
'contact_no'
]
del
users
[
'default_app'
]
users
[
'password'
]
=
password
UserHistory
.
objects
.
create
(
**
users
)
send_mail
=
UserHistory
.
objects
.
filter
(
sent
=
False
)
.
values
()
for
send
in
send_mail
:
admin_email
=
request
.
user
.
email
args
=
[
send
[
'name'
],
send
[
'username'
],
send
[
'password'
],
send
[
'email'
],
admin_email
]
main_threading
(
args
,
sender
.
account_created
)
send_mail
.
update
(
sent
=
True
)
return
Response
(
{
"message"
:
"File already uploaded"
},
status
=
status
.
HTTP_201_CREATED
)
@
action
(
detail
=
True
,
@
action
(
detail
=
True
,
methods
=
[
'put'
],
methods
=
[
'put'
],
url_path
=
'reset-password'
,
url_path
=
'reset-password'
,
...
...
app/applicationlayer/master/user_type/views.py
View file @
7a8bddaa
...
@@ -25,7 +25,7 @@ class UserTypeViewSet(APIView):
...
@@ -25,7 +25,7 @@ class UserTypeViewSet(APIView):
"code"
:
"200"
,
"code"
:
"200"
,
"status"
:
"success"
,
"status"
:
"success"
,
"message"
:
"list of user types"
,
"message"
:
"list of user types"
,
"results"
:
"results"
:
# [
# [
{
{
enums_super_key
:
enums_super
,
enums_super_key
:
enums_super
,
...
...
app/entities/migrations/0028_application_excel_code.py
0 → 100644
View file @
7a8bddaa
# Generated by Django 2.2 on 2019-11-08 14:03
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'entities'
,
'0027_assetgroup'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'application'
,
name
=
'excel_code'
,
field
=
models
.
CharField
(
default
=
'rms'
,
max_length
=
255
),
preserve_default
=
False
,
),
]
app/entities/migrations/0029_userhistory.py
0 → 100644
View file @
7a8bddaa
# Generated by Django 2.2 on 2019-11-08 15:04
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'entities'
,
'0028_application_excel_code'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'UserHistory'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'created'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
(
'createdby'
,
models
.
CharField
(
max_length
=
255
)),
(
'modified'
,
models
.
DateTimeField
(
auto_now
=
True
)),
(
'modifiedby'
,
models
.
CharField
(
max_length
=
255
)),
(
'name'
,
models
.
CharField
(
max_length
=
255
)),
(
'username'
,
models
.
CharField
(
max_length
=
255
,
unique
=
True
)),
(
'email'
,
models
.
EmailField
(
max_length
=
255
)),
(
'password'
,
models
.
CharField
(
max_length
=
255
)),
(
'sent'
,
models
.
BooleanField
(
default
=
False
)),
],
options
=
{
'db_table'
:
'user_history'
,
},
),
]
app/entities/models.py
View file @
7a8bddaa
...
@@ -27,6 +27,7 @@ class Application(AuditClass):
...
@@ -27,6 +27,7 @@ class Application(AuditClass):
max_length
=
255
max_length
=
255
)
)
name
=
models
.
CharField
(
unique
=
True
,
max_length
=
255
)
name
=
models
.
CharField
(
unique
=
True
,
max_length
=
255
)
excel_code
=
models
.
CharField
(
max_length
=
255
)
class
Meta
:
class
Meta
:
db_table
=
'applications'
db_table
=
'applications'
...
@@ -213,6 +214,17 @@ class RolePermission(AuditClass):
...
@@ -213,6 +214,17 @@ class RolePermission(AuditClass):
db_table
=
'role_permissions'
db_table
=
'role_permissions'
class
UserHistory
(
AuditClass
):
name
=
models
.
CharField
(
max_length
=
255
)
username
=
models
.
CharField
(
max_length
=
255
,
unique
=
True
)
email
=
models
.
EmailField
(
max_length
=
255
,
unique
=
False
)
password
=
models
.
CharField
(
max_length
=
255
)
sent
=
models
.
BooleanField
(
default
=
False
)
class
Meta
:
db_table
=
'user_history'
class
User
(
AbstractUser
):
class
User
(
AbstractUser
):
application
=
models
.
ManyToManyField
(
application
=
models
.
ManyToManyField
(
Application
Application
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment