Example #1
0
 def test_get_user_by_google_id(self):
     """
     test that a user can be fetched by their googleID
     """
     self.create_new_user('foobar', '*****@*****.**', '12345678')
     fetched_user= get_user_by_google_id('12345678')
     self.assertEqual(fetched_user.email, '*****@*****.**')
Example #2
0
 def test_get_user_by_google_id(self):
     """
     test that a user can be fetched by their googleID
     """
     self.create_new_user('foobar', '*****@*****.**', '12345678')
     fetched_user = get_user_by_google_id('12345678')
     self.assertEqual(fetched_user.email, '*****@*****.**')
Example #3
0
    def post(self, content_id=None):
        """
        handle the post request for the CurriculumAPI

        if no content_id then assumption is new entity, else the assumption
        is to edit an existing entity
        """
        if not users.get_current_user():
            return {'error': 'you must be logged in'}, 401
        else:
            parser = reqparse.RequestParser()
            parser.add_argument('content_type', type=str)
            parser.add_argument('teacher', type=str)
            parser.add_argument('title', type=str)
            parser.add_argument('body', type=str)
            parser.add_argument('private', type=str)
            parser.add_argument('course', type=str)
            parser.add_argument('unit', type=str)
            args = parser.parse_args()
            try:
                content = {}
                content_type = args['content_type']

                if content_type not in ['course', 'unit', 'lesson']:
                    raise TypeError('invalid content type')

                googleID = users.get_current_user().user_id()
                content['teacher'] = get_user_by_google_id(googleID).key.id()
                if content_type == 'lesson':
                    # the first line of the lesson body IS the title
                    title = args['body'].strip().splitlines()[0]
                    content['title'] = re.sub('^#+', '', title)
                else:
                    content['title'] = args['title']
                content['body'] = args['body']
                content['private'] = args['private']
                if not content_id:
                    if content_type == 'course':
                        content_id = new_course(content)
                    if content_type == 'unit':
                        content['course'] = args['course']
                        content_id = new_unit(content)
                    if content_type == 'lesson':
                        content['unit'] = args['unit']
                        content_id = new_lesson(content)

                else:
                    modify_content(content, content_id)

                new_content = get_content_by_id(content_id)
                data = content_to_dict(new_content)
            except Exception as e:
                logging.info(e)
                return {'error' : str(e)}, 500
            else:
                return data
Example #4
0
def change_card(card_token):
    """
    change the current user's credit card information
    """

    google_id = users.get_current_user().user_id()
    user = get_user_by_google_id(google_id)
    customer = stripe.Customer.retrieve(user.stripeID)
    customer.card = card_token
    customer.save()
Example #5
0
 def check_for_local_user(self, *kw, **kwargs):
     try:
         current_user = users.get_current_user()
         user = get_user_by_google_id(current_user.user_id())
         if not user:
             raise Exception
     except:
         self.abort(401)
     else:
         decorated_function(self, *kw, **kwargs)
Example #6
0
 def get(self):
     """
     render the success page
     """
     user = get_user_by_google_id(users.get_current_user().user_id())
     if user:
         return render_template(
             'success.html', 
             google_users_api = users,
             user = user,
             )
Example #7
0
    def post(self, contentID=None):
        """
        handle the post request for the CurriculumAPI

        if no contentID then assumption is new entity, else the assumption
        is to edit an existing entity

        acceptable params of the data sent in the post request include:

        'content_type' : str
        'title' : str
        'body' : str
        'private' : bool
        """
        try:
            content_type = self.request.POST.get('content_type')
            if content_type not in ['course', 'unit', 'lesson']:
                raise TypeError('invalid content type')
            googleID = users.get_current_user().user_id()

            content = {}

            content['teacher'] = get_user_by_google_id(googleID).key.id()
            if content_type == 'lesson':
                # the first line of the lesson body IS the title
                body = self.request.POST.get('body')
                title = body.splitlines()[0]
                content['title'] = re.sub('^#+', '', title)
            else:
                content['title'] = self.request.POST.get('title')
            content['body'] = self.request.POST.get('body')
            content['private'] = self.request.POST.get('private')
            if contentID is None:
                if content_type == 'course':
                    contentID = new_course(content)
                if content_type == 'unit':
                    content['course'] = self.request.POST.get('course')
                    contentID = new_unit(content)
                if content_type == 'lesson':
                    content['unit'] = self.request.POST.get('unit')
                    contentID = new_lesson(content)

            else:
                modify_content(content, contentID)

            new_content = get_content_by_id(contentID)
            data = content_to_dict(new_content)
        except Exception as e:
            data = {'error': str(e)}
        logging.info(data)
        self.write_json(data)
