def timeline_insert(self, card): """Insert a card for the current user.""" if card.id is not None: raise endpoints.BadRequestException( "ID is not allowed in request body.") if card.menuItems is not None: for menuItem in card.menuItems: if menuItem.action == MenuAction.CUSTOM: if menuItem.id is None: raise endpoints.BadRequestException( "For custom actions id needs to be provided.") if menuItem.values is None or len(menuItem.values) == 0: raise endpoints.BadRequestException( "For custom actions at least one value needs to be provided." ) for value in menuItem.values: if value.displayName is None or value.iconUrl is None: raise endpoints.BadRequestException( "Each value needs to contain displayName and iconUrl." ) card.put() channel.send_message(card.user.email(), json.dumps({"id": card.id})) return card
def timeline_internal_insert(self, card): """Insert a card for the current user. Internal method for the Emulator to work. Not part of the actual Mirror API and shouldn't be used. """ if card.id is not None: raise endpoints.BadRequestException( "ID is not allowed in request body.") if card.menuItems is not None: for menuItem in card.menuItems: if menuItem.action == MenuAction.CUSTOM: if menuItem.id is None: raise endpoints.BadRequestException( "For custom actions id needs to be provided.") if menuItem.values is None or len(menuItem.values) == 0: raise endpoints.BadRequestException( "For custom actions at least one value needs to be provided." ) for value in menuItem.values: if value.displayName is None or value.iconUrl is None: raise endpoints.BadRequestException( "Each value needs to contain displayName and iconUrl." ) if card.htmlPages is not None and len( card.htmlPages) > 0 and card.bundleId is not None: raise endpoints.BadRequestException( "Can't mix HTML and Card bundle.") card.isDeleted = False card.put() return card
def is_valid_name(username): """ Checks if the given username is valid :param username: The username provided by the client :return: True if the name is valid false otherwise """ if username and NAME_RE.match(username): if tables.StudentModel.by_name(username): raise endpoints.BadRequestException('Username exists.' 'Choose another.') else: endpoints.BadRequestException('Username is not valid.')
def subscription_insert(self, subscription): """Insert a new subscription for the current user.""" if subscription.id is not None: raise endpoints.BadRequestException( "ID is not allowed in request body.") if subscription.operation is None or len(subscription.operation) == 0: raise endpoints.BadRequestException( "At least one operation needs to be provided.") subscription.put() return subscription
def is_valid_email(email): """ Checks if the email requested by user is a valid email :param email Email passed by the user :raises BadRequestException when the email already exists in the db or the email is not valid """ if email and EMAIL_RE.match(email): if tables.StudentModel.by_email(email): raise endpoints.BadRequestException('There is an account ' 'associated with that email.') else: raise endpoints.BadRequestException('Email is not valid')
def contacts_insert(self, contact): """Insert a new Contact for the current user.""" if contact.id is None: raise endpoints.BadRequestException("ID needs to be provided.") if contact.displayName is None: raise endpoints.BadRequestException("displayName needs to be provided.") if contact.imageUrls is None or len(contact.imageUrls) == 0: raise endpoints.BadRequestException("At least one imageUrl needs to be provided.") if contact.from_datastore: return contact contact.put() return contact
def locations_insert(self, location): """Insert a new location for the current user. Not part of the actual mirror API but used by the emulator. """ if location.id is not None: raise endpoints.BadRequestException("ID is not allowed in request body.") location.put() # Notify location subscriptions data = {} data["collection"] = "locations" data["itemId"] = "latest" operation = Operation.UPDATE data["operation"] = operation.name header = {"Content-type": "application/json"} query = Subscription.query().filter(Subscription.user == endpoints.get_current_user()) query = query.filter(Subscription.collection == "locations") query = query.filter(Subscription.operation == operation) for subscription in query.fetch(): data["userToken"] = subscription.userToken data["verifyToken"] = subscription.verifyToken req = urllib2.Request(subscription.callbackUrl, json.dumps(data), header) try: urllib2.urlopen(req) except: logging.error(sys.exc_info()[0]) return location
def sign_up(self, request): """ This is the method that adds the user to the database, provided the information submitted by clients are valid :param request: student object requested by the user to add to the database :return: The user object that has been registered """ first_name = request.first_name last_name = request.last_name user_name = request.user_name is_valid_name(user_name) email = request.email password = request.password school = request.school is_valid_email(email) if not is_valid_password(password): raise endpoints.BadRequestException("Passowrd is not valid") tables.StudentModel(first_name=first_name, last_name=last_name, user_name=user_name, email=email, is_verified=False, school=school, password=utility.hash_str(password)).put() schools = school.split(",") return Student(first_name=first_name, last_name=last_name, user_name=user_name, email='', password='', school=school)
def subscription_insert(self, subscription): """Insert a new subscription for the current user.""" if subscription.id is not None: raise endpoints.BadRequestException("ID is not allowed in request body.") if subscription.operation is None or len(subscription.operation) == 0: subscription.operation = [Operation.UPDATE, Operation.INSERT, Operation.DELETE] subscription.put() return subscription
def upload(self, request): ValidateUserIsAuthorized() change_key = models.Change.get_key(request.change_id) change = change_key.get() if change is None: raise endpoints.BadRequestException( 'Invalid change ID.') if request.url_path not in change.upload_paths: raise endpoints.BadRequestException( 'Unexpected upload for path %s.' % request.url_path) if len(request.data) > 900 * 1024: raise endpoints.BadRequestException( 'Uploaded data cannot exceed 900 kilobytes.') publish.create_content( request.change_id, request.url_path, request.content_type, request.data) return GenericResponse()
def create(self, request): try: pledge = pledges.Pledge.create(request.pledge.email, request.pledge.first_name, request.pledge.last_name) except pledges.PledgeExistsError as e: raise endpoints.BadRequestException(str(e)) message = messages.CreatePledgeResponse() message.pledge = pledge.to_message() message.pledge_count = pledges.Pledge.count() + 1 return message
def board_get_move(self, request): """Exposes an API endpoint to simulate a computer move in tictactoe. Args: request: An instance of BoardMessage parsed from the API request. Returns: An instance of BoardMessage with a single 'O' added to the board passed in. """ board_state = request.state if not (len(board_state) == 9 and set(board_state) <= set('OX-')): raise endpoints.BadRequestException('Invalid board.') return BoardMessage(state=self.add_move_to_board(board_state))
def locations_insert(self, location): """Insert a new location for the current user. Not part of the actual mirror API but used by the emulator. """ if location.id is not None: raise endpoints.BadRequestException( "ID is not allowed in request body.") location.put() # TODO: notify location subscriptions return location
def add_course(self, request): """ This is the method that provides the api to add the course to the database :param request: This is the request object that contains query information :return: Returns the course that has been added to the db """ sch = tables.SchoolModel.get_by_id(int(request.school)) if sch: tables.CourseModel(school=sch, course_id=request.course_id, title=request.title).put() return Course(course_id=request.course_id, title=request.title, school=request.school) else: raise endpoints.BadRequestException("Invalid college Id")
def timeline_update(self, card): """Update card with ID for the current user""" if not card.from_datastore or card.user != endpoints.get_current_user(): raise endpoints.NotFoundException("Card not found.") if card.isDeleted: raise endpoints.NotFoundException("Card has been deleted") if card.htmlPages is not None and len(card.htmlPages) > 0 and card.bundleId is not None: raise endpoints.BadRequestException("Can't mix HTML and Card bundle.") card.put() channel.send_message(card.user.email(), json.dumps({"id": card.id})) return card