class AddUser(APIView): user_service = UserService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(AddUser, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): raise Exception('Method not supported') def post(self, request, *args, **kwargs): request_data = request.data try: first_name = request_data.get('first_name') last_name = request_data.get('last_name') email_id = request_data.get('email_id') if not (first_name and email_id): raise MissingRequiredParameterInRequestException( 'Parameter first_name and email_id is mandatory') user = self.user_service.add_user(first_name, last_name, email_id) return self.api_response.prep_success_response( {'user_id': user.id}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class GetUserMailboxEmails(APIView): user_mailbox_content_service = UserMailboxContentService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(GetUserMailboxEmails, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): request_data = request.data try: user_id = kwargs.get('user_id') mailbox_id = kwargs.get('mailbox_id') if not (user_id and mailbox_id): raise MissingRequiredParameterInRequestException('Parameter user_id and mailbox_id is mandatory') mailbox_type = request_data.get('mailbox_type', 'default') if mailbox_type not in ['default', 'user_defined']: raise InvalidMailboxTypeInRequestException('Invalid mailbox type: {0} in request. Supported values: ' 'default, user_defined'.format(mailbox_type)) if mailbox_type == 'default': mails = self.user_mailbox_content_service.get_default_mailbox_mails(user_id, mailbox_id) else: mails = self.user_mailbox_content_service.get_user_defined_mailbox_mails(user_id, mailbox_id) return self.api_response.prep_success_response({'mails': mails}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class SaveComposedEmail(APIView): mail_content_service = MailContentService() user_mailbox_content_service = UserMailboxContentService() user_service = UserService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(SaveComposedEmail, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): raise Exception('Method not supported') def post(self, request, *args, **kwargs): request_data = request.data try: recipients = request_data.get('to') sender_user_id = kwargs.get('user_id') subject = request_data.get('subject') body = request_data.get('body') associated_mail_id = request_data.get('associated_mail_id') if not sender_user_id: raise MissingRequiredParameterInRequestException( "Parameter user_id is mandatory") if not (recipients or subject or body or associated_mail_id): raise MissingRequiredParameterInRequestException( "At least one parameter: " "to, subject, body or associated_mail_id is required") sender = self.user_service.get_user_by_id(sender_user_id) mail_content = self.mail_content_service.add_mail_content( sender, recipients, subject, body, associated_mail_id) self.user_mailbox_content_service.add_user_mailbox_content( sender, mail_content) return self.api_response.prep_success_response( {'mail_id': mail_content.id}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class SendEmail(APIView): user_mailbox_content_service = UserMailboxContentService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(SendEmail, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): raise Exception('Method not supported') def post(self, request, *args, **kwargs): try: user_mailbox_content_id = kwargs.get('mail_id') if not user_mailbox_content_id: raise MissingRequiredParameterInRequestException( "Parameter mail_id is mandatory") user_mailbox_content = \ self.user_mailbox_content_service.get_user_mailbox_content_by_id(user_mailbox_content_id) internal_recipients, external_recipients = \ self.user_mailbox_content_service.send_mail(user_mailbox_content) mail_sending_response = [] for internal_recipient in internal_recipients: mail_sending_response.append({ 'email_id': internal_recipient, 'status': 'Email sent', 'user_type': 'System user' }) for external_recipient in external_recipients: mail_sending_response.append({ 'email_id': external_recipient, 'status': 'Email sent', 'user_type': 'External user' }) data = {'action_response': mail_sending_response} return self.api_response.prep_success_response({'data': data}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class GetMailboxes(APIView): mailbox_service = MailboxService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(GetMailboxes, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): try: user_id = kwargs.get('user_id') if not user_id: raise MissingRequiredParameterInRequestException( 'Parameter user_id is mandatory') mailboxes = self.mailbox_service.get_all_mailboxes(user_id) return self.api_response.prep_success_response(mailboxes) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class ViewAndDeleteEmail(APIView): user_mailbox_content_service = UserMailboxContentService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(ViewAndDeleteEmail, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): try: user_mailbox_content_id = kwargs.get('mail_id') if not user_mailbox_content_id: raise MissingRequiredParameterInRequestException( 'Parameter mail_id is mandatory') mail_details = self.user_mailbox_content_service.get_mail_details( user_mailbox_content_id) return self.api_response.prep_success_response( {'mail': mail_details}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e)) def delete(self, request, *args, **kwargs): try: user_mailbox_content_id = kwargs.get('mail_id') if not user_mailbox_content_id: raise MissingRequiredParameterInRequestException( 'Parameter mail_id is mandatory') self.user_mailbox_content_service.delete_email_to_trash( user_mailbox_content_id) return self.api_response.prep_success_response( {'action_response': 'success'}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))
class ForwardEmail(APIView): mail_content_service = MailContentService() user_mailbox_content_service = UserMailboxContentService() api_response = APIResponse() @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(ForwardEmail, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): raise Exception('Method not supported') def post(self, request, *args, **kwargs): request_data = request.data try: recipients = request_data.get('to') subject = request_data.get('subject') user_mailbox_content_id = kwargs.get('mail_id') if not (recipients and user_mailbox_content_id): raise MissingRequiredParameterInRequestException( "Parameter to and mail_id are mandatory") user_mailbox_content = \ self.user_mailbox_content_service.get_user_mailbox_content_by_id(user_mailbox_content_id) if not subject: subject = 'Fwd: ' + user_mailbox_content.mail_content.subject new_mail_content = self.mail_content_service.add_mail_content( sender=user_mailbox_content.user, recipients=recipients, subject=subject, body=user_mailbox_content.mail_content.body, associated_mail_id=user_mailbox_content.mail_content.id) new_user_mailbox_content = \ self.user_mailbox_content_service.add_user_mailbox_content(user_mailbox_content.user, new_mail_content) internal_recipients, external_recipients = \ self.user_mailbox_content_service.send_mail(new_user_mailbox_content) data = { 'mail_id': new_user_mailbox_content.id, } mail_forwarding_response = [] for internal_recipient in internal_recipients: mail_forwarding_response.append({ 'email_id': internal_recipient, 'status': 'Email forwarded', 'user_type': 'System user' }) for external_recipient in external_recipients: mail_forwarding_response.append({ 'email_id': external_recipient, 'status': 'Email forwarded', 'user_type': 'External user' }) data['action_response'] = mail_forwarding_response return self.api_response.prep_success_response({'data': data}) except RabbitMailException as e: return self.api_response.rabbitmail_exception_error_response(e) except Exception as e: return self.api_response.internal_error_response(str(e))