def GET(self,link_id=None,page=1,success=False,csrf_error=False,create=False,cl='en',ct='?',cp=None,cu='http://',*args,**kwargs): """Can display 3 kinds of pages (depending on args): 1) If link_id is supplied (url was /link/<n>), shows the link's edit form 2) If create is not False, shows a create form with values from cl,ct,cp,cu 3) Otherwise, shows "editor view" of all links (with edit button per link, and a create button) """ conf = cherrypy.request.app.config['ligiloj'] try: page = max(int(page),1) except: raise cherrypy.HTTPError(404,"nevalida paĝo nombro :(") if not link_id and not create: #--- 3rd option: show a list of all links query = models.Link().select(models.Link,models.Language,models.Link.id).join(models.Language) result = { #'debug':[cherrypy.request.script_name,cherrypy.request.path_info], 'user':cherrypy.serving.user.name, 'title':'Redaktilo', 'site_title':conf['site_title'], 'url_base':cherrypy.request.base, 'site_root':cherrypy.request.base+cherrypy.request.script_name+'/', 'here':cherrypy.request.base+cherrypy.request.script_name+cherrypy.request.path_info+'/', 'success':success, 'links':query.paginate(page,cherrypy.request.config['paginate_by']).dicts() } result.update(nextprev(query.count(),page,cherrypy.request.config['paginate_by'])) return stache.render(stache.load_template('edit_links'),result) elif create: #--- 2nd option: show a creation form try: # I'm sure there's a simpler way :) cp = datetime.date.fromtimestamp(time.mktime(time.strptime(cp,'%Y-%m-%d'))) except: cp = datetime.date.today() l = models.Link( published=cp, title=ct, url=cu, language=models.get_language(cl) or models.get_language('eo')) title = u'Aldonu ligilon' fancy_title =u'<i class="glyphicon glyphicon-plus"></i> Aldonu ligilon' else: #--- 1st option: show edit form for an existing link try: # get link to edit l = models.Link.get(id=link_id) except ValueError,models.DoesNotExist: raise cherrypy.HTTPError(404,"Ligilo ne estas trovita :(") title = u'Redaktu ligilon: {0}'.format(l.__unicode__()) fancy_title =u'<i class="glyphicon glyphicon-edit"></i> Redaktu ligilon'
def create_users(api): logger.info("KIMAI2 => Creating users") # check and collect info try: tz = models.get_timezone(models.session_factory()).value lang = models.get_language(models.session_factory()).value except Exception: logging.exception("Failed to create users") logger.info("KIMAI2 => Finished creating users") return if tz is None: logger.error("Failed to create user in kimai2, no timezone defined.") logger.info("KIMAI2 => Finished creating users") return elif lang is None: logger.error("Failed to create user in kimai2, no language defined.") logger.info("KIMAI2 => Finished creating users") return # collect not created users session = models.session_factory() users = models.get_new_kimai2_users(session) users_counter = 0 # create users for user in users: try: resp = api.create_user(user.k2_name, user.k2_email, lang, tz, user.k2_password) if resp.status_code != 200: # wrong hhtp response, log error logger.error("Failed to create user in Kimai2") logger.error("Status: {}, Values: {}, {}, {}, {} {}".format( str(resp.status_code), user.k2_name, user.k2_email, lang, tz, user.k2_password)) logger.error(resp.json()) else: # right http response, update local database obj = resp.json() models.set_user_kimai2_id(user, obj['id'], session) logger.info("User created: {}, {}".format( obj['id'], obj['username'])) users_counter += 1 except Exception: logging.exception(f"Failed to create user {user.name}") session.close() logger.info(f"Total users created: {users_counter}") logger.info("KIMAI2 => Finished creating users")
def rss(self,language=None,*args,**kwargs): if language: l = models.get_language(language) conf = cherrypy.request.app.config['ligiloj'] query = models.Link().select(models.Link,models.Language).join(models.Language) if language: query = query.where(models.Link.language == l) cherrypy.response.headers['Content-Type'] = 'application/xml' return RSS2(title=u'{0} - {1}'.format(conf['site_title'],language and l.name or conf['global_title_text']), link=conf['rss_site_url'], description=conf['rss_description'], language=language or conf['rss_default_language'], items=[RSSItem(title=language and link.title or u"{0}: {1}".format(link.language.name,link.title), link=link.url, pubDate=link.published.isoformat(), guid=Guid(link.url,str(link.id))) for link in query]).to_xml('utf-8')
def default(self,*args,**kwargs): """The end user method at /[language/][page]""" arglist = list(args) if not arglist or not arglist[-1].isdigit(): arglist.append('1') if len(arglist)<2: arglist.insert(0,None) try: page = max(1,int(arglist[1])) except: raise cherrypy.HTTPError(404,"Nevalida nombro :(") lang = arglist and arglist[0] or None language = None if lang is not None: language = models.get_language(lang) if language is None: raise cherrypy.HTTPError(404,"Nekonata lingvo") conf = cherrypy.request.app.config['ligiloj'] page_title = language and language.__unicode__().split(':')[-1].strip() or conf['global_title_text'] menu,active_language = make_menu(lang) query = models.Link().select(models.Link,models.Language).join(models.Language) if language: query = query.where(models.Link.language == language) try: user = cherrypy.serving.user and cherrypy.serving.user.name except: user = None result = { #'debug':[cherrypy.request.base,cherrypy.request.script_name,cherrypy.request.path_info], 'user':user, 'title':u'{0} - {1}'.format(conf['site_title'],page_title), 'fancy_title':u'{0} <small>{1}</small>'.format( conf['site_title_html'], language and page_title or conf['global_title_html']), 'site_root':cherrypy.request.base+cherrypy.request.script_name+'/', 'lang':lang or 'en', 'menu':menu, 'active_language':active_language, 'is_international':lang is None, 'count':query.count(), 'links':query.paginate(page,cherrypy.request.config['paginate_by']).dicts() } result.update(nextprev(query.count(),page,cherrypy.request.config['paginate_by'])) skin = kwargs.get('skin','index') return stache.render(stache.load_template(skin),result)
def create_team_leader(api, force=False): logger.info("KIMAI2 => Creating team leader") try: # check if is already created leader_id = models.get_leader(models.session_factory()).k2_id if leader_id is not None and not force: logger.info("Team leader no created. Team leader already exists.") logger.info("KIMAI2 => Finished creating team leader") return # collect info username = models.get_leader(models.session_factory()).value email = models.get_leader_email(models.session_factory()).value lang = models.get_language(models.session_factory()).value tz = models.get_timezone(models.session_factory()).value password = models.get_leader_password(models.session_factory()).value # create team leader in Kimai2 resp = api.create_team_leader(username, email, lang, tz, password) if resp.status_code != 200: logger.error("Failed to create team leader in Kimai2: %s", username) logger.error("Status: {}, Values: {}, {}, {}, {}".format( str(resp.status_code), username, email, lang, tz)) logger.error(resp.json()) return # update local database obj = resp.json() models.set_leader(obj['username'], models.session_factory(), obj['id']) logger.info("Team leader created: {}, {}".format( obj['id'], obj['username'])) except Exception: logging.exception("Failed to create a team leader in Kimai2") logger.info("KIMAI2 => Finished creating team leader")