Example #8
0
    def post(self, contentID=None):
        """
        handle the post request for the CurriculumAPI

        if no contentID then assumption is new entity, else the assumption
        is to edit an existing entity

        acceptable params of the data sent in the post request include:

        'content_type' : str
        'title' : str
        'body' : str
        'private' : bool
        """
        try:
            content_type = self.request.POST.get('content_type')
            if content_type not in ['course', 'unit', 'lesson']:
                raise TypeError('invalid content type')
            googleID = users.get_current_user().user_id()

            content = {}

            content['teacher'] = get_user_by_google_id(googleID).key.id()
            if content_type == 'lesson':
                # the first line of the lesson body IS the title
                body = self.request.POST.get('body')
                title = body.splitlines()[0]
                content['title'] = re.sub('^#+', '', title)
            else:
                content['title'] = self.request.POST.get('title')
            content['body'] = self.request.POST.get('body')
            content['private'] = self.request.POST.get('private')
            if contentID is None:
                if content_type == 'course':
                    contentID = new_course(content)
                if content_type == 'unit':
                    content['course'] = self.request.POST.get('course')
                    contentID = new_unit(content)
                if content_type == 'lesson':
                    content['unit'] = self.request.POST.get('unit')
                    contentID = new_lesson(content)

            else:
                modify_content(content, contentID)

            new_content = get_content_by_id(contentID)
            data = content_to_dict(new_content)
        except Exception as e:
            data = {'error' : str(e)}
        logging.info(data)
        self.write_json(data)
Example #9
0
def get_customer():
    """
    return customer information for the current user
    """

    stripe.api_key = api_key

    google_user = users.get_current_user()
    googleID = google_user.user_id()
    local_user = get_user_by_google_id(googleID)

    stripeID = local_user.stripeID
    customer = stripe.Customer.retrieve(stripeID)

    return customer
Example #10
0
def get_customer():
    """
    return customer information for the current user
    """

    stripe.api_key = api_key

    google_user = users.get_current_user()
    googleID = google_user.user_id()
    local_user = get_user_by_google_id(googleID)

    stripeID = local_user.stripeID
    customer = stripe.Customer.retrieve(stripeID)
    
    return customer
Example #11
0
    def get(self, userID=None):
        """
        handle the get request userID; return none if userID does not exist
        """
        try:
            if userID is None:
                googleID = users.get_current_user().user_id()
                user = get_user_by_google_id(googleID)
            else:
                user = get_user(userID)
            if not user:
                raise Exception('that user does not exist')
            data = user_to_dict(user)

        except Exception as e:
            data = {'error' : str(e)}

        self.write_json(data)
Example #12
0
def get_user_card():
    """
    return card information for the current user
    """

    stripe.api_key = api_key

    google_user = users.get_current_user()
    googleID = google_user.user_id()
    local_user = get_user_by_google_id(googleID)

    stripeID = local_user.stripeID
    customer = stripe.Customer.retrieve(stripeID)
    # return the first card from the customer's card list

    data = {}
    data['card'] = customer.cards.data[0]
    
    return customer.cards.data[0]
Example #13
0
def get_user_card():
    """
    return card information for the current user
    """

    stripe.api_key = api_key

    google_user = users.get_current_user()
    googleID = google_user.user_id()
    local_user = get_user_by_google_id(googleID)

    stripeID = local_user.stripeID
    customer = stripe.Customer.retrieve(stripeID)
    # return the first card from the customer's card list

    data = {}
    data['card'] = customer.cards.data[0]

    return customer.cards.data[0]
Example #14
0
    def get(self, migration_type=None):
        current_user = users.get_current_user()
        if current_user.email() != '*****@*****.**':
            abort(403)
        else:
            if migration_type == 'clear_content':
                ndb.delete_multi(Curriculum.query().fetch(keys_only=True))

            elif migration_type == 'clear_teacher_courses':
                teachers = User.query()
                for teacher in teachers:
                    logging.info('clearing courses for %s' % teacher.key.id())
                    teacher.courses = []
                    teacher.put()
                    logging.info('Completed clearing courses for %s' % teacher.key.id())

            elif migration_type == 'course':
                courses = Content.query(Content.contentType == 'course')
                for course in courses:
                    if course.listed != 'done_migrating3':
                        try:
                            logging.info("Begin course migration for %s" % course.key.id())
                            app_user = course.key.parent().get()
                            teacher = get_user_by_google_id(app_user.googleID)
                            course_data = {
                                'teacher' : teacher.key.id(),
                                'title' : course.title,
                                'body' : course.body
                            }
                            new_course_id = new_course(course_data)
                            logging.info("Saved data for Curriculum ID: %s" % new_course_id)
                            units = Content.query(Content.contentType == 'unit', ancestor=course.key)
                            for unit in units:
                                logging.info("Begin unit migration for %s" % unit.key.id())
                                unit_data = {
                                    'teacher' : teacher.key.id(),
                                    'course' : new_course_id,
                                    'title' : unit.title,
                                    'body' : unit.body
                                }
                                new_unit_id = new_unit(unit_data)
                                logging.info("Saved data for Unit ID: %s" % new_unit_id)

                                lessons = Content.query(Content.contentType == 'lesson', ancestor=unit.key)
                                for lesson in lessons:
                                    logging.info("Begin lesson migration for %s" % lesson.key.id())
                                    lesson_data = {
                                        'teacher' : teacher.key.id(),
                                        'course' : new_course_id,
                                        'unit' : new_unit_id,
                                        'title' : lesson.title,
                                        'body' : lesson.body
                                    }
                                    lesson_id = new_lesson(lesson_data)
                                    logging.info("Saved data for Lesson ID: %s" % lesson_id)

                            course.listed = 'done_migrating3'
                            course.put()
                            logging.info("Finished course migration for %s" % course.key.id())
                        except Exception as e:
                            logging.info("Error migrating course %s" % course.key.id())
                            logging.info(str(e))


            return render_template(
                'migrate.html',
                status_msg = 'migration complete'
                )