here I go (hope there is enough room here...), and yes, I know it
looks sad ;):
Controller:
## -*- coding: utf-8 -*-
#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does
streaming)
## - call exposes all registered services (none by default)
#########################################################################
import os, sys, string, re
from collections import deque
@auth.requires_login()
def index():
"""
example action using the internationalization operator T and flash
rendered by views/default/index.html or views/generic.html
"""
#response.flash = T('P4 user account cleanup!')
return dict()
@auth.requires_login()
def test():
return(dict)
def data():
return dict(form=crud())
'''
def populateP4Users():
# proposed list to start -> suspect MANY more to come
non_active_list = [
'benoit.nadeau',
'bernard.sjiariel',
'binh.dang',
'bulucc',
'craigt',
'davidk',
'gabriels',
'kevin',
'kyans',
'max',
'mikem',
'nam.phan',
'nguyen.tran',
'nirbhay.vashi',
'rachel',
'rachelv',
'reuben',
'ron.prior',
'sanamm',
'sang.vo',
'thinh.phan',
'tom.zhang',
'trang.mai',
'van.nguyen',
'vesna.cakarevic',
'will',
'yosef',
'yuehuay'
]
form = SQLFORM(db.p4users)
if form.accepts(request.vars, session, dbio=False):
response.flash = T('in populateP4Users!')
f = open('./p4users.txt', 'r')
users = f.readlines()
f.close()
uInfo = deque()
garbageList = deque()
for user in users:
uDictList = {}
one = string.split(user, '<')
if one:
p4UserStatus = None
strP4_user = string.strip(one[0])
two = string.split(one[1],'>')
strMail = string.strip(two[0])
three = string.split(two[1],'(')
four = string.split(user, '(')
[1]
strName = string.split(four,')')[0]
last_accessDate = string.split(three[1],')')[1]
date = string.split(last_accessDate,'accessed')[1]
strAccessed = string.strip(date,'\\n')
if not 'tao' in user or not 'error:' in user:
if strP4_user in non_active_list:
p4UserStatus = 1
db.p4users.insert(p4_user=strP4_user,mail=strMail,p4_name=strName,accessed=strAccessed,p4status=p4UserStatus)
SQLFORM(table, record=None, deletable=False,
linkto=None, upload=None, fields=None, labels=None, col3={},
submit_button='Submit', delete_label='Check to delete:',
id_label='Record id: ', showid=True,
readonly=False, comments=True, keepopts=[],
ignore_rw=False, formstyle='table3cols',**attributes)
'''
def update_user():
form = SQLFORM(db.p4users_all,record)
if form.accepts(request.vars, session, dbio=False):
if form.vars.get('delete_this_record', False):
db(db.person.id==record.id).delete()
else:
record.update_record(**dict(form.vars))
response.flash = 'record updated'
def edit():
row = db.p4users_all[request.args(0)]
form = SQLFORM(db.p4users_all, row, deletable=True)
if form.accepts(request.vars, session):
response.flash = 'record updated'
return dict(form=form)
def display_form():
# record = db.p4users_all(request.args(0))
row = db.p4users_all[request.args(0)]
form = SQLFORM(db.p4users_all, row, deletable=True)
form.vars.id = request.vars.id
if form.accepts(request.vars, session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
def countUsers():
pass # will do this later -> for pie chart
'''
didn't work out -> page for for update wasn't 'pretty'
'''
@auth.requires_login()
def p4():
grid = webgrid.WebGrid(crud)
grid.crud_function = 'data'
grid.datasource = db(db.p4users.p4status==db.p4Status.id)
grid.pagesize = 200
grid.fields = ['p4users.id',
'p4users.p4_user',
'p4users.mail',
'p4users.p4_name',
'p4users.accessed',
'p4users.p4status',
'p4users.license_status',
'p4users.comments']
# grid.filters =
['summitTrackableItems.responsible','summitTrackableItems.status']
grid.filters = ['p4users.p4status']
grid.filter_query = lambda f, v: f >= v
return dict(grid=grid())
def user():
"""
exposes:
http://..../[app]/default/user/login
http://..../[app]/default/user/logout
http://..../[app]/default/user/register
http://..../[app]/default/user/profile
http://..../[app]/default/user/retrieve_password
http://..../[app]/default/user/change_password
use @auth.requires_login()
@auth.requires_membership('group name')
@auth.requires_permission('read','table name',record_id)
to decorate functions that need access control
"""
return dict(form=auth())
def download():
"""
allows downloading of uploaded files
http://..../[app]/default/download/[filename]
"""
return response.download(request,db)
def call():
"""
exposes services. for example:
http://..../[app]/default/call/jsonrpc
decorate with @services.jsonrpc the functions to expose
supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
"""
session.forget()
return service()
Model:
# -*- coding: utf-8 -*-
#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################
if request.env.web2py_runtime_gae: # if running on Google
App Engine
db = DAL('gae') # connect to Google
BigTable
session.connect(request, response, db = db) # and store sessions
and tickets there
### or use the following lines to store sessions in Memcache
# from gluon.contrib.memdb import MEMDB
# from google.appengine.api.memcache import Client
# session.connect(request, response, db = MEMDB(Client()))
else: # else use a normal
relational database
db = DAL('sqlite://storage.sqlite') # if not, use SQLite or
other DB
## if no need for session
# session.forget()
#########################################################################
## 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)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################
from gluon.tools import *
mail = Mail() # mailer
auth = Auth(globals(),db) # authentication/
authorization
crud = Crud(globals(),db) # for CRUD helpers
using auth
service = Service(globals()) # for json, xml,
jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()
mail.settings.server = 'logging' or 'smtp.gmail.com:587' # your SMTP
server
mail.settings.sender = '[email protected]' # your email
mail.settings.login = 'username:password' # your credentials or
None
auth.settings.hmac_key =
'sha512:15cd54c5-8b0a-400d-8a39-6bf56b40696a' # before
define_tables()
auth.define_tables() # creates all needed
tables
auth.settings.mailer = mail # for user email
verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link
http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['verify_email'])+'/%(key)s
to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link
http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['reset_password'])+'/%(key)s
to reset your password'
#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin,
etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
#
auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request,
api_key='...',domain='...',
# url = "http://localhost:8000/%s/default/user/login" %
request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################
crud.settings.auth = None # =auth to enforce
authorization on crud
import string
from collections import deque
import datetime
from gluon.contrib.populate import populate
'
# REFERENCE
SQLFORM(table, record=None, deletable=False,
linkto=None, upload=None, fields=None, labels=None, col3={},
submit_button='Submit', delete_label='Check to delete:',
id_label='Record id: ', showid=True,
readonly=False, comments=True, keepopts=[],
ignore_rw=False, formstyle='table3cols',**attributes)
'''
db.define_table('license',
SQLField('licenseStatus'),format='%(licenseStatus)s')
db.define_table('p4Status',
SQLField('userStatus'),format='%(userStatus)s')
#db.define_table('confirmedInactive',
# SQLField('name', db.p4users, default=p4users.id))
# SQLField('logged_in', db.auth_user,
default=auth.user_id),
#########################
# requests and stuff....
#########################
db.define_table('p4users',
SQLField('p4_user'),format='%(p4_user)s')
#SQLField('mail'),
#SQLField('p4_name'),
#SQLField('accessed'),
#SQLField('license_status',db.license),
#SQLField('p4status',db.p4Status),
#SQLField('comments'))
#db.p4users.p4_user.requires=[IS_IN_DB(db,'p4Status.id')]
#db.p4users.p4_user.requires=[IS_NOT_EMPTY()]
#db.p4users.mail.requires=IS_NOT_EMPTY()
#db.p4users.p4_name.requires=IS_NOT_EMPTY()
#db.define_table('person',
# Field('name', requires=IS_NOT_EMPTY()))
#db.define_table('dog',
# Field('owner', db.person),
# Field('name', requires=IS_NOT_EMPTY()))
#db.dog.owner.requires = IS_IN_DB(db,db.person.id,'%(name)s')
#########################
# requests and stuff....
#########################
db.define_table('p4users_all',
SQLField('Name', db.auth_user, default=auth.user_id),
# SQLField('User',db.p4users),
SQLField('User_id',requires=IS_IN_DB(db,db.p4users.id,'%
(p4_user)s')),
SQLField('Status',db.p4Status),
SQLField('License',db.license))
#db.p4users_all.User_id.requires = IS_IN_DB(db,db.p4users.id,'%
(p4_user)s')
#db.p4users_all.User_id.requires=[IS_IN_DB(db,'p4users.id')]
#SQLField('License',db.license)
#db.p4users_all.pUser.requires=[IS_IN_DB(db,'p4users.p4_user')]
#db.p4users_all.pStatus.requires=[IS_IN_DB(db,'p4Status.id')]
#db.updatedUsers.p4user.requires=[IS_IN_DB(db,'p4users.p4_user')]
#db.updatedUsers.activeStatus.requires=[IS_IN_DB(db,'p4Status.id')]
#db.p4users.drop()
'''
non_active_list = [
'benoit.nadeau',
'bernard.sjiariel',
'binh.dang',
'bulucc',
'craigt',
'davidk',
'gabriels',
'kevin',
'kyans',
'max',
'mikem',
'nam.phan',
'nguyen.tran',
'nirbhay.vashi',
'rachel',
'rachelv',
'reuben',
'ron.prior',
'sanamm',
'sang.vo',
'thinh.phan',
'tom.zhang',
'trang.mai',
'van.nguyen',
'vesna.cakarevic',
'will',
'yosef',
'yuehuay'
]
f = open('./p4users.txt', 'r')
users = f.readlines()
f.close()
uInfo = deque()
garbageList = deque()
for user in users:
uDictList = {}
one = string.split(user, '<')
if one:
p4UserStatus = None
strP4_user = string.strip(one[0])
two = string.split(one[1],'>')
strMail = string.strip(two[0])
three = string.split(two[1],'(')
four = string.split(user, '(')[1]
strName = string.split(four,')')[0]
last_accessDate = string.split(three[1],')')[1]
date = string.split(last_accessDate,'accessed')[1]
strAccessed = string.strip(date,'\\n')
if not 'tao' in user or not 'error:' in user:
if strP4_user in non_active_list:
p4UserStatus = 1
db.p4users.insert(p4_user=strP4_user,mail=strMail,p4_name=strName,accessed=strAccessed,p4status=p4UserStatus)
'''
#if request.controller == 'p4' and request.function == 'data':
# if request.args:
# crud.settings[request.args(0)+'_next'] =
URL(r=request,f='p4UserCleanup')
USING plugin_wiki (just the one page):
[[ /nmc_rm_Portal/static/images/rm_logo.png right]]
-------
## P4 User Cleanup Event!
------
``
{{=plugin_wiki.widget('load_action',
action='display_form.load',
controller='default',
ajax='True')}}``:template
*** note to self:
*** THIS IS NOT WORKING!!!!! :( must fix, or remove it
``
name: pie_chart
data: len(db().select(db.p4users_all.id)),167
names: active,non-active
width: 300
height: 150
``:widget
``
<div class="seachOne" id="seachIdOne"><H2>[search]</H2></div>
<div class="seachTwo hidden"
id="seachIdTwo">{{=plugin_wiki.widget('search',
table='p4users',
fields='name,value')}}</div>``:template``
<div class="update_createOne" id="update_createIdOne"><H2>[create/
update p4 user]</H2></div>
<div class="update_createTwo hidden"
id="update_createTwo">{{=plugin_wiki.widget('update',
table='p4users_all',
record_id='User',
message='updated user status',
next='/p4UsersCleanup/plugin_wiki/page/
p4usercleanp')}}</div>``:template``
<div class="mailOne" id="mailIdOne"><H2>[email questions or comments]</
H2></div>
<div class="mailTwo hidden"
id="mailIdTwo">{{=plugin_wiki.widget('ask',
email_label='[email protected]',
question_label='question/comment')}}</
div>``:template``
<script>
jQuery('.seachOne').click(function()
{jQuery('.seachTwo').slideToggle()});
jQuery('.update_createOne').click(function()
{jQuery('.update_createTwo').slideToggle()});
jQuery('.mailOne').click(function()
{jQuery('.mailTwo').slideToggle()});
</script>
{{=plugin_wiki.widget('jqgrid',
table='p4users_all',
col_width='100',
width='700',
height='400')}}</div>``:template
``
name: comments
table: p4Users.Comments
``:widget
````
On Sep 27, 12:23 pm, mart <[email protected]> wrote:
> Alright :) it may be "not good looking" right now, but I'll gladly
> post my novice web2py code :) Which parts would be useful?
>
> thanks,
> Mart :)
>
> On Sep 27, 12:10 pm, mdipierro <[email protected]> wrote:
>
> > This
>
> > row = db.p4users_all(request.args(0))
> > form = SQLFORM(db.p4users_all, row, deletable=True)
>
> > in your code should not create a new db.p4users_all unless row is
> > None. Check the value of row. If it is none you are passing something
> > wrong for request.args(0). Hard to tell without the rest of the code
> > and/or an example.
>
> > On Sep 27, 10:12 am, mart <[email protected]> wrote:
>
> > > Hi
>
> > > so, I have this table:
>
> > > db.define_table('p4users',
> > > SQLField('p4_user'),format='%(p4_user)s')
>
> > > which gives me a nice dropdown :)
>
> > > then I have this one, where I want to update the Status field.
>
> > > db.define_table('p4users_all',
> > > SQLField('Name', db.auth_user, default=auth.user_id),
> > > # SQLField('User',db.p4users),
> > > SQLField('User_id',requires=IS_IN_DB(db,db.p4users.id,'%
> > > (p4_user)s')),
> > > SQLField('Status',db.p4Status),
> > > SQLField('License',db.license))
>
> > > in the controller, I have something like this (i know a little messy,
> > > but I am in a rush ... I have been grasping at this point ;) )
>
> > > def display_form():
> > > # record = db.p4users_all(request.args(0))
> > > row = db.p4users_all[request.args(0)]
> > > form = SQLFORM(db.p4users_all, row, deletable=True)
> > > form.vars.id = request.vars.id
> > > if form.accepts(request.vars, session):
>
> > > response.flash = 'form accepted'
> > > elif form.errors:
> > > response.flash = 'form has errors'
> > > return dict(form=form)
>
> > > Any help, would REALLY be appreciated! :)
>
> > > Thahnnks,
> > > Mart :)
>
> > > How do I make so it doesn't create a new record every time I want to
> > > update a user's status?
>
>