# -*- coding: utf-8 -*- from time import tzname from lybmods.lybformauth import AuthController, member_of, any_of from lybmods.lybclasses import Doc, Cat import cherrypy from lybmods import lybtools from lybmods import lybhtdata from lybmods import lybedit from lybmods import lybctl class Root: _cp_config = { 'tools.sessions.on': True, 'tools.sessions.storage_type' : 'pgsql', 'tools.sessions.name' : 'PGSQLSes', 'tools.sessions.timeout': 120, 'tools.auth.on': True } auth = AuthController() mdict = lybtools.mdict from_page = '/' def __init__(self): self.edit = lybedit.Edit(self) self.ctl = lybctl.Ctl(self) @cherrypy.expose def index(self): self.from_page = '/' return self.buildhtml('Архив', lybhtdata.index_html) @cherrypy.expose def css(self): return lybhtdata.style @cherrypy.expose def poisk(self, search = None, catid = 0): if not search or search == '': cherrypy.HTTPRedirect("/") search.replace('<', '').replace('>', '').replace(';', '').replace(',', '') html_cont = '
\n\n{html}\n
' html = '' cat = Cat(0) res = cat.search(search) html += '

Найдено ' + str(len(res)) + ' документов

' for did in res: doc = Doc(did) catid = doc.cat.id html += '' + doc.name + '\n(' + self.path(did, doc = True) + ')\n' return self.buildhtml('Архив[Запрос: ' + search + ']', html_cont.format(html=html)) @cherrypy.expose def loginpage(self, from_page='/'): self.from_page = from_page.replace('<', '').replace('>', '').replace(';', '').replace(',', '') username = cherrypy.session.get(lybtools.SESSION_KEY, None) if username: raise cherrypy.HTTPRedirect("/") else: return self.buildhtml('Архив [Получение доступа]', lybhtdata.login_body) @cherrypy.expose def denied(self): return self.buildhtml('Архив [Доступ закрыт]', lybhtdata.access_denied_body) def path(self, oid, doc = False, lcat = False): if doc: p = Doc(oid).path else: p = Cat(oid).path html = '' for i in p: name = Cat(i).name if len(name) > 15: name = name[:15] + '...' if i > 0: q = '»' else: q = '' html += q + '[' + name + ']' if p == []: q = '' else: q = '»' if not doc and not lcat: html += q + '' + Cat(oid).name + '' if lcat: html += q + '[' + Cat(oid).name + ']' return html @cherrypy.expose def category(self, catid = 0): catid = int(catid) category = Cat(catid) html = '
' + self.path(catid) + '
\n\n' if category.catcount: html += '\n' for cat in category.categories: rec = '\n' if any_of(member_of('admins'), member_of('editors'))(): html += '\n{rec}\n\n\n\n\n' else: html += '{rec}'.format(rec = rec) if category.catcount and category.doccount: html += '\n' if category.doccount: html += '\n' for doc in category.documents: did = doc.id rec = '\n' if any_of(member_of('admins'), member_of('editors'))(): html += '\n{rec}\n\n\n\n\n\n\n' else: html += '{rec}'.format(rec = rec) html += '

Подразделы:

[' + cat.name + ']
************

Документы:

' + doc.name + '
\n
\n' if any_of(member_of('admins'), member_of('editors'))(): html += '
\n
\n\n\n
\n
\n' html += '
\n
\n\n\n
\n
\n' html += '
\n
\nНовый раздел\n\n\n\n
\n
\n' if catid != 0: parent = Cat(catid).parent html += '
← Обратно в раздел "' + parent.name + '"
\n' title = 'Архив[Раздел: ' + category.name + ']' return self.buildhtml(title, html) @cherrypy.expose def get_doc(self, catid = 0, doc = None, plain = False): catid = int(catid) cat = Cat(catid) if not doc: return doc = int(doc) doco = cat[doc] txt = doco.text muser = doco.muser mtime = doco.mtime.strftime('%d.%m.%Y %H:%M:%S') + ' /' + tzname[0] + '/' if plain: return txt else: doct = '
' + self.path(doco.id, doc = True) + '
\n← Обратно в раздел "' + cat.name + '"\n\n
\n' + txt if any_of(member_of('admins'), member_of('editors'))(): doct += '
\n\n\n\n\n\n\n
\n' doct += '
\nОтредактировано пользователем: ' + muser.dname + '[' + muser.name + '].
\n
Дата редактирования: ' + mtime + '\n
\n' doct += '
\n← Обратно в раздел "' + cat.name + '"\n\n
\n' return self.buildhtml('Архив[' + doco.name + ']', doct) @cherrypy.expose def getobj(self, lybsrcobj = None): obj = lybtools.getbin(cherrypy.session, lybsrcobj) cherrypy.response.headers["Content-Type"] = obj['type'] return obj['body'] @cherrypy.expose def menu_img(self): cherrypy.response.headers["Content-Type"] = 'image/png' return lybhtdata.m_i() def menupanel(self, mdict): menu = lybtools.menu_gen(mdict) menu += lybhtdata.form_minisearch username = cherrypy.session.get(lybtools.SESSION_KEY, None) if username: menu += lybhtdata.form_user.format(username=username) else: menu += lybhtdata.form_auth.format(from_page=self.from_page) return menu def buildhtml(self, title, doc): return lybhtdata.html.format(title=title, style='/css', body=lybhtdata.body.format(menu = self.menupanel(self.mdict), document = doc))