Created
February 24, 2012 00:49
-
-
Save jvanasco/1896363 to your computer and use it in GitHub Desktop.
making an api in pyramid, example a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
log = logging.getLogger(__name__) | |
from pyramid.httpexceptions import HTTPFound | |
from pyramid.renderers import render_to_response | |
from pyramid.view import view_config | |
# all that's necessary to know is that _base sets self.request | |
from ..lib import handlers as _base | |
import pyramid_formencode_classic as formhandling | |
import formencode | |
class _Schema_Base(formencode.Schema): | |
allow_extra_fields = True | |
filter_extra_fields = False | |
class FormLogin(_Schema_Base): | |
email_address = formencode.validators.Email(not_empty=True) | |
password = formencode.validators.UnicodeString(not_empty=True) | |
remember_me = formencode.validators.Bool() | |
def new_rval(): | |
return { 'status':'error' } | |
class ApiInternal1(_base.HandlerApi): | |
@view_config(renderer='json', route_name="api-internal::is_logged_in") | |
def is_logged_in(self): | |
rval= new_rval() | |
try: | |
if 'useraccount_id' in self.request.session and self.request.session['useraccount_id']: | |
rval['status']= 'success' | |
rval['is_logged_in']= True | |
else: | |
rval['is_logged_in']= False | |
except : | |
rval['error']= 'There was an error' | |
return rval | |
@view_config(renderer='json', route_name="api-internal::login") | |
def login(self): | |
rval= new_rval() | |
try: | |
( result , formStash ) = formhandling.form_validate( self.request , schema=FormLogin ) | |
if not result : | |
raise formhandling.FormInvalid('Invalid Submission') | |
# find the user | |
useraccount= load_useraccount( formStash.results['email'] , formStash.results['password'] ) | |
if not useraccount: | |
formStash.setError( section='Error_Main' , message='No User' , raise_form_invalid=True ) | |
h.do_login( self.request, useraccount ) | |
rval['status']= 'success' | |
rval['is_logged_in']= True | |
rval['useraccount_id']= useraccount.id | |
except formhandling.FormInvalid , e: | |
rval['error']= e | |
return rval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment