|
37 | 37 | import os |
38 | 38 | import update_address as ua |
39 | 39 | from rorapi.management.commands.generaterorid import check_ror_id |
40 | | -from rorapi.management.commands.generaterorid import check_ror_id |
41 | 40 | from rorapi.management.commands.indexror import process_files |
42 | 41 | from django.core import management |
43 | 42 | import rorapi.management.commands.indexrordump |
| 43 | +from django.core.mail import EmailMultiAlternatives |
| 44 | +from django.utils.timezone import now |
| 45 | +from rorapi.v2.models import Client |
| 46 | +from rorapi.v2.serializers import ClientSerializer |
| 47 | + |
| 48 | +class ClientRegistrationView(APIView): |
| 49 | + def post(self, request, version='v2'): |
| 50 | + serializer = ClientSerializer(data=request.data) |
| 51 | + if serializer.is_valid(): |
| 52 | + client = serializer.save() |
| 53 | + |
| 54 | + subject = 'ROR API client ID' |
| 55 | + from_email = "ROR API Support <api@ror.org>" |
| 56 | + recipient_list = [client.email] |
| 57 | + |
| 58 | + html_content = self._get_html_content(client.client_id) |
| 59 | + text_content = self._get_text_content(client.client_id) |
| 60 | + |
| 61 | + msg = EmailMultiAlternatives(subject, text_content, from_email, recipient_list) |
| 62 | + msg.attach_alternative(html_content, "text/html") |
| 63 | + msg.send() |
| 64 | + |
| 65 | + return Response({'client_id': client.client_id}, status=status.HTTP_201_CREATED) |
| 66 | + |
| 67 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 68 | + |
| 69 | + def _get_text_content(self, client_id): |
| 70 | + return f""" |
| 71 | + Thank you for registering for a ROR API client ID! |
| 72 | +
|
| 73 | + Your ROR API client ID is: |
| 74 | + {client_id} |
| 75 | +
|
| 76 | + This client ID is not used for authentication or authorization, and is therefore not secret and can be sent as plain text. |
| 77 | +
|
| 78 | + In order to receive a rate limit of 2000 requests per 5 minute period, please include this client ID with your ROR API requests, in a custom HTTP header named Client-Id, for example: |
| 79 | +
|
| 80 | + curl -H "Client-Id: {client_id}" https://api.ror.org/organizations?query=oxford |
| 81 | +
|
| 82 | + Requests without a valid client ID are subject to a rate limit of 50 requests per 5 minute period. |
| 83 | +
|
| 84 | + We do not provide a way to recover or revoke a lost client ID. If you lose track of your client ID, please register a new client ID. For more information about ROR API client IDs, see https://ror.readme.io/docs/client-id |
| 85 | +
|
| 86 | + If you have questions, please see ROR documentation or contact us at support@ror.org |
| 87 | +
|
| 88 | + Cheers, |
| 89 | + The ROR Team |
| 90 | + support@ror.org |
| 91 | + https://ror.org |
| 92 | + """ |
| 93 | + |
| 94 | + |
| 95 | + def _get_html_content(self, client_id): |
| 96 | + return f""" |
| 97 | + <div style="font-family: Arial, sans-serif; line-height: 1.5;"> |
| 98 | + <p>Thank you for registering for a ROR API client ID!</p> |
| 99 | + <p><strong>Your ROR API client ID is:</strong></p> |
| 100 | + <pre style="background:#f4f4f4;padding:10px;">{client_id}</pre> |
| 101 | + <p>This client ID is not used for authentication or authorization, and is therefore not secret and can be sent as plain text.</p> |
| 102 | + <p>In order to receive a rate limit of <strong>2000 requests per 5 minute period</strong>, please include this client ID with your ROR API requests, in a custom HTTP header named <code>Client-Id</code>, for example:</p> |
| 103 | + <pre style="background:#f4f4f4;padding:10px;">curl -H "Client-Id: {client_id}" https://api.ror.org/organizations?query=oxford</pre> |
| 104 | + <p>Requests without a valid client ID are subject to a rate limit of 50 requests per 5 minute period.</p> |
| 105 | + <p>We do not provide a way to recover or revoke a lost client ID. If you lose track of your client ID, please register a new one.</p> |
| 106 | + <p>For more information about ROR API client IDs, see <a href="https://ror.readme.io/docs/client-id/">our documentation</a>.</p> |
| 107 | + <p>If you have questions, please see the ROR documentation or contact us at <a href="mailto:support@ror.org">support@ror.org</a>.</p> |
| 108 | + <p>Cheers,<br> |
| 109 | + The ROR Team<br> |
| 110 | + <a href="mailto:support@ror.org">support@ror.org</a><br> |
| 111 | + <a href="https://ror.org">https://ror.org</a></p> |
| 112 | + </div> |
| 113 | + """ |
| 114 | + |
| 115 | + |
| 116 | +class ValidateClientView(APIView): |
| 117 | + def get(self, request, client_id): |
| 118 | + client_exists = Client.objects.filter(client_id=client_id).exists() |
| 119 | + |
| 120 | + return Response({'valid': client_exists}, status=status.HTTP_200_OK) |
44 | 121 |
|
45 | 122 | class OurTokenPermission(BasePermission): |
46 | 123 | """ |
|
0 commit comments