Skip to content

Commit

Permalink
Add copy to clipboard ability to API
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdiirh committed May 26, 2022
1 parent c6b3b4b commit 37ce337
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

urlpatterns = [
path('execute/', api_views.exec_command, name='exec-command'),
path('copy/', api_views.copy_to_clipboard, name='copy-to-clipboard'),
path('commands/', api_views.list_commands, name='list-of-commands'),
]
37 changes: 36 additions & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from utils.api.tools import save_received_request, api_response, get_request_data, get_search_query
from utils.core import http_status as sc
from utils.core.tools import execute_command
from utils.core.tools import execute_command, copy_content_to_clipboard, linux_package_installed
from utils.auth.http.decorators import login_required


Expand Down Expand Up @@ -106,3 +106,38 @@ def list_commands(request):
}

return api_response(request, data, commit=True)


@require_POST
@csrf_exempt
@login_required(api_endpoint=True)
def copy_to_clipboard(request):

if not linux_package_installed('xclip'):
return api_response(
request,
status_code=sc.HTTP_400_BAD_REQUEST,
message='xclip package is not installed'
)

data = get_request_data(request)
content = data.get('content')

if not content:
return api_response(
request,
status_code=sc.HTTP_400_BAD_REQUEST,
message='no content provided',
)

output = copy_content_to_clipboard(content)

status_code = sc.HTTP_200_OK if output else sc.HTTP_400_BAD_REQUEST

return api_response(
request,
data={'content': content},
status=output,
status_code=status_code,
)

13 changes: 13 additions & 0 deletions control/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from utils import generators
from utils.generators import stylers

from utils.core.tools import copy_content_to_clipboard, linux_package_installed

import string


Expand Down Expand Up @@ -70,6 +72,17 @@ def test_render_json_tool(self):
self.assertEqual(stylers.render_json(1), 1)


class TestTools(TestCase):

def test_copy_to_clipboard_tool(self):
if not linux_package_installed('xclip'):
return # stop test if xclip is not installed

for content in ['hello world', '*', 123, None, '\n', '']:
status = copy_content_to_clipboard(content)
self.assertEqual(status, True)


class TestMainPage(TestCase):

fixtures = ['test_fixtures/fixtures.json']
Expand Down
46 changes: 46 additions & 0 deletions utils/core/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import subprocess
import tempfile


def execute_command(command: str) -> [None, str]:
Expand All @@ -13,3 +15,47 @@ def execute_command(command: str) -> [None, str]:
"""

return os.popen(command).read()


def copy_content_to_clipboard(content):
"""
Copy content to clipboard
Args:
content (str): content to be copied
Returns:
bool: True if content is copied
"""

content = str(content)
with tempfile.NamedTemporaryFile(mode='w+t') as f:
f.write(content)
f.flush()
return_code = subprocess.call(
['xclip', '-d', ':0', '-selection', 'clipboard', f.name],
stdout=subprocess.PIPE,
)

status = True if return_code == 0 else False
return status


def linux_package_installed(package_name: str) -> bool:
"""
Check if a linux package is installed
Args:
package_name (str): package name
Returns:
bool: True if package is installed
"""

return_code = subprocess.call(
['which', package_name],
stdout=subprocess.PIPE,
)

status = True if return_code == 0 else False
return status

0 comments on commit 37ce337

Please sign in to comment.