Advertisement
Guest User

Conference api

a guest
Jan 5th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. conference.py -- Udacity conference server-side Python App Engine API;
  5.    uses Google Cloud Endpoints
  6.  
  7. $Id: conference.py,v 1.25 2014/05/24 23:42:19 wesc Exp wesc $
  8.  
  9. created by wesc on 2014 apr 21
  10.  
  11. """
  12.  
  13. __author__ = 'wesc+api@google.com (Wesley Chun)'
  14.  
  15.  
  16. from datetime import datetime
  17.  
  18. import endpoints
  19. from protorpc import messages
  20. from protorpc import message_types
  21. from protorpc import remote
  22.  
  23. from google.appengine.ext import ndb
  24.  
  25. from models import Profile
  26. from models import ProfileMiniForm
  27. from models import ProfileForm
  28. from models import TeeShirtSize
  29.  
  30. from settings import WEB_CLIENT_ID
  31. import utils
  32.  
  33. EMAIL_SCOPE = endpoints.EMAIL_SCOPE
  34. API_EXPLORER_CLIENT_ID = endpoints.API_EXPLORER_CLIENT_ID
  35.  
  36. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  37.  
  38.  
  39. @endpoints.api( name='conference',
  40.                 version='v1',
  41.                 allowed_client_ids=[WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID],
  42.                 scopes=[EMAIL_SCOPE], hostname = 'cc.mdsarowar.me')
  43. class ConferenceApi(remote.Service):
  44.     """Conference API v0.1"""
  45.  
  46. # - - - Profile objects - - - - - - - - - - - - - - - - - - -
  47.  
  48.     def _copyProfileToForm(self, prof):
  49.         """Copy relevant fields from Profile to ProfileForm."""
  50.         # copy relevant fields from Profile to ProfileForm
  51.         pf = ProfileForm()
  52.         for field in pf.all_fields():
  53.             if hasattr(prof, field.name):
  54.                 # convert t-shirt string to Enum; just copy others
  55.                 if field.name == 'teeShirtSize':
  56.                     setattr(pf, field.name, getattr(TeeShirtSize, getattr(prof, field.name)))
  57.                 else:
  58.                     setattr(pf, field.name, getattr(prof, field.name))
  59.         pf.check_initialized()
  60.         return pf
  61.  
  62.  
  63.     def _getProfileFromUser(self):
  64.         """Return user Profile from datastore, creating new one if non-existent."""
  65.         user = endpoints.get_current_user()
  66.         if not user:
  67.             raise endpoints.UnauthorizedException('Authorization required')
  68.  
  69.         # TODO 1
  70.         # step 1. copy utils.py from additions folder to this folder
  71.         #         and import getUserId from it
  72.         # step 2. get user id by calling getUserId(user)
  73.         # step 3. create a new key of kind Profile from the id
  74.         user_id=utils.getUserId(user)
  75.         p_key=ndb.Key(Profile,user_id);
  76.  
  77.         # TODO 3
  78.         # get the entity from datastore by using get() on the key
  79.         profile = p_key.get()
  80.         if not profile:
  81.             profile = Profile(
  82.                 key = p_key, # TODO 1 step 4. replace with the key from step 3
  83.                 displayName = user.nickname(),
  84.                 mainEmail= user.email(),
  85.                 teeShirtSize = str(TeeShirtSize.NOT_SPECIFIED),
  86.             )
  87.             # TODO 2
  88.             # save the profile to datastore
  89.             profile.put()
  90.  
  91.         return profile      # return Profile
  92.  
  93.  
  94.     def _doProfile(self, save_request=None):
  95.         """Get user Profile and return to user, possibly updating it first."""
  96.         # get user Profile
  97.         prof = self._getProfileFromUser()
  98.  
  99.         # if saveProfile(), process user-modifyable fields
  100.         if save_request:
  101.             for field in ('displayName', 'teeShirtSize'):
  102.                 if hasattr(save_request, field):
  103.                     val = getattr(save_request, field)
  104.                     if val:
  105.                         setattr(prof, field, str(val))
  106.             # TODO 4
  107.             # put the modified profile to datastore
  108.             prof.put()
  109.  
  110.         # return ProfileForm
  111.         return self._copyProfileToForm(prof)
  112.  
  113.  
  114.     @endpoints.method(message_types.VoidMessage, ProfileForm,
  115.             path='profile', http_method='GET', name='getProfile')
  116.     def getProfile(self, request):
  117.         """Return user profile."""
  118.         return self._doProfile()
  119.  
  120.  
  121.     @endpoints.method(ProfileMiniForm, ProfileForm,
  122.             path='profile', http_method='POST', name='saveProfile')
  123.     def saveProfile(self, request):
  124.         """Update & return user profile."""
  125.         return self._doProfile(request)
  126.  
  127.  
  128. # registers API
  129. api = endpoints.api_server([ConferenceApi])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement