added default roles to user: shop_user, coop_manager
This commit is contained in:
@@ -5,12 +5,13 @@ from factory import LazyAttribute, SubFactory
|
||||
from factory.fuzzy import FuzzyText, FuzzyChoice, FuzzyDateTime, FuzzyDate, FuzzyDecimal
|
||||
from factory.django import DjangoModelFactory
|
||||
|
||||
ROLES = ('SHOP_USER', 'COOP_MANAGER')
|
||||
|
||||
class CustomUserFactory(DjangoModelFactory):
|
||||
|
||||
email = FuzzyText(length=6, suffix="@mail.com")
|
||||
full_name = FuzzyText(length=15, prefix="TestName_")
|
||||
role = FuzzyText(length=15, prefix="TestPosition_")
|
||||
role = FuzzyChoice(ROLES)
|
||||
notify = FuzzyChoice(choices=(True, False))
|
||||
provider = FuzzyText(length=15, prefix="TestProvider_")
|
||||
email_verified = True
|
||||
|
||||
@@ -37,9 +37,17 @@ class UserManager(BaseUserManager):
|
||||
|
||||
class CustomUser(AbstractBaseUser, PermissionsMixin):
|
||||
|
||||
SHOP_USER = 'SHOP_USER'
|
||||
COOP_MANAGER = 'COOP_MANAGER'
|
||||
|
||||
ROLES = (
|
||||
(SHOP_USER, 'Shop User'),
|
||||
(COOP_MANAGER, 'Coop Manager'),
|
||||
)
|
||||
|
||||
email = models.EmailField('Dirección de email', unique=True)
|
||||
full_name = models.CharField('Nombre completo', max_length=100, blank=True)
|
||||
role = models.CharField('Rol', max_length=100, blank=True, null=True)
|
||||
role = models.CharField('Rol', choices=ROLES, default=SHOP_USER, max_length=100, blank=True, null=True)
|
||||
notify = models.BooleanField('Notificar', default=False, null=True)
|
||||
provider = models.CharField('Proveedor', max_length=1000, blank=True, null=True) # red social de registro
|
||||
email_verified = models.BooleanField('Email verificado', default=False, null=True)
|
||||
|
||||
@@ -1,18 +1,35 @@
|
||||
import csv
|
||||
import logging
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.generics import UpdateAPIView
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
|
||||
from . import models
|
||||
from . import serializers
|
||||
|
||||
from back_latienda.permissions import CustomUserPermissions, YourOwnUserPermissions
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
# Create your views here.
|
||||
|
||||
logging.basicConfig(
|
||||
filename='logs/csv-load.log',
|
||||
filemode='w',
|
||||
format='%(levelname)s:%(message)s',
|
||||
level=logging.INFO,
|
||||
)
|
||||
|
||||
|
||||
class CustomUserViewSet(viewsets.ModelViewSet):
|
||||
|
||||
model = models.CustomUser
|
||||
@@ -62,3 +79,31 @@ class UpdateUserView(UpdateAPIView):
|
||||
queryset = model.objects.all()
|
||||
permission_classes = (YourOwnUserPermissions,)
|
||||
serializer_class = serializers.UpdateUserSerializer
|
||||
|
||||
|
||||
@api_view(['POST',])
|
||||
@permission_classes([IsAdminUser,])
|
||||
def load_coop_managers(request):
|
||||
"""Read CSV file being received
|
||||
Parse it to create users and related companies
|
||||
"""
|
||||
csv_file = request.FILES["csv_file"]
|
||||
if csv_file.name.endswith('.csv') is not True:
|
||||
logging.error(f"File {csv_file.name} is not a CSV file")
|
||||
return Response({"errors":{"details": "File is not CSV type"}})
|
||||
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
csv_reader = csv.DictReader(csv_file, delimiter=',')
|
||||
for row in csv_reader:
|
||||
email = row['email']
|
||||
cif = row['cif']
|
||||
company_name = row['nombre-coop']
|
||||
short_name = row['nombre-corto']
|
||||
url = row['url']
|
||||
shop = row['es-tienda']
|
||||
|
||||
coop = None
|
||||
coop_user = User.objects.create_user(email=email, company=coop, role='COOP_MANAGER')
|
||||
|
||||
|
||||
return Response
|
||||
|
||||
Reference in New Issue
Block a user