def post(self): url = self.data['url'] progress = self.data['progress'] chapter = Chapter.get_by_url(url) if chapter is None: raise PyError('nonexistent_chapter') ChapterProgress.set_progress(self.user.key.id(), progress, chapter.url, chapter.series_url) return {}
def get(self): series = create_or_get_series(self.data['url']) resp = { field: getattr(series, field) for field in ('site', 'name', 'thumb_url', 'tags', 'status', 'description', 'authors') } # If the provided chapter_limit is valid, return only that many # chapters in API response. chapters = series.chapters chapter_limit = self.data['chapter_limit'] if chapter_limit in range(len(chapters)): chapters = chapters[:chapter_limit] resp['chapters'] = chapters # If user is logged in if hasattr(self, 'user'): # tell if this series is in their bookmarks resp['is_bookmarked'] = series.is_bookmarked_by(self.user) # insert user's reading progress into each chapter too progresses = ChapterProgress.get_by_series_url(self.user.key.id(), self.data['url']) for chap in resp['chapters']: if chap['url'] in progresses: chap['progress'] = progresses[chap['url']] else: chap['progress'] = 'unread' return resp
def get(self): series = create_or_get_series(self.data['url']) resp = { field: getattr(series, field) for field in ('site', 'name', 'thumb_url', 'tags', 'status', 'description', 'authors', 'chapters') } if not hasattr(self, 'user'): self._fetch_first_chapter(resp['chapters']) return resp # tell if this series is in their bookmarks resp['is_bookmarked'] = series.is_bookmarked_by(self.user) # insert user's reading progress into each chapter too progresses = ChapterProgress.get_by_series_url(self.user.key.id(), self.data['url']) # Only show chapters that come after the latest "finished" chapter, or # starting from the latest "reading" chapter, whichever comes last. if self.data['only_unread']: shown_chapters = deque(maxlen=6) for chap in resp['chapters']: if chap['url'] in progresses: chap['progress'] = progresses[chap['url']] if chap['progress'] == 'finished': break elif chap['progress'] == 'reading': shown_chapters.append(chap) break else: chap['progress'] = 'unread' shown_chapters.append(chap) resp['chapters'] = list(shown_chapters) # Show all chapters: else: for chap in resp['chapters']: if chap['url'] in progresses: chap['progress'] = progresses[chap['url']] else: chap['progress'] = 'unread' self._fetch_first_chapter(resp['chapters']) return resp