# -*- coding: utf-8 -*-

#########################################################################
## This scaffolding model makes your app work on Google App Engine too
## File is released under public domain and you can use without limitations
#########################################################################

## if SSL/HTTPS is properly configured and you want all HTTP requests to
## be redirected to HTTPS, uncomment the line below:
# request.requires_https()

## app configuration made easy. Look inside private/appconfig.ini
from gluon.contrib.appconfig import AppConfig
## once in production, remove reload=True to gain full speed
myconf = AppConfig(reload=True)


if not request.env.web2py_runtime_gae:
    ## if NOT running on Google App Engine use SQLite or other DB
    db = DAL(myconf.take('db.uri'), pool_size=myconf.take('db.pool_size', cast=int), check_reserved=['all'])
else:
    ## connect to Google BigTable (optional 'google:datastore://namespace')
    db = DAL('google:datastore+ndb')
    ## store sessions and tickets there
    session.connect(request, response, db=db)
    ## or store session in Memcache, Redis, etc.
    ## from gluon.contrib.memdb import MEMDB
    ## from google.appengine.api.memcache import Client
    ## session.connect(request, response, db = MEMDB(Client()))

## by default give a view/generic.extension to all actions from localhost
## none otherwise. a pattern can be 'controller/function.extension'
response.generic_patterns = ['*'] if request.is_local else []
## choose a style for forms
response.formstyle = myconf.take('forms.formstyle')  # or 'bootstrap3_stacked' or 'bootstrap2' or other
response.form_label_separator = myconf.take('forms.separator')


## (optional) optimize handling of static files
# response.optimize_css = 'concat,minify,inline'
# response.optimize_js = 'concat,minify,inline'
## (optional) static assets folder versioning
# response.static_version = '0.0.0'
#########################################################################
## Here is sample code if you need for
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - old style crud actions
## (more options discussed in gluon/tools.py)
#########################################################################

from gluon.tools import Auth, Service, PluginManager

auth = Auth(db)
service = Service()
plugins = PluginManager()

## create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)

## configure email
mail = auth.settings.mailer
mail.settings.server = 'logging' if request.is_local else myconf.take('smtp.server')
mail.settings.sender = myconf.take('smtp.sender')
mail.settings.login = myconf.take('smtp.login')

## configure auth policy
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.settings.reset_password_requires_verification = True

## set True or auth.has_membership(role='whatever') to enable /app/default/user/bulk_register
auth.settings.bulk_register_enabled = False

#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################

## after defining tables, uncomment below to enable auditing
# auth.enable_record_versioning(db)

def check_initialize():
    if not db().select(db.auth_user.ALL).first():        
        user1 = db.auth_user.insert(
            password = db.auth_user.password.validate('frosty')[0],
            email = 'jack.frost@gmail.com',
            first_name = 'Jack',
            last_name = 'Frost',
        )

        users = (user1,)
        
        sadmin_group_id = auth.add_group('super_admin', 'Admin powers + user administration')
        admin_group_id = auth.add_group('admin', 'Site content administration')
        member_group_id = auth.add_group('member', 'Access to membership info')

        for user in users:
            auth.add_membership(sadmin_group_id, user)
            auth.add_membership(admin_group_id, user)
            auth.add_membership(member_group_id, user)

cache.ram('db_initialized', check_initialize(), time_expire=None)

@@ -16,16 +16,108 @@
 tf-8 -*-
+%0Aimport logging%0Alogger = logging.getLogger(%22web2py.app.init%22)%0Alogger.setLevel(logging.DEBUG)
 %0A%0A######
@@ -4136,16 +4136,270 @@
 lize():%0A
+%0A    if not db().select(db.auth_group.ALL).first():%0A        auth.add_group('super_admin', 'Admin powers + user administration')%0A        auth.add_group('admin', 'Site content administration')%0A        auth.add_group('member', 'Access to membership info')%0A%0A
     if n
@@ -4464,12 +4464,114 @@
 user
-1
+s
  = 
+%5B%5D%0A%0A        # Uncomment and add your details to create a new user automatically%0A        #users.append(
 db.a
@@ -4591,24 +4591,25 @@
 rt(%0A        
+#
     password
@@ -4660,24 +4660,25 @@
 0%5D,%0A        
+#
     email = 
@@ -4705,24 +4705,25 @@
 m',%0A        
+#
     first_na
@@ -4739,24 +4739,25 @@
 k',%0A        
+#
     last_nam
@@ -4781,436 +4781,232 @@
     
-)%0A%0A        users = (user1,)%0A        %0A        sadmin_group_id = auth.add_group('super_admin', 'Admin powers + user administration')%0A        admin_group_id = auth.add_group('admin', 'Site content administration')%0A        member_group_id = auth.add_group('member', 'Access to membership info')%0A%0A        for user in users:%0A            auth.add_membership(sadmin_group_id, user)%0A            auth.add_membership(admin_
+#))%0A%0A        groups = db().select(db.auth_group.id).as_list()%0A        # Create groups if a new user was created%0A        %0A%0A        # Add the users to the groups%0A        for user in users:%0A            for group in 
 group
-_id, user)%0A
+s:%0A    
     
@@ -5037,23 +5037,19 @@
 hip(
-member_group_id
+group%5B'id'%5D
 , us

disable create user by default