Coverage for cookbook/provider/local.py: 44%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1import io 

2import os 

3from datetime import datetime 

4from os import listdir 

5from os.path import isfile, join 

6 

7from cookbook.models import Recipe, RecipeImport, SyncLog 

8from cookbook.provider.provider import Provider 

9 

10 

11class Local(Provider): 

12 

13 @staticmethod 

14 def import_all(monitor): 

15 files = [f for f in listdir(monitor.path) if isfile(join(monitor.path, f))] 

16 

17 import_count = 0 

18 for file in files: 

19 path = monitor.path + '/' + file 

20 if not Recipe.objects.filter(file_path__iexact=path, space=monitor.space).exists() and not RecipeImport.objects.filter(file_path=path, space=monitor.space).exists(): 

21 name = os.path.splitext(file)[0] 

22 new_recipe = RecipeImport( 

23 name=name, 

24 file_path=path, 

25 storage=monitor.storage, 

26 space=monitor.space, 

27 ) 

28 new_recipe.save() 

29 import_count += 1 

30 

31 log_entry = SyncLog( 

32 status='SUCCESS', 

33 msg='Imported ' + str(import_count) + ' recipes', 

34 sync=monitor, 

35 ) 

36 log_entry.save() 

37 

38 monitor.last_checked = datetime.now() 

39 monitor.save() 

40 

41 return True 

42 

43 @staticmethod 

44 def get_file(recipe): 

45 file = io.BytesIO(open(recipe.file_path, 'rb').read()) 

46 

47 return file 

48 

49 @staticmethod 

50 def rename_file(recipe, new_name): 

51 os.rename(recipe.file_path, os.path.join(os.path.dirname(recipe.file_path), (new_name + os.path.splitext(recipe.file_path)[1]))) 

52 

53 return True 

54 

55 @staticmethod 

56 def delete_file(recipe): 

57 os.remove(recipe.file_path) 

58 return True