Skip to content

Commit

Permalink
Merge pull request #91 from OpenDataServices/rhiaro/90-delete
Browse files Browse the repository at this point in the history
utils: function to delete project, fixes #90
  • Loading branch information
rhiaro committed Mar 11, 2021
2 parents f4b9809 + 3106800 commit d4987dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements_dev.txt
- name: Start Redis
uses: supercharge/redis-github-action@1.2.0
with:
redis-version: 6
- name: Run tests
run: coverage run --source='./standards_lab' ./standards_lab/manage.py test ./standards_lab

Expand Down
21 changes: 21 additions & 0 deletions standards_lab/utils/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ def create_new_project(project_name, json_format=False):
return True, json.dumps(project)

return True, project


def delete_project(project_name):
"""
Delete all the files and directory for a project, and clear it from the cache
"""

path = os.path.join(settings.ROOT_PROJECTS_DIR, project_name)

if cache.has_key(project_name):
cache.delete(project_name)
if os.path.exists(path):
try:
os.rmdir(path)
except OSError:
files = os.listdir(path)
for file in files:
os.remove(os.path.join(path, file))
os.rmdir(path)

return True
19 changes: 19 additions & 0 deletions standards_lab/utils/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from utils.project import get_project_config, create_new_project, delete_project

from django.core.cache import cache
from django.test import TestCase


class TestUtils(TestCase):
def test_delete_project(self):
created, new_project = create_new_project("test_project", json_format=True)
delete_project("test_project")
try:
project = get_project_config("test_project")
except FileNotFoundError:
project = None

cached_project = cache.get("test_project")

self.assertEqual(project, None)
self.assertEqual(cached_project, None)

0 comments on commit d4987dc

Please sign in to comment.