summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2015-06-20 22:09:09 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2015-06-20 22:09:09 (GMT)
commitefb637c83f5a1dd4702b9a41801d305e9d1077fe (patch)
tree9ffc2282492459926e090eab2d447209fc64f580
parent96aea360c77deaff5048d29215a285e83ff61176 (diff)
downloadmanga-reader-efb637c83f5a1dd4702b9a41801d305e9d1077fe.zip
manga-reader-efb637c83f5a1dd4702b9a41801d305e9d1077fe.tar.gz
Print some debug info when an orphan is uploaded.
-rw-r--r--file_management/views.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/file_management/views.py b/file_management/views.py
index dff54bd..0743225 100644
--- a/file_management/views.py
+++ b/file_management/views.py
@@ -52,7 +52,24 @@ def handle_upload(form_data):
52 zf.close() 52 zf.close()
53 53
54 extracted_list = os.listdir(extracted_dir) 54 extracted_list = os.listdir(extracted_dir)
55 print('[handle_upload] form_data: {}'.format(form_data)) 55 if len(extracted_list) >= 1 and os.path.isdir(os.path.join(extracted_dir, extracted_list[0])):
56 ## At least one directory in root of archive.
57 print(' Found a single directory in extracted dir.')
58 pass
59 elif len(extracted_list) >= 1 and os.path.isfile(os.path.join(extracted_dir, extracted_list[0])):
60 ## Normal files in root
61 found_dir = False
62 for f in extracted_list:
63 if os.path.isdir(os.path.join(extracted_dir, f)):
64 print(' Found a directory among files in extracted dir.')
65 found_dir = True
66 if not found_dir:
67 print(' Archive contans only files.')
68 else:
69 print(' Archive contains folders and files.')
70 elif len(extracted_list) == 0:
71 # TODO: Error out here
72 print(' Empty archive uploaded.')
56 73
57 if form_data['oneshot'] == True: 74 if form_data['oneshot'] == True:
58 # One subdirectory, move everything up one level 75 # One subdirectory, move everything up one level
@@ -68,6 +85,7 @@ def edit_manga(request, mid):
68 return render(request, 'file_management/edit_manga.html', {'id': manga.id}) 85 return render(request, 'file_management/edit_manga.html', {'id': manga.id})
69 86
70def edit_orphan(request, oid): 87def edit_orphan(request, oid):
88 ## Only on form post
71 if request.method == 'POST': 89 if request.method == 'POST':
72 form = EditOrphanedManga(request.POST) 90 form = EditOrphanedManga(request.POST)
73 91
@@ -84,16 +102,21 @@ def edit_orphan(request, oid):
84 move_orphan(oid, manga.id) 102 move_orphan(oid, manga.id)
85 #return HttpResponse('Given name: {n}<br />System: {s}'.format(n=form['manga_name'].value(), s=form['system_name'].value())) 103 #return HttpResponse('Given name: {n}<br />System: {s}'.format(n=form['manga_name'].value(), s=form['system_name'].value()))
86 return HttpResponseRedirect(reverse('management:edit', kwargs={'mid': manga.id})) 104 return HttpResponseRedirect(reverse('management:edit', kwargs={'mid': manga.id}))
105
106 ## Display the form.
87 else: 107 else:
108 ## Get the orphan object and list it's directory's contents
88 orphan = get_object_or_404(UploadedOrphan, pk=oid) 109 orphan = get_object_or_404(UploadedOrphan, pk=oid)
89 file_list = sorted( os.listdir( os.path.join('tmp', '{f}_extracted'.format(f=orphan.filename) ) ) ) 110 file_list = sorted( os.listdir( os.path.join('tmp', '{f}_extracted'.format(f=orphan.filename) ) ) )
90 ## TODO: find a better way to chop off the extension. Or better name guessing in the first place. 111 ## TODO: find a better way to chop off the extension. Or better name guessing in the first place.
91 112
113 ## Try and figure out the system name
92 system_name = re.sub(r'(\[|\()[^\]]+(\]|\))\s?', '', orphan.original_filename[:-4]).lower() 114 system_name = re.sub(r'(\[|\()[^\]]+(\]|\))\s?', '', orphan.original_filename[:-4]).lower()
93 system_name = re.sub(r'\s+', '_', system_name) 115 system_name = re.sub(r'\s+', '_', system_name)
94 system_name = re.sub(r'[^a-z0-9_-]+', '_', system_name) 116 system_name = re.sub(r'[^a-z0-9_-]+', '_', system_name)
95 system_name = re.sub(r'_+', '_', system_name) 117 system_name = re.sub(r'_+', '_', system_name)
96 118
119 # Return the form
97 form = EditOrphanedManga( initial={ 120 form = EditOrphanedManga( initial={
98 'manga_name': orphan.original_filename[:-4], 121 'manga_name': orphan.original_filename[:-4],
99 'system_name': system_name, 122 'system_name': system_name,
@@ -115,6 +138,7 @@ def move_orphan(oid, mid):
115 new_directory = os.path.join(manga.system_name[0:1], manga.system_name[1:2], manga.system_name, "") 138 new_directory = os.path.join(manga.system_name[0:1], manga.system_name[1:2], manga.system_name, "")
116 os.makedirs(os.path.join('manga_data', new_directory), exist_ok=True) 139 os.makedirs(os.path.join('manga_data', new_directory), exist_ok=True)
117 140
141 ## Get the last chapter number and add one to it.
118 chapter_list = Chapter.objects.filter(manga=manga) 142 chapter_list = Chapter.objects.filter(manga=manga)
119 highest_chapter = -1.0 143 highest_chapter = -1.0
120 144
@@ -125,15 +149,20 @@ def move_orphan(oid, mid):
125 chapter_number = highest_chapter + 1 149 chapter_number = highest_chapter + 1
126 chapter_directory = os.path.join(new_directory, str(chapter_number), "") 150 chapter_directory = os.path.join(new_directory, str(chapter_number), "")
127 151
152 ## Make the chapter object, and link it to the manga
128 chapter = Chapter.objects.create( 153 chapter = Chapter.objects.create(
129 number=chapter_number, 154 number=chapter_number,
130 directory=chapter_directory, 155 directory=chapter_directory,
131 manga=manga, 156 manga=manga,
132 ) 157 )
133 158
159 ## Move the files from the orphan directory to the manga_data directory, ignoring non-images.
134 orphan_dir = os.path.join( 'tmp', '{n}_extracted'.format(n=orphan.filename)) 160 orphan_dir = os.path.join( 'tmp', '{n}_extracted'.format(n=orphan.filename))
135 for f in os.listdir(orphan_dir): 161 for f in os.listdir(orphan_dir):
136 os.rename( os.path.join(orphan_dir, f), os.path.join('manga_data', chapter_directory) ) 162 if os.path.splitext(f)[1] in ['jpg', 'png', 'jpeg', 'bmp', 'gif']:
163 os.rename( os.path.join(orphan_dir, f), os.path.join('manga_data', chapter_directory) )
164 else:
165 print('Found non-image in orphan. Skipping ({})'.format(f))
137 166
138 ## Remove orphan files. 167 ## Remove orphan files.
139 rmtree(orphan_dir) 168 rmtree(orphan_dir)