__init__.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import flask
  2. import config
  3. from groupie import indexer, models, VERSION
  4. app = flask.Flask('groupie.web')
  5. app.config.from_object(config)
  6. @app.context_processor
  7. def common_vars():
  8. return {'GROUPIE_VERSION': VERSION}
  9. @app.template_filter()
  10. def format_time(time):
  11. return time.strftime('%Y-%m-%d')
  12. @app.template_filter()
  13. def nl2br(text):
  14. return flask.Markup('<br>').join(text.splitlines())
  15. @app.route('/')
  16. def index():
  17. groups = []
  18. for slug in config.GROUPS:
  19. groups.append(models.Group.get(slug))
  20. return flask.render_template('index.html', groups=groups)
  21. @app.route('/<group_slug>')
  22. def search(group_slug):
  23. group = models.Group.get(group_slug)
  24. q = flask.request.args.get('q', '').strip()
  25. sort = flask.request.args.get('sort', 'popular').strip()
  26. page = flask.request.args.get('page', 1, int)
  27. limit = 20
  28. result = error = next_page = None
  29. if q:
  30. if len(q) <= 1:
  31. error = 'Search query should be longer than 1 character.'
  32. else:
  33. result, next_page = indexer.search(group, q, sort, page, limit)
  34. return flask.render_template('search.html', group=group, q=q, sort=sort, error=error, result=result, page=page, prev_page=page - 1, next_page=next_page, limit=limit)