Beispiel #1
0
def fetch_posts(self, year=None, month=None, slug=None):
    # our base regexp
    regexp = BASE_POST_REGEXP

    if year is not None:
        # amend our regexp to filter by year
        regexp = regexp.replace('(?P<year>[0-9]{4})', str(year))

    if month is not None:
        regexp = regexp.replace('(?P<month>[0-9]{2})', "%02d" % (int(month),))

    if slug is not None:
        regexp = regexp.replace('(?P<slug>[a-zA-Z_-]*)', str(slug))

    path = current_app.config.get('BLOG_FILE_POSTS_FOLDER', False)

    posts = []

    for file_name in os.listdir(path):
        m = re.match(regexp, file_name)
        if m:
            posts.append(BlogPost(**{
                'title': deslugify(m.group('slug')),
                'year': m.group('year'),
                'month': m.group('month'),
                'day': m.group('day'),
                'slug': m.group('slug'),
                'path': os.path.join(path, file_name),
                'created': date(int(m.group('year')), int(m.group('month')), int(m.group('day')))
            }))

    return sorted(posts, key=attrgetter('created'), reverse=True)
Beispiel #2
0
    def __init__(self, year=None, month=None, slug=None, current_app=None):

        self.regexp = '(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})-(?P<slug>[0-9a-zA-Z_-]*)\.m(?:ark)?d(?:own)?'

        if year is not None:
            self.regexp = self.regexp.replace('(?P<year>[0-9]{4})', '(?P<year>%s)' % str(year))

        if month is not None:
            self.regexp = self.regexp.replace('(?P<month>[0-9]{2})', '(?P<month>%s)' % str(month))

        if slug is not None:
            self.regexp = self.regexp.replace('(?P<slug>[0-9a-zA-Z_-]*)', '(?P<slug>%s)' % str(slug))

        path = current_app.config.get('BLOG_FILE_POSTS_FOLDER', False)

        self.posts = []

        for file_name in os.listdir(path):
            m = re.match(self.regexp, file_name)
            if m:
                self.posts.append(BlogPost(**{
                    'title': deslugify(m.group('slug')),
                    'year': m.group('year'),
                    'month': m.group('month'),
                    'day': m.group('day'),
                    'slug': m.group('slug'),
                    'path': os.path.join(path, file_name),
                    'created': date(int(m.group('year')), int(m.group('month')), int(m.group('day')))
                }))

        self.posts.sort(key=attrgetter('created'), reverse=True)