2018年6月28日 星期四

用Flask-SQLAlchemy把Model定義獨立到models.py

https://stackoverflow.com/questions/9692962/flask-sqlalchemy-import-context-issue/9695045#9695045

models.py
=====
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)

def __repr__(self):
return '%r>' % self.username

class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
body = db.Column(db.Text, nullable=False)
json = db.Column(db.JSON)
pub_date = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)

category_id = db.Column(db.Integer, db.ForeignKey('category.id'),
nullable=False)
category = db.relationship('Category',
backref=db.backref('posts', lazy=True))

def __repr__(self):
return '%r>' % self.title

class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)

def __repr__(self):
return '%r>' % self.name


app.py
======
from flask import Flask
# from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from models import db, User, Post, Category

app = Flask(__name__)
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///jack'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# db = SQLAlchemy(app)
db.init_app(app)
admin = Admin(app, name='flask-sql', template_mode='bootstrap3')

admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(Post, db.session))
admin.add_view(ModelView(Category, db.session))

2018年6月22日 星期五

Ubuntu install postgresql & flask-sqlalchemy

# sudo apt install postgresql
# sudo systemctl enable postgresql
# sudo -i -u postgres
# createuser --interactive
jack
y
Note. already have a user named jack
# createdb jack
Note. create a database named jack.
logout from postgres
#logout

Edit the Flask app.py
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///jack'

You can save JSON type now
json = db.Column(db.JSON)

2018年6月21日 星期四

遇到密碼對 可是登不進ssh

Check the log in /var/log/auth.log
$ grep sshd /var/log/auth.log | less
看到
sshd[2110]: User jack not allowed because shell /usr/bin/zsh does not exist

可能是之前有安裝過zsh但是沒有移乾淨
下env | grep SHELL看到的確是/usr/bin/zsh

用chsh指令改
$ sudo chsh -s /bin/bash jack
解決不能登入ssh問題

Robotframework 變數檔案

使用-V 參數

# robot -V vars.yaml a.robot

vars.yaml內容
BMC_IP: 172.16.1.16

For using "Library    Dialogs"
apt-get install python3-tk
pip intall tkinter