Advertisement
Guest User

flask-admin-modelview-custom-validation

a guest
Apr 14th, 2014
2,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. from flask import Flask
  2. import peewee
  3. from flask.ext import admin
  4. from flask.ext.admin.contrib.peewee import ModelView
  5.  
  6. app = Flask(__name__)
  7. app.config['SECRET_KEY'] = '123456790'
  8. db = peewee.SqliteDatabase('test.sqlite', check_same_thread=False)
  9.  
  10. class BaseModel(peewee.Model):
  11.     class Meta:
  12.         database = db
  13.  
  14. class User(BaseModel):
  15.     username = peewee.CharField(max_length=80, unique=True, null = False)
  16.     email = peewee.CharField(max_length=120)
  17.     def __unicode__(self):
  18.         return self.username
  19.  
  20. class UserInfo(BaseModel):
  21.     key = peewee.CharField(max_length=64)
  22.     value = peewee.CharField(max_length=64)
  23.     user = peewee.ForeignKeyField(User)
  24.     def __unicode__(self):
  25.         return '%s - %s' % (self.key, self.value)
  26.  
  27. class Post(BaseModel):
  28.     title = peewee.CharField(max_length=120)
  29.     text = peewee.TextField(null=False)
  30.     date = peewee.DateTimeField()
  31.     user = peewee.ForeignKeyField(User)
  32.     def __unicode__(self):
  33.         return self.title
  34.  
  35. from wtforms.validators import ValidationError
  36. class UserAdmin(ModelView):
  37.     def no_root_allowed(form, field):
  38.         if field.data == 'root':
  39.             raise ValidationError('"root" is not allowed')
  40.  
  41.     inline_models = (UserInfo,)
  42.     form_args = dict(
  43.         username=dict(validators=[no_root_allowed])
  44.     )
  45.    
  46.     def on_model_change(self, form, model, is_created):
  47.         return super(UserAdmin, self).on_model_change(form, model, is_created)
  48.        
  49.     def update_model(self, form, model):
  50.         return super(UserAdmin, self).update_model(form, model)
  51.        
  52.     def create_model(self, form):
  53.         return super(UserAdmin, self).create_model(form)
  54.        
  55.     def delete_model(self, model):
  56.         return super(UserAdmin, self).delete_model(model)
  57.        
  58.     pass
  59.  
  60. class PostAdmin(ModelView):
  61.     column_exclude_list = ['text']
  62.     column_sortable_list = ('title', ('user', User.email), 'date')
  63.     column_searchable_list = ('title', User.username)
  64.     column_filters = ('title', 'date', User.email)
  65.     form_ajax_refs = { 'user': { 'fields': (User.username, 'email') } }
  66.  
  67. @app.route('/')
  68. def index():
  69.     return '<a href="/admin/">Click me to get to Admin!</a>'
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     import logging
  74.     logging.basicConfig()
  75.     logging.getLogger().setLevel(logging.DEBUG)
  76.  
  77.     admin = admin.Admin(app, 'Peewee Models')
  78.  
  79.     admin.add_view(UserAdmin(User))
  80.     admin.add_view(PostAdmin(Post))
  81.  
  82.     try:
  83.         User.create_table()
  84.         UserInfo.create_table()
  85.         Post.create_table()
  86.     except:
  87.         pass
  88.  
  89.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement