def index(self): c.users_by_posts = Session.query(User). \ order_by(User.num_of_posts.desc()).limit(10) c.users_by_topics = Session.query(User). \ order_by(User.num_of_topics.desc()).limit(10) c.title = 'kralji gnoja' return render('/rankings.mako')
def topics(self, id, page=1): c.user = Session.query(User).filter(User.id==int(id)).first() c.topics = webhelpers.paginate.Page( Session.query(Topic).filter(Topic.user_id==c.user.id). \ order_by(Topic.last_post_created_at.desc()), page=int(page), items_per_page=25) c.title = 'teme od ' + c.user.nick_name return render('/users/topics.mako')
def topics(self, id, page=1): c.user = Session.query(User).filter(User.id == int(id)).first() c.topics = webhelpers.paginate.Page( Session.query(Topic).filter(Topic.user_id==c.user.id). \ order_by(Topic.last_post_created_at.desc()), page=int(page), items_per_page=25) c.title = 'teme od ' + c.user.nick_name return render('/users/topics.mako')
def posts(self, id, page=1): c.user = Session.query(User).filter(User.id==int(id)).first() c.posts = webhelpers.paginate.Page( Session.query(Post, Topic). \ options(orm.joinedload(Post.avatar)). \ filter(Post.topic_id==Topic.id). \ filter(Post.user_id==c.user.id). \ order_by(Post.created_at.desc()), page=int(page), items_per_page=40) c.title = 'posti od ' + c.user.nick_name return render('/users/posts.mako')
def show(self, id, page=1): c.topic = Session.query(Topic).filter(Topic.id==int(id)).first() if not c.topic: abort(404) c.posts = webhelpers.paginate.Page( Session.query(Post, User).filter(Post.user_id==User.id). \ options(orm.joinedload(Post.avatar)). \ filter(Post.topic_id==c.topic.id). \ order_by(Post.created_at), page=int(page), items_per_page=40) c.title = c.topic.full_title() return render('/topics/show.mako')
def posts(self, id, page=1): c.user = Session.query(User).filter(User.id == int(id)).first() c.posts = webhelpers.paginate.Page( Session.query(Post, Topic). \ options(orm.joinedload(Post.avatar)). \ filter(Post.topic_id==Topic.id). \ filter(Post.user_id==c.user.id). \ order_by(Post.created_at.desc()), page=int(page), items_per_page=40) c.title = 'posti od ' + c.user.nick_name return render('/users/posts.mako')
def show(self, id, page=1): c.topic = Session.query(Topic).filter(Topic.id == int(id)).first() if not c.topic: abort(404) c.posts = webhelpers.paginate.Page( Session.query(Post, User).filter(Post.user_id==User.id). \ options(orm.joinedload(Post.avatar)). \ filter(Post.topic_id==c.topic.id). \ order_by(Post.created_at), page=int(page), items_per_page=40) c.title = c.topic.full_title() return render('/topics/show.mako')
def show(self, id): c.user = Session.query(User). \ options(orm.joinedload(User.avatar)). \ get(id) if not c.user: abort(404) c.avatar_guesses_total = Session.query(AvatarGuess). \ filter(AvatarGuess.user==c.user). \ count() c.avatars_guessed = Session.query(AvatarGuess). \ filter(AvatarGuess.user==c.user). \ filter(AvatarGuess.guessed==True). \ count() c.title = c.user.nick_name return render('/users/show.mako')
def avatars(self, id): c.user = Session.query(User). \ options(orm.joinedload(User.avatars)). \ get(id) if not c.user: abort(404) return render('/users/avatars.mako')
def claim(self, id): c.user = Session.query(User).filter(User.id==id). \ filter(User.password==None).first() if not c.user: abort(404) if request.method == 'POST': c.user.token = uuid.uuid4().get_hex() Session.add(c.user) Session.commit() mn3 = mn3njalnik.Mn3njalnik() try: mn3.login(config['mn3njalnik.username'], config['mn3njalnik.password']) mn3.pm(c.user.nick_name.encode('utf-8'), 'Polasti se svojega racuna se danes!', """ Oj, ti! Svojega racuna na nasi strani http://sedemnajst.si se lahko polastis zdaj: http://sedemnajst.si/users/passwd/%s Lep pozdrav, in ostani vedno /17/ """ % c.user.token) h.flash(u'Imaš ZS—velik uspeh!') except: h.flash(u'Oh ne, nekaj je šlo napak.') c.title = u'polasti se svojega računa' return render('/users/claim.mako')
def login(self): if request.method == 'POST': user = Session.query(User). \ filter(User.nick_name==request.params['nick_name']) . \ filter(User.password!=None).first() if user and bcrypt.hashpw(request.params['password'], user.password) == user.password: session['user'] = user session.save() if 'return_to' in session: to = session['return_to'] del session['return_to'] session.save() redirect(to) elif 'return_to' in request.params: redirect(request.params['return_to']) else: redirect('/') else: h.flash( u'Sorči, to ni nobena kombinacija uporabniškega imena' + 'in gesla, ki bi bila meni znana.') c.title = 'prijava' return render('/auth/login.mako')
def claim(self, id): c.user = Session.query(User).filter(User.id==id). \ filter(User.password==None).first() if not c.user: abort(404) if request.method == 'POST': c.user.token = uuid.uuid4().get_hex() Session.add(c.user) Session.commit() mn3 = mn3njalnik.Mn3njalnik() try: mn3.login(config['mn3njalnik.username'], config['mn3njalnik.password']) mn3.pm( c.user.nick_name.encode('utf-8'), 'Polasti se svojega racuna se danes!', """ Oj, ti! Svojega racuna na nasi strani http://sedemnajst.si se lahko polastis zdaj: http://sedemnajst.si/users/passwd/%s Lep pozdrav, in ostani vedno /17/ """ % c.user.token) h.flash(u'Imaš ZS—velik uspeh!') except: h.flash(u'Oh ne, nekaj je šlo napak.') c.title = u'polasti se svojega računa' return render('/users/claim.mako')
def index(self): current_page = 'page' in request.params \ and int(request.params['page']) or 1 q = request.params['q'] c.results = webhelpers.paginate.Page( Session.query(Post, Topic, User). \ options(orm.joinedload(Post.avatar)). \ filter(Topic.id==Post.topic_id). \ filter(User.id==Post.user_id). \ filter('tsv @@ plainto_tsquery(:terms)'). \ params(terms=q). \ add_column(func.ts_headline('pg_catalog.simple', Post.body, func.plainto_tsquery(q), 'HighlightAll=TRUE, ' \ 'StartSel=<strong>, ' \ 'StopSel=</strong>')). \ order_by(Post.created_at.desc()), page=current_page, items_per_page=40, url=webhelpers.paginate.PageURL_WebOb(request)) c.title = 'rezultati iskanja' return render('/search.mako')
def login(self): if request.method == 'POST': user = Session.query(User). \ filter(User.nick_name==request.params['nick_name']) . \ filter(User.password!=None).first() if user and bcrypt.hashpw(request.params['password'], user.password) == user.password: session['user'] = user session.save() if 'return_to' in session: to = session['return_to'] del session['return_to'] session.save() redirect(to) elif 'return_to' in request.params: redirect(request.params['return_to']) else: redirect('/') else: h.flash(u'Sorči, to ni nobena kombinacija uporabniškega imena' + 'in gesla, ki bi bila meni znana.') c.title = 'prijava' return render('/auth/login.mako')
def show(self, id): c.quote = Session.query(Quote). \ options(orm.joinedload_all(Quote.post, Post.avatar)). \ get(id) if not c.quote: abort(404) c.title = 'navedek' return render('/quotes/show.mako')
def index(self): c.users = Session.query(User). \ filter(User.avatar!=None). \ options(orm.joinedload(User.avatar)). \ order_by(func.random()). \ limit(8). \ all() c.one = choice(c.users) if 'user' in session: c.avatar_guesses = Session.query(AvatarGuess). \ options(orm.joinedload(AvatarGuess.guessed_avatar_)). \ filter(AvatarGuess.user==session['user']). \ order_by(AvatarGuess.created_at.desc()). \ limit(25) if 'ajax' in request.params: return render('/guess-avatar/choice.mako') else: c.title = 'ugani avatar' return render('/guess-avatar/index.mako')
def create(self): post = Session.query(Post). \ filter(Post.id==int(request.params['post'])).first() quote = Quote(post, session['user']) Session.add(quote) Session.commit() Session.refresh(session['user']) h.flash('Uspeh') redirect(url('quote', id=quote.id))
def guessed(self): user = Session.query(User). \ get(request.params['user_id']) avatar = Session.query(Avatar). \ get(request.params['avatar_id']) guessed = user.avatar.id == avatar.id if 'user' in session: avatar_guess = AvatarGuess(guessed, avatar, session['user']) Session.add(avatar_guess) Session.commit() Session.refresh(session['user']) else: avatar_guess = AvatarGuess(guessed, avatar) if 'ajax' in request.params: c.avatar_guess = avatar_guess return render('/guess-avatar/guessed-avatar.mako') else: if guessed: h.flash('Bravo, uganil/a si pravilno!') else: h.flash(u'Ne bo držalo.') redirect("/games/guess-avatar")
def new(self, post): c.post = Session.query(Post). \ options(orm.joinedload(Post.avatar)). \ get(post) if not c.post: abort(404) # quoted already? if c.post.quote != None: h.flash(u'Ta post je že v bazi navedkov, glej:') redirect(url('quote', id=c.post.quote.id)) c.title = 'dodaj navedek' return render('/quotes/new.mako')
def summary(self, id): post = Session.query(Post). \ filter(Post.topic_id==id). \ order_by(Post.created_at). \ first() text = lxml.html.fromstring(post.body).text_content() parts = textwrap.wrap(text, 250) if len(parts) == 0: c.summary = '[…]' elif len(parts) == 1: c.summary = parts[0] else: c.summary = parts[0] + ' […]' return render('/topics/summary.mako')
def vote(self, id, way): quote = Session.query(Quote).filter(Quote.id==id).first() if not quote: abort(404) qv = QuoteVote(quote, session['user'], way == 'up') Session.add(qv) if way == 'up': quote.upvotes += 1 elif way == 'down': quote.downvotes += 1 Session.add(quote) Session.commit() Session.refresh(session['user']) h.flash(u'Tvoj glas smo zabeležili.') redirect(url('quote', id=quote.id))
def passwd(self, token): c.user = Session.query(User).filter(User.token == token).first() if not c.user: abort(404) if request.method == 'POST': if request.params['passwd'] == request.params['passwd_confirm']: c.user.password = bcrypt.hashpw(request.params['passwd'], bcrypt.gensalt()) c.user.token = None Session.add(c.user) Session.commit() h.flash(u'Uspeh—zdaj se lahko prijaviš.') redirect(url('login')) else: h.flash('Gesli se ne ujemata. :(') c.title = 'nastavi geslo' return render('/users/passwd.mako')
def passwd(self, token): c.user = Session.query(User).filter(User.token==token).first() if not c.user: abort(404) if request.method == 'POST': if request.params['passwd'] == request.params['passwd_confirm']: c.user.password = bcrypt.hashpw(request.params['passwd'], bcrypt.gensalt()) c.user.token = None Session.add(c.user) Session.commit() h.flash(u'Uspeh—zdaj se lahko prijaviš.') redirect(url('login')) else: h.flash('Gesli se ne ujemata. :(') c.title = 'nastavi geslo' return render('/users/passwd.mako')
def charts(self, id, type, limit=None, start=None, end=None): user = Session.query(User).get(id) if not user: abort(404) if limit: start_date = datetime.date.today() - \ datetime.timedelta(days=int(limit)) end_date = None elif start and end: start = [int(i) for i in start.split('-')] start_date = datetime.date(start[0], start[1], start[2]) end = [int(i) for i in end.split('-')] end_date = datetime.date(end[0], end[1], end[2]) else: start_date = None end_date = None if type == 'ppdow': data = user.posts_per_dow(start_date=start_date, end_date=end_date) chart = HorizontalBarStack(data) chart.axes.type('xy') chart.axes.label(0, '0', '100') chart.axes.label(1, *reversed([i[1] for i in days])) chart.fill('bg', 's', 'feeebd') chart.grid(10, 0, 10, 0) chart.scale(0, max(data)) chart.size(680, 220) elif type == 'pph': data = user.posts_per_hour(start_date=start_date, end_date=end_date) chart = VerticalBarStack(data) chart.axes.type('yx') chart.axes.label(0, '0', '100') chart.axes.label(1, *range(0, 24)) chart.fill('bg', 's', 'feeebd') chart.grid(0, 10, 10, 0) chart.scale(0, max(data)) chart.size(680, 300) else: abort(400) return chart.img()
def index(self, page=None): if not page: if 'page' in request.params: page = int(request.params['page']) else: page = 1 if request.cookies.get("archive_limit"): c.limit = int(request.cookies.get("archive_limit")) else: c.limit = 25 if request.cookies.get("archive_view"): c.view = request.cookies.get("archive_view") else: c.view = "full" query = Session.query(Topic, User). \ options(orm.joinedload(User.avatar)). \ filter(Topic.user_id==User.id). \ order_by(Topic.last_post_created_at.desc()) if 'from' in request.params: c.from_ = datetime.strptime(request.params['from'], '%Y-%m-%d') query = query.filter(Topic.last_post_created_at >= c.from_) else: c.from_ = datetime(2009, 9, 26) # Day we started if 'to' in request.params: c.to = datetime.strptime(request.params['to'], '%Y-%m-%d') # Defaults to 00:00 on that day, while we need end of day c.to += timedelta(days=1) query = query.filter(Topic.last_post_created_at <= c.to) else: c.to = datetime.now() c.topics = webhelpers.paginate.Page( query, page=int(page), items_per_page=c.limit, url=webhelpers.paginate.PageURL_WebOb(request)) c.title = 'arhiv' return render('/archive/index.mako')
def index_json(self): users = Session.query(User). \ options(orm.joinedload(User.avatar)) usrs = {} for user in users: usrs[user.id] = {'id': user.id, 'nick_name': user.nick_name, 'num_of_posts': user.num_of_posts, 'num_of_topics': user.num_of_topics} if user.avatar: usrs[user.id]['avatar'] = { 'id': user.avatar.id, 'created_at': user.avatar.created_at.isoformat(), 'filename': user.avatar.filename, 'md5sum': user.avatar.md5sum, 'width': user.avatar.width, 'height': user.avatar.height } return usrs
def index(self, page=None): if not page: if 'page' in request.params: page = int(request.params['page']) else: page = 1 if request.cookies.get("archive_limit"): c.limit = int(request.cookies.get("archive_limit")) else: c.limit = 25 if request.cookies.get("archive_view"): c.view = request.cookies.get("archive_view") else: c.view = "full" query = Session.query(Topic, User). \ options(orm.joinedload(User.avatar)). \ filter(Topic.user_id==User.id). \ order_by(Topic.last_post_created_at.desc()) if 'from' in request.params: c.from_ = datetime.strptime(request.params['from'], '%Y-%m-%d') query = query.filter(Topic.last_post_created_at>=c.from_) else: c.from_ = datetime(2009, 9, 26) # Day we started if 'to' in request.params: c.to = datetime.strptime(request.params['to'], '%Y-%m-%d') # Defaults to 00:00 on that day, while we need end of day c.to += timedelta(days=1) query = query.filter(Topic.last_post_created_at<=c.to) else: c.to = datetime.now() c.topics = webhelpers.paginate.Page( query, page=int(page), items_per_page=c.limit, url=webhelpers.paginate.PageURL_WebOb(request)) c.title = 'arhiv' return render('/archive/index.mako')
def index_json(self): users = Session.query(User). \ options(orm.joinedload(User.avatar)) usrs = {} for user in users: usrs[user.id] = { 'id': user.id, 'nick_name': user.nick_name, 'num_of_posts': user.num_of_posts, 'num_of_topics': user.num_of_topics } if user.avatar: usrs[user.id]['avatar'] = { 'id': user.avatar.id, 'created_at': user.avatar.created_at.isoformat(), 'filename': user.avatar.filename, 'md5sum': user.avatar.md5sum, 'width': user.avatar.width, 'height': user.avatar.height } return usrs
def reset_passwd(self): if request.method == 'POST': user = Session.query(User). \ filter(User.nick_name==request.params['nick_name']). \ first() if user: user.token = uuid.uuid4().get_hex() Session.add(user) Session.commit() mn3 = mn3njalnik.Mn3njalnik() try: mn3.login(config['mn3njalnik.username'], config['mn3njalnik.password']) mn3.pm(user.nick_name.encode('utf-8'), 'Pozabljeno geslo? Ni problema!', """ Ojla, prijatelj! Zahteval si ponastavitev svojega gesla na nasi strani: http://sedemnajst.si To lahko storis sedaj, na naslednjem naslovu: http://sedemnajst.si/users/passwd/%s Ce te zahteve nisi sprozil sam, pa lahko to sporocilo brez skrbi ignoriras. Lep pozdrav, in ostani vedno /17/ """ % user.token) h.flash(u'Rečeno, storjeno—imaš ZS.') except: h.flash(u'Oh ne, nekaj je šlo napak.') else: h.flash(u'Sori, uporabnik s takšnim imenom ne obstaja.') c.title = 'resetiraj geslo' return render('/users/reset-passwd.mako')
def reset_passwd(self): if request.method == 'POST': user = Session.query(User). \ filter(User.nick_name==request.params['nick_name']). \ first() if user: user.token = uuid.uuid4().get_hex() Session.add(user) Session.commit() mn3 = mn3njalnik.Mn3njalnik() try: mn3.login(config['mn3njalnik.username'], config['mn3njalnik.password']) mn3.pm( user.nick_name.encode('utf-8'), 'Pozabljeno geslo? Ni problema!', """ Ojla, prijatelj! Zahteval si ponastavitev svojega gesla na nasi strani: http://sedemnajst.si To lahko storis sedaj, na naslednjem naslovu: http://sedemnajst.si/users/passwd/%s Ce te zahteve nisi sprozil sam, pa lahko to sporocilo brez skrbi ignoriras. Lep pozdrav, in ostani vedno /17/ """ % user.token) h.flash(u'Rečeno, storjeno—imaš ZS.') except: h.flash(u'Oh ne, nekaj je šlo napak.') else: h.flash(u'Sori, uporabnik s takšnim imenom ne obstaja.') c.title = 'resetiraj geslo' return render('/users/reset-passwd.mako')
def index(self, content_type='text/html'): c.users = Session.query(User).order_by(User.nick_name) c.title = 'uporabniki' return render('/users/index.mako')
def index(self): c.quotes = Session.query(Quote). \ options(orm.joinedload_all(Quote.post, Post.avatar)). \ order_by((Quote.upvotes-Quote.downvotes).desc()) c.title = 'baza navedkov' return render('/quotes/index.mako')
def index(self): c.users_by_posts = Session.query(User).order_by(User.num_of_posts.desc()).limit(10) c.users_by_topics = Session.query(User).order_by(User.num_of_topics.desc()).limit(10) c.title = "kralji gnoja" return render("/rankings.mako")