def get(self, request, *args, **kwargs): self.object = self.get_object() try: issues = utils.get_collected_issues(self.object) except ValueError as ex: messages.error( self.request, message=ex.args[0]) return redirect(self.get_failure_url()) series_names = [ series.group('series_name').strip() for series in issues ] self.all_found, series_data = utils.check_series_exist(self.object, series_names) if self.all_found: success, message = utils.parse_description(self.object) level = messages.SUCCESS if success else messages.WARNING messages.add_message( self.request, level, message) return redirect(self.get_success_url()) return self.render_to_response( self.get_context_data( series_data=series_data, collecting=utils.get_collecting(self.object), title='Series found for "{}"'.format(self.object.title)))
def handle(self, *args, **options): for book in Book.objects.exclude(book_type__exact='comic'): try: collecting = get_collecting(book) except ValueError as ex: self.stderr.write(str(ex)) continue self.stdout.write('\n\n=====================') self.stdout.write('Parsing {} ...'.format(book.title)) self.stdout.write(collecting) self.stdout.write('=====================\n\n') book.series_mapping.clear() for collected_series in get_collected_issues(book): series_name = collected_series.group('series_name').strip().upper() db_series = Series.objects.filter(matches__contains=[series_name]).exclude(marvel_id__isnull=True) series_map = {str(i): s for i, s in enumerate(db_series)} selection = '' if db_series.count() == 1: series = db_series.first() book.series_mapping[series_name] = str(series.id) book.series.add(series) book.save() continue if db_series.count() > 1: for idx, s in iter(sorted(series_map.items())): self.stdout.write('{}. {}'.format(idx, s)) selection = input('"{}" Enter # matching or press ENTER: '.format(series_name)) if selection != '' and selection in series_map: book.series_mapping[series_name] = str(series_map[selection].id) book.series.add(series_map[selection]) if db_series.count() == 0 or selection == '' or selection not in series_map: provider = MarvelSeriesInfoProvider() querystring = input('"{}" Correct name or press ENTER to search on Marvel API: '.format(series_name)) querystring = series_name if not querystring else querystring provider.query(querystring) series_map = {str(i): s for i, s in enumerate(provider.results)} if provider.total > 0: for i, s in iter(sorted(series_map.items())): self.stdout.write('{}. {} ({}) [{}]'.format(i, s.name, s.start_year, s.issue_count)) selection = input('"{}" Enter matching # or press ENTER: '.format(series_name)) if selection != '' and selection in series_map: s = series_map[selection] series, _ = Series.objects.get_or_create( name=s.name, start_year=s.start_year, ) series.marvel_id = s.marvel_id series.matches.append(series_name) series.save() book.series_mapping[series_name] = str(series.id) book.series.add(series) book.save() success, message = parse_description(book) if success: self.stdout.write(message) self.stdout.write('{} has the following series and issues:'.format(book.title)) for s in book.series.all(): self.stdout.write('{}'.format(s)) for i in book.issues.filter(series=s): self.stdout.write('\t{0.title} ({0.marvel_id})'.format(i)) else: self.stderr.write('\n{} while parsing {}\n'.format(message, book.title))