from pyramid.view import view_config
from pyramid.view import view_defaults
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.security import authenticated_userid
from sqlalchemy.exc import DBAPIError
import datetime
import hashlib

from .models import *

import logging

log = logging.getLogger(__name__)


@view_defaults(route_name='recent', renderer='templates/recent.jinja2')
class RecentView(object):
	def __init__(self, request):
		self.request = request

	@view_config(request_method='GET')
	def get(self):
		try:
			recent = DBSession.query(Paste).filter(Paste.public == True).order_by(Paste.createdon.desc()).limit(50)
			return {'title': 'Recent Pastes', 'noindex': True, 'recent': recent}
		except DBAPIError:
			return HTTPNotFound('There is no such resource')
