class OrganizationRegistration(generics.CreateAPIView):
queryset = Organization.objects.all()
permission_classes = (permissions.AllowAny,)
serializer_class = OrgRegistrationSerializer
"""
{
"admin":{
"first_name":"Cindy",
"last_name":"Georgia",
"password":"password",
},
"org":{
"name":"ARIZONA LAW SOCIETY",
"short_name":"ALS",
"tag_line":"ALS",
"company_phone": "+25475533222",
"po_box": "200",
"location":"NAKURU",
}
}
"""
def post(self, request, format=None, *args, **kwargs):
try:
admins = Group.objects.get(name__iexact='admin')
admin_role = Role.objects.get(name__iexact="admin")
except:
# The admin group is
admins = Group.objects.create(name="admin")
admin_role = Role.objects.create(name="admin")
# Validate USer and Org
if Organization.objects.filter(name__iexact=request.data['org']['name']).exists() is True:
res = {"data": None, "msg":"Organization with that name already registered","success": False}
return Response(data=res,status=status.HTTP_400_BAD_REQUEST)
if User.objects.filter(username__iexact=request.data['admin']['username']).exists() is True:
res = {"data": None, "msg":"User with that name already registered.","success": False}
return Response(data=res,status=status.HTTP_400_BAD_REQUEST)
try:
admin = User.objects.create(
username=request.data['admin']['username'],
email=request.data['admin']['email'],
first_name=request.data['admin']['first_name'],
last_name=request.data['admin']['last_name'],
)
location_name = request.data['org']['location'].upper()
location_obj, _ = Location.objects.get_or_create(name=location_name)
# Create Random Password
password = User.objects.make_random_password(length=10)
admin.set_password(password)
admin.save()
admin.groups.add(admins)
admin.roles.add(admin_role)
except Exception as e:
return Response(data={"msg":str(e),"success":False, "data": None},status=status.HTTP_400_BAD_REQUEST)
try:
first_file_logo = request.data['org']['first_logo']
second_file_logo = request.data['org']['second_logo']
third_file_logo = request.data['org']['third_logo']
org = Organization.objects.create(
name=request.data['org']['name'],
email=request.data['org']['email'],
location=request.data['org']['location'],
short_name = request.data['org']['short_name'],
tag_line = request.data['org']['tag_line'],
company_phone = request.data['org']['company_phone'],
po_box = request.data['org']['po_box'],
first_logo = first_file_logo,
second_logo =second_file_logo ,
third_logo = third_file_logo
)
admin.save()
# add the user creating the Organization to admins by DEFAULT
# NOTE:
# all the other normaall users will be added normally i.e
admins.save()
org.admin.add(admin)
org.users.add(admin)
admin.is_active = False
admin.save()
payload = jwt_payload_handler(admin, org)
jwt_token = jwt_encode_handler(payload)
support_email = settings.SUPPORT_EMAIL
# # FIXME:
# refactor this code inorder to have its own function
path = get_full_path(request)
path = settings.BASE_URL
subject = 'Welcome onboard E-Voting'
org_html_message = loader.render_to_string('authenticate/org-confirmation.html')
admin_html_message = loader.render_to_string('authenticate/admin-confirmation.html')
# if the org is regestering themselves the org id will alway be zero
admin_message_string = strip_tags(admin_html_message)
org_message_string = strip_tags(org_html_message)
admin_ctx = {
'name': admin.username,
'password': password,
'email': admin.email,
'link': '{}registration/confirmation/{}/'.format(path, jwt_token.decode("utf-8"))
}
org_ctx = {
'email': org.email,
'link': '{}registration/confirmation/{}/'.format(path, jwt_token.decode("utf-8"))
}
admin_html_content = loader.render_to_string('authenticate/admin-confirmation.html', admin_ctx)
org_html_content = loader.render_to_string('authenticate/org-confirmation.html', org_ctx)
admin_message = {
"html": admin_html_content,
"text": admin_message_string
}
org_message = {
"html": org_html_content,
"text": org_message_string
}
# SEND BOTH MAIL TO ACTIVATE USER AND ORG
send_mail_to_org = send_credentials.delay(
'Welcome onboard E-Vote: Organization Confirmation',
org_message,
org.email
)
send_mail_to_admin = send_credentials.delay(
'Welcome onboard E-Vote: Admin Confirmation',
admin_message,
admin.email
)
serializer = OrgRegSerializer(org)
res = {"msg": "Check personal and organization emails, NOTE: finish org registration first!!!", "success": True, "data": serializer.data}
return Response(
data=res,
status=status.HTTP_201_CREATED
)
except Exception as e:
return Response(data={"msg":str(e),"success":False, "data": None},status=status.HTTP_400_BAD_REQUEST)