def get_all(): """List all messages Returns: """ return ContactMessage.get_all()
def contact(request): """Contact form Parameters: request: Returns: Http response """ if request.method == "POST": contact_form = ContactForm(request.POST) if contact_form.is_valid(): # Call the API contact_message = ContactMessage( name=request.POST["name"], email=request.POST["email"], content=request.POST["message"], ) contact_message_api.upsert(contact_message) messages.add_message( request, messages.INFO, "Your message has been sent to the administrator.", ) return redirect(reverse("core_main_app_homepage")) else: contact_form = ContactForm() return render( request, "core_website_app/user/contact.html", context={"contact_form": contact_form}, )
def setUp(self): self.mock_account_request = ContactMessage(name="mock", content="mock", email="*****@*****.**") self.mock_data = { "name": "name", "content": "message", "email": "email" }
def post(request): """ Create a message Parameters: { "name": "name", "message": "message", "email": "email" } Args: request: HTTP request Returns: - code: 200 content: Contact message - code: 400 content: Validation error / missing parameters """ try: # Get parameters message_name = request.DATA['name'] message_email = request.DATA['email'] message_content = request.DATA['message'] try: # Create the message and insert it contact_message_object = ContactMessage( name=message_name, email=message_email, content=message_content ) new_contact_message = contact_message_api.upsert(contact_message_object) # Serialize the message serializer = ContactMessageSerializer(new_contact_message) if serializer.is_valid(): return Response(serializer.data, status=status.HTTP_200_OK) else: content = {'message': 'Serialization failed'} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)
def _create_contact_message(pk=1, name="message name", email="*****@*****.**", content="lorem ipsum dolor sit amet "): """ :param pk: :param name: :param email: :param content: :return: """ return ContactMessage(pk=pk, name=name, email=email, content=content)
def get(message_id): """Get a message Args: message_id: Returns: """ try: return ContactMessage.get_by_id(message_id) except Exception as e: logger.error(str(e)) raise exceptions.ApiError( "No message could be found with the given id.")
def _create_contact_message( name="name", email="*****@*****.**", content="message", ): """Create a contact message Args: name: email: content: Returns: """ return ContactMessage(name=name, email=email, content=content)
def delete(request): """ Delete a message :param request: :return: """ try: # Get parameters message_id = request.DATA['messageid'] try: contact_message = ContactMessage(pk=message_id) contact_message_api.delete(contact_message) return Response({}, status=status.HTTP_204_NO_CONTENT) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)
def delete(request): """ Delete a message Parameters: { "messageid": "message_id" } Args: request: HTTP request Returns: - code: 204 content: {} - code: 400 content: Missing parameters """ try: # Get parameters message_id = request.DATA['messageid'] try: contact_message = ContactMessage(pk=message_id) contact_message_api.delete(contact_message) return Response({}, status=status.HTTP_204_NO_CONTENT) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)
def post(request): """ Post a message :param request: :return: """ try: # Get parameters message_name = request.DATA['name'] message_email = request.DATA['email'] message_content = request.DATA['message'] try: # Create the message and insert it contact_message_object = ContactMessage(name=message_name, email=message_email, content=message_content) new_contact_message = contact_message_api.upsert( contact_message_object) # Serialize the message serializer = ContactMessageSerializer(new_contact_message) if serializer.is_valid(): return Response(serializer.data, status=status.HTTP_200_OK) else: content = {'message': 'Serialization failed'} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)