Add CRUDMixin

master
Steven Loria 11 years ago
parent 41ccc34e83
commit 7328186086
  1. 36
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/database.py
  2. 3
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py
  3. 6
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/public/views.py
  4. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/nav.html
  5. 5
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py
  6. 1
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/views.py

@ -3,6 +3,38 @@
mixins. mixins.
''' '''
from flask.ext.sqlalchemy import SQLAlchemy from .extensions import db
db = SQLAlchemy() class CRUDMixin(object):
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
@classmethod
def get_by_id(cls, id):
if any(
(isinstance(id, basestring) and id.isdigit(),
isinstance(id, (int, float))),
):
return cls.query.get(int(id))
return None
@classmethod
def create(cls, **kwargs):
instance = cls(**kwargs)
return instance.save()
def update(self, commit=True, **kwargs):
for attr, value in kwargs.iteritems():
setattr(self, attr, value)
return commit and self.save() or self
def save(self, commit=True):
db.session.add(self)
if commit:
db.session.commit()
return self
def delete(self, commit=True):
db.session.delete(self)
return commit and db.session.commit()

@ -6,3 +6,6 @@ bcrypt = Bcrypt()
from flask.ext.login import LoginManager from flask.ext.login import LoginManager
login_manager = LoginManager() login_manager = LoginManager()
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()

@ -3,7 +3,6 @@
from flask import (Blueprint, request, render_template, flash, url_for, from flask import (Blueprint, request, render_template, flash, url_for,
redirect, session) redirect, session)
from flask.ext.login import login_user, login_required, logout_user from flask.ext.login import login_user, login_required, logout_user
from sqlalchemy.exc import IntegrityError
from {{cookiecutter.repo_name}}.extensions import login_manager from {{cookiecutter.repo_name}}.extensions import login_manager
from {{cookiecutter.repo_name}}.user.models import User from {{cookiecutter.repo_name}}.user.models import User
@ -16,10 +15,7 @@ blueprint = Blueprint('public', __name__, static_folder="../static")
@login_manager.user_loader @login_manager.user_loader
def load_user(id): def load_user(id):
try: return User.get_by_id(int(id))
return User.query.get(int(id))
except Exception:
return None
@blueprint.route("/", methods=["GET", "POST"]) @blueprint.route("/", methods=["GET", "POST"])

@ -17,7 +17,7 @@
<!-- Collect the nav links, forms, and other content for toggling --> <!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse"> <div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li class="active"><a href="{{ url_for('public.home') }}">Home</a></li> <li><a href="{{ url_for('public.home') }}">Home</a></li>
<li><a href="{{ url_for('public.about') }}">About</a></li> <li><a href="{{ url_for('public.about') }}">About</a></li>
</ul> </ul>
{% if current_user.is_authenticated() %} {% if current_user.is_authenticated() %}

@ -3,14 +3,13 @@ import datetime as dt
from flask.ext.login import UserMixin from flask.ext.login import UserMixin
from {{cookiecutter.repo_name}}.database import db from {{cookiecutter.repo_name}}.database import db, CRUDMixin
from {{cookiecutter.repo_name}}.extensions import bcrypt from {{cookiecutter.repo_name}}.extensions import bcrypt
class User(UserMixin, db.Model): class User(UserMixin, CRUDMixin, db.Model):
__tablename__ = 'users' __tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False) username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(80), unique=True, nullable=False)
password_hash = db.Column(db.String, nullable=False) password_hash = db.Column(db.String, nullable=False)

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from flask import Blueprint, render_template from flask import Blueprint, render_template
from flask.ext.login import login_required from flask.ext.login import login_required

Loading…
Cancel
Save