<s> from __future__ import unicode_literals , absolute_import , print_function <EOL> from flask import g , abort <EOL> from flask_restaction import Resource <EOL> from werkzeug . security import generate_password_hash , check_password_hash <EOL> import datetime <EOL> from . import db , model , auth <EOL> class User ( Resource ) : <EOL> """<STR_LIT>""" <EOL> email_password = { <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT:password>" : "<STR_LIT>" <EOL> } <EOL> info = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT:name>" , <EOL> "<STR_LIT>" : "<STR_LIT:url>" , <EOL> "<STR_LIT:date>" : ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> } <EOL> schema_inputs = { <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : email_password , <EOL> "<STR_LIT>" : email_password , <EOL> "<STR_LIT>" : { "<STR_LIT:name>" : "<STR_LIT:name>" , "<STR_LIT>" : "<STR_LIT:url>" } , <EOL> "<STR_LIT>" : None <EOL> } <EOL> schema_outputs = { <EOL> "<STR_LIT>" : info , <EOL> "<STR_LIT>" : info , <EOL> "<STR_LIT>" : info , <EOL> "<STR_LIT>" : info , <EOL> "<STR_LIT>" : { "<STR_LIT:message>" : "<STR_LIT>" } <EOL> } <EOL> output_types = [ model . User ] <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> return model . User . query . get_or_404 ( g . token [ "<STR_LIT:id>" ] ) <EOL> def post ( self , email , password ) : <EOL> """<STR_LIT>""" <EOL> user = model . User . query . filter_by ( email = email ) . first ( ) <EOL> if user : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" % email ) <EOL> pwdhash = generate_password_hash ( password ) <EOL> date = datetime . datetime . utcnow ( ) <EOL> user = model . User ( email = email , pwdhash = pwdhash , date = date ) <EOL> db . session . add ( user ) <EOL> db . session . flush ( ) <EOL> user . name = "<STR_LIT>" % user . userid <EOL> db . session . commit ( ) <EOL> return user <EOL> def post_login ( self , email , password ) : <EOL> """<STR_LIT>""" <EOL> user = model . User . query . filter_by ( email = email ) . first ( ) <EOL> if not user : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if not check_password_hash ( user . pwdhash , password ) : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> header = auth . gen_auth_header ( { "<STR_LIT:id>" : user . userid } ) <EOL> user . date = datetime . datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> return user , header <EOL> def put ( self , name , photo ) : <EOL> """<STR_LIT>""" <EOL> user = model . User . query . get_or_404 ( g . token [ "<STR_LIT:id>" ] ) <EOL> user . name = name <EOL> user . photo = photo <EOL> user . date = datetime . datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> return user <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> user = model . User . query . get_or_404 ( g . token [ "<STR_LIT:id>" ] ) <EOL> db . session . delete ( user ) <EOL> return { "<STR_LIT:message>" : "<STR_LIT:OK>" } </s>
