def make_project(self): """ Creates the django project and some templates. """ call_command("startproject", self.project_name) os.chdir(self.project_name) self._create_dir("templates") self._create_dir("templates", "index") self._create_file(self._get_dir("templates", "index", "index.html"), render_template("index.html")) if self.public_app: self._create_dir("templates", "oauth") self._create_file(self._get_dir("templates", "oauth", "login.html"), render_template("login.html"))
def _make_launcher_file(self): content = render_template(filename="launcher.py.j2", import_path=self.import_path, name=self.name) filename = join(self.project.base_dir, self.name + "Launcher.py") with open(filename, "w") as fd: fd.write(content)
def send_email(routes_to_report): """Send email using sendgrid.""" number_of_routes = len(routes_to_report) if number_of_routes == 0: return False formatted_date = dt.datetime.utcnow().strftime("%A, %b %d") rich_email_body = render_template("email.html", routes=routes_to_report, date=formatted_date) sg = SendGridClient(config.SG_USERNAME, config.SG_PASSWORD, raise_errors=True) formatted_time = dt.datetime.utcnow().strftime("%F %T") subject = '({}): {} routes' subject = subject.format(formatted_time, number_of_routes) try: message = Mail(to=config.TO_EMAIL, subject=subject, html=rich_email_body, from_email=config.FROM_EMAIL) status, msg = sg.send(message) return msg except SendGridClientError as e: print 'Failed to send email: ', e raise
def _make_job_file(self): content = render_template( filename="job-template.job.j2", type=self._job_type, dependencies=[d._job_name() for d in self.dependencies], name=self.name, retries=getattr(self.class_obj, "retries", 0), retry_backoff=getattr(self.class_obj, "retry_backoff", 0)) filename = join(self.project.base_dir, self._job_filename()) with open(filename, "w") as fd: fd.write(content)
def _make_job_file(self): content = render_template( filename="job-template.job.j2", type=self._job_type, dependencies=[d._job_name() for d in self.dependencies], name=self.name, retries=getattr(self.class_obj, "retries", 0), retry_backoff=getattr(self.class_obj, "retry_backoff", 0), ) filename = join(self.project.base_dir, self._job_filename()) with open(filename, "w") as fd: fd.write(content)
def setup_urls(self): """ Replaces the django default urls.py for a template we provide. """ print AnsiColors.OKCYAN + "Configuring " + AnsiColors.WHITE + "urls.py" + AnsiColors.OKCYAN + "..." + AnsiColors.ENDC if self.public_app: template_name = "urls_public" else: template_name = "urls" urls_file = render_template(template_name, {"app_name": self.app_name }) self._create_file("urls.py", urls_file)
def manualTestPageContent(environ): deviceList = functions.DeviceDB.query_all_devices() if os.path.exists(UploadPath): filelist = os.listdir(UploadPath) else: filelist = [] os.mkdir(UploadPath) template = templates.get_template( 'ManualTestPage.html' ) templateVars = { 'devicelist': deviceList, 'filelist':filelist } return templates.render_template(template, templateVars)
def make_default_app(self): """ Creates the django app, the views root folder and some example views. """ print AnsiColors.OKCYAN + "Starting Django app..." + AnsiColors.ENDC call_command("startapp", self.app_name) os.remove(self._get_app_dir("views.py")) self._create_dir(self.app_name, "views") self._create_file(self._get_app_dir("views", "__init__.py"), "") self._create_file(self._get_app_dir("views", "index.py"), render_template("index")) os.chdir(self.project_name)
def setup_settings(self): """ Replaces the django default settings.py for a template we provide. """ print AnsiColors.OKCYAN + "Configuring " + AnsiColors.WHITE + "settings.py" + AnsiColors.OKCYAN + "..." + AnsiColors.ENDC # Create a random SECRET_KEY hash to put it in the main settings. chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' secret_key = get_random_string(50, chars) if self.public_app: template_name = "settings_public" else: template_name = "settings" settings_file = render_template(template_name, {"app_name": self.app_name, "project_name": self.project_name, "secret_key": secret_key }) self._create_file("settings.py", settings_file)
async def index(request): statements = await parse() random.shuffle(statements) return html(render_template('index.html', statements=statements))
def get(self): self._init() self.response.out.write(templates.render_template('latest.rss', { 'items': self.items }))
def GET(self): logger.debug('Rendering index') web.header('Content-Type', 'text/html') return render_template('index.html')
def create_nginx_config(self): ''' Create Nginx config file >>> from serverenv import ServerEnviroment >>> import os >>> from datetime import datetime >>> today = datetime.today() >>> os.system('mkdir -m 1777 /tmp/config') 0 >>> os.system('mkdir -m 1777 /tmp/nginx_conf') 0 >>> path = os.path.realpath('enviroment.py') >>> enviroment = ServerEnviroment('postgres', 'gerrard', ... 'thecaptain', '', '/tmp', ... '/home/openerp/instancias/estable/agrinos/server') >>> enviroment.create_postgres_user() True >>> enviroment.create_linux_user() True >>> enviroment.create_database('test_env') True >>> enviroment.create_config_file() True >>> os.system('sudo fuser -k 759/tcp') 0 >>> enviroment.port = 8069 >>> enviroment.create_nginx_config() True >>> os.system('sudo su -c "dropdb test_env" postgres') 0 >>> os.system('sudo su -c "dropuser gerrard" postgres') 0 >>> os.system('sudo fuser -k 759/tcp') 0 >>> os.system("sudo userdel gerrard -r") 0 >>> os.system('rm /tmp/nginx_conf -r') 0 >>> os.system('rm /tmp/config -r') 0 ''' try: nginx_folder = os.path.join(self.config_folder, 'nginx_conf/') nginx_conf = os.path.join(self.config_folder, 'nginx_conf', 'cfdi_main_nginx.conf') if os.path.isfile(nginx_conf): domain = ' server_name %s.%s;\n' % (self.database, self.sdomain) main_nginx = open(nginx_conf, "rw") lines = main_nginx.readlines() if domain in lines: return True main_nginx = open(nginx_conf, "rw") tmp = open('/tmp/temp.conf', "w") done = False for i in lines: if 'server {' in i and not done: content = templates.\ render_template('server_nginx.mako', r=self) tmp.write(content) tmp.write(i) done = True else: tmp.write(i) main_nginx.close() tmp.close() os.popen('sudo mv /tmp/temp.conf %s' % nginx_conf) else: f = open(nginx_conf, "w") content = templates.render_template('nginx.conf.mako', r=self) f.write(content) f.close() nginx_pid_path = os.path.join(nginx_folder, 'nginx', 'nginx.pid') if os.path.isfile(nginx_pid_path): try: os.system('sudo fuser -k %s/tcp' % self.nport) time.sleep(3) os.popen("sudo nginx -p %s -c %s" % \ (nginx_folder, "cfdi_main_nginx.conf" )) except OSError: log('ERROR: cannot reload nginx config') else: os.popen("sudo nginx -p %s -c %s" % \ (nginx_folder, "cfdi_main_nginx.conf" )) return True except Exception, error: return error