def get(self, id): """Fetch entry by id. Raises :exc:`Entry.Empty` if the entry is unfound. Blocking.""" data = raw_query( action='display_message', id=int(id), language='sv', ) token_order = ['title', 'mail1', 'submitted', 'body', 'mail2', 'report'] cur_token_id = 0 title = u'' submitted = None body = u'' mail = u'' token = None for line in data.splitlines()[1:-1]: token = token_order[cur_token_id] line = line.strip() if token == 'title': title = regex.MATCH_H3.match(line).groups()[0] cur_token_id += 1 elif token == 'mail1': mail = regex.MATCH_MAIL.match(line).groups()[0] cur_token_id += 1 elif token == 'submitted': try: submitted = parse_datetime( regex.MATCH_DATETIME.match(line).groups()[0] ) except: pass cur_token_id += 1 elif token == 'body': start_p = '<p>' end_p = '</p>' ends_now = line.endswith(end_p) body += line.replace(start_p, '').replace(end_p, '') if ends_now: cur_token_id += 1 else: body += '\n' elif token == 'mail2': cur_token_id += 1 elif token == 'report': break assert token == token_order[-1] if not title and not body: raise Entry.Empty('empty entry %s' % id) return Entry(id, title, mail, submitted, body)
def get(self, id): """Fetch category by id. Blocking.""" if id in constants.CATEGORIES: title = constants.CATEGORIES[id] else: raise Category.Invalid('invalid id %s' % id) data = raw_query( category_id=int(id), language='sv', ) for reg in [ regex.LATEST_ENTRIES_HEADER, regex.CURRENTLY_EMPTY, ]: if re.search(reg, data): return Category(id, title, []) lines = data.splitlines() entry_ids = [] for line in lines: if regex.POST_NEW_ENTRY.search(line): continue match = regex.MATCH_MESSAGE_ID.search(line) if not match: continue entry_ids.append(int(match.groups()[0])) entries = [] for eid in entry_ids: try: entries.append(Entry.objects.get(eid)) except Entry.Empty: pass return Category(id, title, entries)