|
| 1 | +# |
| 2 | +# Copyright (c) 2015-2016 LabKey Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +""" |
| 17 | +unsupported.messageboard |
| 18 | +~~~~~~~~~~~~~~~~ |
| 19 | +WARNING: This module is not officially supported! Use at your own risk. |
| 20 | +
|
| 21 | +This module provides functions for interacting with Message Boards on the |
| 22 | +LabKey Server. |
| 23 | +""" |
| 24 | +from __future__ import unicode_literals |
| 25 | +from requests.exceptions import SSLError |
| 26 | +from labkey.utils import build_url |
| 27 | + |
| 28 | + |
| 29 | +def post_message(server_context, message_title, message_body, render_as, container_path=None): |
| 30 | + """ |
| 31 | + Post a message to a message board on a LabKey instance. |
| 32 | + :param server_context: A LabKey server context. See utils.create_server_context. |
| 33 | + :param message_title: The title of the message. |
| 34 | + :param message_body: The content of the message. |
| 35 | + :param render_as: The content of the message. |
| 36 | + :param container_path: Optional container path that can be used to override the server_context container path |
| 37 | + :return: Returns 1 if successful, 0 is post failed. |
| 38 | + """ |
| 39 | + # Build the URL for querying LabKey Server |
| 40 | + message_url = build_url(server_context, 'announcements', 'insert.api', container_path=container_path) |
| 41 | + |
| 42 | + message_data = { |
| 43 | + 'title': message_title, |
| 44 | + 'body': message_body, |
| 45 | + 'rendererType': render_as |
| 46 | + } |
| 47 | + |
| 48 | + session = server_context['session'] |
| 49 | + |
| 50 | + try: |
| 51 | + message_response = session.post(message_url, message_data) |
| 52 | + except SSLError as e: |
| 53 | + print("There was problem while attempting to submit the message to " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode())) |
| 54 | + print("The HTTP client error was: " + format(e)) |
| 55 | + return 0 |
| 56 | + |
| 57 | + return 1 |
0 commit comments