|
1 | | -# For interacting with the database |
2 | | -from ..utilities import DbUtils |
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Activate Account |
3 | 3 |
|
4 | | -# For the user lookup |
5 | | -from django.contrib.auth.models import User |
| 4 | +""" |
6 | 5 |
|
| 6 | +from api.scripts.utilities import DbUtils |
| 7 | + |
| 8 | +# For url |
| 9 | +from django.conf import settings |
7 | 10 | # Responses |
8 | 11 | from rest_framework import status |
9 | 12 | from rest_framework.response import Response |
10 | 13 |
|
11 | | - |
12 | 14 | # Source: https://codeloop.org/django-rest-framework-course-for-beginners/ |
13 | 15 |
|
14 | | -def GET_activate_account( |
15 | | - username, |
16 | | - temp_identifier |
17 | | - ): |
18 | | - # Activate an account that is stored in the temporary table. |
| 16 | +def GET_activate_account(username,temp_identifier): |
| 17 | + """Activate Account |
19 | 18 |
|
20 | | - # Instantiate any necessary imports. |
21 | | - db = DbUtils.DbUtils() |
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + request: rest_framework.request.Request |
| 22 | + Django request object. |
22 | 23 |
|
23 | | - # Does the account associated with this e-mail already |
24 | | - # exist in either a temporary or a permanent user profile? |
25 | | - # if User.objects.filter(email=username).exists(): |
26 | | - # # TODO: This doesn't work since the username is the prefix of the email plus a random seed |
27 | | - # # (so we can't actually check). So this can only be true if there is a clash. |
28 | | - # # Account has already been activated. |
29 | | - # return (Response({ |
30 | | - # 'previously_activated': True, |
31 | | - # 'username' : username, |
32 | | - # 'status' : status.HTTP_208_ALREADY_REPORTED, |
33 | | - # }, |
34 | | - # status=status.HTTP_208_ALREADY_REPORTED) |
35 | | - # ) |
| 24 | + Returns |
| 25 | + ------- |
| 26 | + rest_framework.response.Response |
| 27 | + An HttpResponse that allows its data to be rendered into |
| 28 | + arbitrary media types. |
| 29 | + """ |
| 30 | + # Activate an account that is stored in the temporary table. |
| 31 | + |
| 32 | + db_utils = DbUtils.DbUtils() |
36 | 33 |
|
37 | 34 | # The account has not been activated, but does it exist |
38 | 35 | # in the temporary table? |
39 | | - if db.check_activation_credentials( |
| 36 | + if db_utils.check_activation_credentials( |
40 | 37 | p_app_label='api', |
41 | 38 | p_model_name='new_users', |
42 | 39 | p_email=username, |
43 | 40 | p_temp_identifier=temp_identifier |
44 | | - ) == 1: |
45 | | - |
| 41 | + ): |
46 | 42 |
|
47 | 43 | # The credentials match, so activate the account. |
48 | | - credential_try = db.activate_account( |
49 | | - p_email=username |
50 | | - ) |
| 44 | + credential_try = db_utils.activate_account(p_email=username) |
51 | 45 |
|
52 | 46 | if len(credential_try) > 0: |
53 | 47 | # Everything went fine. |
54 | | - return ( |
55 | | - Response( |
56 | | - { |
57 | | - 'activation_success': True, |
58 | | - 'username' : credential_try[0], |
59 | | - 'status': status.HTTP_201_CREATED, |
60 | | - |
61 | | - }, |
62 | | - status=status.HTTP_201_CREATED |
63 | | - ) |
64 | | - ) |
65 | | - |
66 | | - else: |
67 | | - |
68 | | - # The credentials weren't good. |
69 | | - return ( |
70 | | - Response( |
71 | | - { |
72 | | - 'activation_success': False, |
73 | | - 'status' : status.HTTP_403_FORBIDDEN |
74 | | - }, |
75 | | - status=status.HTTP_403_FORBIDDEN |
76 | | - ) |
77 | | - ) |
78 | | - |
79 | | - else: |
80 | | - |
81 | | - return ( |
82 | | - Response( |
83 | | - { |
84 | | - 'activation_success': False, |
85 | | - 'status' : status.HTTP_424_FAILED_DEPENDENCY |
86 | | - }, |
87 | | - status=status.HTTP_424_FAILED_DEPENDENCY |
88 | | - ) |
89 | | - ) |
| 48 | + return (Response({ |
| 49 | + 'activation_success': True, |
| 50 | + 'activation_url': settings.PUBLIC_HOSTNAME+'/login/', |
| 51 | + 'username': credential_try[0], |
| 52 | + 'status': status.HTTP_201_CREATED, |
| 53 | + }, status=status.HTTP_201_CREATED)) |
| 54 | + |
| 55 | + # The credentials weren't good. |
| 56 | + return(Response({ |
| 57 | + 'activation_success': False, |
| 58 | + 'status' : status.HTTP_403_FORBIDDEN}, |
| 59 | + status=status.HTTP_403_FORBIDDEN)) |
| 60 | + |
| 61 | + return(Response({ |
| 62 | + 'activation_success': False, |
| 63 | + 'status': status.HTTP_424_FAILED_DEPENDENCY}, |
| 64 | + status=status.HTTP_424_FAILED_DEPENDENCY)) |
0 commit comments