def get(self, request, id, slug): context = {} manga = self.get_manga_for_view(id) manga_page_list = list(manga.mangapage_set.order_by('page')) payload = self.get_payload(manga_page_list) if manga.tank_id: manga_list = Manga.published.filter(tank_id=manga.tank_id) context['chapter_list'] = natural_sort(manga_list, 'tank_chapter') if manga.collection_id: manga_list = Manga.published.filter(collection_id=manga.collection_id) context['collection_list'] = natural_sort(manga_list, 'collection_part') try: archive = MangaArchive.objects.get(manga=manga) except MangaArchive.DoesNotExist: archive = MangaArchiveGenerator.generate(manga) download_available = (archive != None) and os.path.exists(archive.file.path) if not download_available: MangaArchiveGenerator.generate(manga) context.update({ 'archive': archive, 'download_available': download_available, 'manga': manga, 'page_count': len(manga_page_list), 'payload': payload, }) return self.render_to_response(context)
def test_generate_manga_archive(self): self.assertFalse(MangaArchive.objects.filter(manga=self.manga).exists()) MangaArchiveGenerator.generate(self.manga) ma = MangaArchive.objects.get(manga=self.manga) ma_path1 = ma.file.path self.assertTrue(os.path.exists(ma_path1)) MangaArchiveGenerator.generate(self.manga) ma = MangaArchive.objects.get(manga=self.manga) ma_path2 = ma.file.path self.assertTrue(os.path.exists(ma_path1)) self.assertTrue(os.path.exists(ma_path2)) self.assertTrue(DeletedFile.objects.filter(path=ma_path1).exists())
def save(self): manga = super().save(commit=False) cd = self.cleaned_data tank_name, tank_chapter = cd.get('tank'), cd.get('tank_chapter') if tank_name and tank_chapter: manga.tank = get_or_create_tag_by_name_or_alias(TagType.TANK, tank_name, self.request.user) manga.tank_chapter = tank_chapter else: manga.tank = None manga.tank_chapter = None collection_name, collection_part = cd.get('collection'), cd.get('collection_part') if collection_name and collection_part: manga.collection = get_or_create_tag_by_name_or_alias(TagType.COLLECTION, collection_name, self.request.user) manga.collection_part = collection_part else: manga.collection = None manga.collection_part = None if self.html: manga.html = self.html if cd.get('action') == MangaAction.PUBLISH: manga.status = MangaStatus.PUBLISHED manga.published_on = timezone.now() send_email_alert( subject='[Fufufuu] Published: {}'.format(manga.title), message=manga.info_text, ) manga.save(self.request.user) manga.tags.clear() manga.tags.add(*self.get_tag_list()) if manga.status == MangaStatus.PUBLISHED: MangaArchiveGenerator.generate(manga) if manga.tank and not manga.tank.cover: manga.tank.set_default_cover() if manga.collection and not manga.collection.cover: manga.collection.set_default_cover() return manga
def test_download_view_get(self): manga_archive = MangaArchiveGenerator.generate(self.manga) download_link = DownloadLink.objects.create( url=manga_archive.file.url, ip_address='127.0.0.1', created_by=self.user, ) response = self.client.get(reverse('download', args=[download_link.key, manga_archive.name])) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], 'http://testserver{}'.format(manga_archive.file.url))
def post(self, request, id, slug): manga = self.get_manga_for_view(id) try: manga_archive = MangaArchive.objects.get(manga=manga) except MangaArchive.DoesNotExist: manga_archive = MangaArchiveGenerator.generate(manga) if not manga_archive or not os.path.exists(manga_archive.file.path): MangaArchiveGenerator.generate(manga) messages.error(request, _('Sorry, the download is currently unavailable.')) return redirect('manga', id=id, slug=slug) link, created = DownloadLink.objects.get_or_create( url=manga_archive.file.url, ip_address=get_ip_address(request), created_by=request.user if request.user.is_authenticated() else None, ) if created: manga_archive.downloads += 1 manga_archive.save() return redirect('download', key=link.key, filename=manga_archive.name)
def test_clear_downloads(self): now = datetime.datetime.now(tz=pytz.UTC) manga_archive = MangaArchiveGenerator.generate(self.manga) download_link1 = DownloadLink.objects.create( url=manga_archive.file.url, ip_address='some-ip-address', created_by=self.user, ) download_link1.created_on = now - datetime.timedelta(minutes=90) download_link1.save() download_link2 = DownloadLink.objects.create( url=manga_archive.file.url, ip_address='some-ip-address', created_by=self.user, ) download_link2.created_on = now - datetime.timedelta(minutes=30) download_link2.save() call_command('clear_downloads') self.assertFalse(DownloadLink.objects.filter(id=download_link1.id).exists()) self.assertTrue(DownloadLink.objects.filter(id=download_link2.id).exists())
def test_generate_manga_archive_lock(self): MangaArchiveGenerator.acquire_lock(self.manga) self.assertEqual(MangaArchiveGenerator.generate(self.manga), None)
def save(self, manga): getattr(self, self.data.get('action'))() if manga.status == MangaStatus.PUBLISHED: MangaArchiveGenerator.generate(manga)