Skip to content

Commit

Permalink
added profile support
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Dec 13, 2011
1 parent 9e6dc95 commit 0a11f35
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 10 deletions.
4 changes: 2 additions & 2 deletions demo.ini
@@ -1,8 +1,8 @@
[app:main]
paste.app_factory = demo:main
session.type = file
session.data_dir = /tmp/eventq/data/sessions/data
session.lock_dir = /tmp/eventq/data/sessions/lock
session.data_dir = /tmp/pyramid_signup/data/sessions/data
session.lock_dir = /tmp/pyramid_signup/data/sessions/lock
sqlalchemy.url = sqlite:////%(here)s/test.db

su.using_tm = true
Expand Down
23 changes: 23 additions & 0 deletions pyramid_signup/__init__.py
Expand Up @@ -20,6 +20,29 @@

from pyramid_signup.routes import build_routes

def groupfinder(userid, request):
user = request.user
groups = []

if user:
for org in user.organizations:
groups.append('organization:%s' % org.pk)

for group in user.groups:
groups.append('group:%s' % group.pk)

# if user.kind == 'admin':
# groups.append('group:admin')

# account_perms = session.query(AccountUserPermission) \
# .filter(AccountUserPermission.user_pk == user.pk).all()
#
# for account_perm in account_perms:
# if account_perm.has_access:
# groups.append('account:%s:%s' % (account_perm.account_pk, account_perm.key))
#
return groups

class SignUpRequestFactory(Request):
@reify
def user(self):
Expand Down
6 changes: 6 additions & 0 deletions pyramid_signup/interfaces.py
Expand Up @@ -26,3 +26,9 @@ class ISUResetPasswordForm(Interface):

class ISUResetPasswordSchema(Interface):
pass

class ISUProfileForm(Interface):
pass

class ISUProfileSchema(Interface):
pass
4 changes: 1 addition & 3 deletions pyramid_signup/models.py
Expand Up @@ -110,7 +110,7 @@ class Organization(SUEntity):
cancelled_reason = Column(UnicodeText, nullable=True)
owner_pk = Column(Integer, ForeignKey('user.pk'))
owner = relation('User', backref='owned_organizations')
users = relation('User', secondary=org_member_table, backref='member_accounts')
users = relation('User', secondary=org_member_table, backref='organizations')

def __init__(self, name, owner):
self.name = name
Expand Down Expand Up @@ -153,8 +153,6 @@ class User(SUEntity):
first_name = Column(UnicodeText)
last_name = Column(UnicodeText)
activated = Column(Boolean, default=False)
accounts = relation('Organization', secondary=org_member_table,
backref='user')
activation_pk = Column(Integer, ForeignKey('activation.pk'))
activation = relation('Activation', backref='user')
suspended = Column(Boolean, default=False)
Expand Down
10 changes: 8 additions & 2 deletions pyramid_signup/resources.py
Expand Up @@ -4,7 +4,12 @@

from pyramid_signup.managers import OrganizationManager

class RootFactory(object):
class BaseFactory(object):
def __init__(self, request):
self.request = request
self.is_root = False

class RootFactory(BaseFactory):
@property
def __acl__(self):
defaultlist = [
Expand All @@ -15,7 +20,8 @@ def __acl__(self):
return defaultlist

def __init__(self, request):
pass # pragma: no cover
super(RootFactory, self).__init__(request)
self.is_root = True

class OrganizationFactory(RootFactory):
def __init__(self, request):
Expand Down
2 changes: 1 addition & 1 deletion pyramid_signup/routes.py
Expand Up @@ -6,4 +6,4 @@ def build_routes(config):
config.add_route('activate', '/activate/{code}')
config.add_route('forgot_password', '/forgot_password')
config.add_route('reset_password', '/reset_password/{code}')

config.add_route('profile', '/profile')
13 changes: 13 additions & 0 deletions pyramid_signup/schemas.py
Expand Up @@ -30,3 +30,16 @@ class ResetPasswordSchema(CSRFSchema):
validator=colander.Length(min=2),
widget=deform.widget.CheckedPasswordWidget())

class ProfileSchema(CSRFSchema):
Username = colander.SchemaNode(colander.String(),
widget=deform.widget.TextInputWidget(template='readonly/textinput'),
missing=colander.null,
)
Email = colander.SchemaNode(colander.String(),
validator=colander.Email())
First_Name = colander.SchemaNode(colander.String())
Last_Name = colander.SchemaNode(colander.String())
Password = colander.SchemaNode(colander.String(),
validator=colander.Length(min=2),
widget=deform.widget.CheckedPasswordWidget(),
missing=colander.null)
67 changes: 67 additions & 0 deletions pyramid_signup/views.py
Expand Up @@ -21,6 +21,8 @@
from pyramid_signup.interfaces import ISUForgotPasswordSchema
from pyramid_signup.interfaces import ISUResetPasswordForm
from pyramid_signup.interfaces import ISUResetPasswordSchema
from pyramid_signup.interfaces import ISUProfileForm
from pyramid_signup.interfaces import ISUProfileSchema
from pyramid_signup.managers import UserManager
from pyramid_signup.managers import ActivationManager
from pyramid_signup.models import User
Expand Down Expand Up @@ -324,3 +326,68 @@ def activate(self):
return HTTPFound(location=self.activate_redirect_view)

return HTTPNotFound()


class ProfileController(BaseController):
def __init__(self, request):
super(RegisterController, self).__init__(request)

schema = self.request.registry.getUtility(ISUProfileSchema)
self.schema = schema().bind(request=self.request)

form = self.request.registry.getUtility(ISUProfileForm)
self.form = form(schema)


@view_config(permission='view', route_name='profile', renderer='profile.mako')
def profile(self):
if self.request.method == 'GET':
username = self.request.user.username
first_name = self.request.user.first_name
last_name = self.request.user.last_name
email = self.request.user.email

return {
'form': self.form.render(
appstruct= dict(
Username=username,
First_Name=first_name if first_name else '',
Last_Name=last_name if last_name else '',
Email=email if email else '',
)
)
}
if self.request.method == 'POST':
try:
controls = self.request.POST.items()
captured = self.form.validate(controls)
except deform.ValidationFailure, e:
# We pre-populate username
e.cstruct['Username'] = self. request.user.username
return {'form': e.render(), 'errors': e.error.children}

user = self.request.user

user.first_name = captured.get('First_Name', '')
user.last_name = captured.get('Last_Name', '')
user.last_name = captured.get('Email', '')

password = captured.get('Password')

if password:
user.password = password

self.request.session.flash(_('Profile successfully updated.'), 'success')

self.db.add(user)

return {
'form': self.form.render(
appstruct= dict(
Username=user.username,
First_Name=user.first_name,
Last_Name=user.last_name,
Password=password,
)
)
}
4 changes: 2 additions & 2 deletions test.ini
@@ -1,8 +1,8 @@
[app:main]
paste.app_factory = demo:main
session.type = file
session.data_dir = /tmp/eventq/data/sessions/data
session.lock_dir = /tmp/eventq/data/sessions/lock
session.data_dir = /tmp/pyramid_signup/data/sessions/data
session.lock_dir = /tmp/pyramid_signup/data/sessions/lock
sqlalchemy.url = sqlite:////%(here)s/test.db
su.using_tm = false

Expand Down

0 comments on commit 0a11f35

Please sign in to comment.