def stats_handler(data):
    pos, line = data

    if not line:
        raise StopIteration
    try:
        stats = json.loads(line)
    except (ValueError, TypeError):
        logging.error("Failed to process rosh review stats: %s", line)
        raise StopIteration

    student_email = stats.get('email', '').lower()
    student_id = User.email_to_student_id(student_email)
    if student_id is None:
        logging.error(
            "Failed to process rosh review stats: invalid student email (%s)",
            line)
        raise StopIteration

    student = Student.get_by_id(student_id)
    if student is None:
        logging.error(
            "Failed to process rosh review stats: student not found (%s)",
            line)
        raise StopIteration

    user_stats = RoshReviewUserStats.new_stats(student, stats, commit=False)
    user_topic_stats = user_stats.update_topic_stats(commit=False)

    yield op.db.Put(user_stats)
    for ts in user_topic_stats:
        yield op.db.Put(ts)

    yield op.counters.Increment('User stats', 1)
    yield op.counters.Increment('User topic stats', len(user_topic_stats))
Exemple #2
0
 def from_data(cls, data, commit=True):
     student_id = User.email_to_student_id(data['email'])
     topic_id = data['category_id']
     date = cls.cal_day_to_date(data['cal_day_dt'])
     return cls.new_activity(
         student_id, topic_id, date, data, commit=commit
     )
    def post(self):
        self.set_session_store()
        self.response.headers['Content-Type'] = "application/json"

        sender_id = self.current_user_id()
        sender = User.get_by_id(sender_id) if sender_id else None
        if not sender:
            self.render_json(
                {"error": "you need to be logged in to upload files."},
                401
            )
            return

        if (
            not sender.is_staff
            and not sender.is_admin
            and not sender.is_domain_admin
        ):
            self.render_json(
                {"error": "Only admin and staff can upload files."},
                403
            )
            return

        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        name = self.request.POST.get('name', blob_info.filename)

        exam = models.AssessmentExam.new_exam(name, blob_info.key())
        self.render_json(exam.summary())
    def get(self, keyId):
        self.set_session_store()

        keyId = str(urllib.unquote(keyId))

        viewer_id = self.current_user_id()
        viewer = User.get_by_id(viewer_id) if viewer_id else None
        if not viewer:
            self.error(401)
            return

        doc = models.Document.get_by_id(keyId)
        if (
            not viewer.is_staff
            and not viewer.is_admin
            and not viewer.is_domain_admin
            and viewer.student_id != doc.dest_ref.id()
        ):
            self.error(403)

        blob_info = blobstore.BlobInfo.get(keyId)
        if blob_info.filename.endswith('.pdf'):
            self.send_blob(blob_info, content_type='application/pdf')
        else:
            self.send_blob(blob_info)
Exemple #5
0
 def from_data(cls, data, commit=True):
     student_id = User.email_to_student_id(data['email'])
     topic_id = data['category_id']
     date = cls.cal_day_to_date(data['cal_day_dt'])
     return cls.new_activity(student_id,
                             topic_id,
                             date,
                             data,
                             commit=commit)
    def post(self):
        self.set_session_store()
        self.response.headers['Content-Type'] = "application/json"

        sender_id = self.current_user_id()
        sender = User.get_by_id(sender_id) if sender_id else None
        if not sender:
            self.render_json(
                {"error": "you need to be logged in to upload files."},
                401
            )
            return

        if (
            not sender.is_staff
            and not sender.is_admin
            and not sender.is_domain_admin
        ):
            self.render_json(
                {"error": "Only admin and staff can upload files."},
                403
            )
            return

        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        dest_student_id = self.request.POST.get('destId')
        name = self.request.POST.get('name', blob_info.filename)
        doc_type = self.request.POST.get('docType')

        if not dest_student_id:
            self.render_json(
                {"error": 'No recipent was given.'},
                400
            )
            return

        if not doc_type:
            self.render_json(
                {"error": 'A document should have a type.'},
                400
            )
            return

        try:
            new_file = models.Document.new_file(
                dest_student_id, blob_info, doc_type, sender, name
            )
        except (ValueError, ValidationError,), e:
            self.render_json(
                {"error": "Failed to safe new file (%s)." % str(e)},
                400
            )
            return
Exemple #7
0
    def new_user(self,
                 name="Alice Smith",
                 is_staff=False,
                 is_admin=False,
                 is_domain_admin=False,
                 student_id=None):
        given_name, family_name = name.split(' ')
        domain = "example.com"

        if student_id is not None:
            domain = "example.com"
            email = "*****@*****.**" % student_id
        else:
            email = "%s_%s@%s" % (
                given_name,
                family_name,
                domain,
            )

        data = {
            "displayName": name,
            "image": {
                "url": "http://example.com/image.jpg?sw=50"
            },
            "primaryEmail": email,
            "name": {
                "givenName": given_name,
                "familyName": family_name
            },
            "isDomainAdmin": is_domain_admin,
            "isDelegatedAdmin": False,
            "orgUnitPath": '/',
            "domain": domain,
        }

        user = User(id=str(self._id_count),
                    is_staff=is_staff,
                    is_admin=is_admin,
                    data=data).put().get(use_cache=False)
        self._id_count += 1
        return user
    def post(self):
        self.set_session_store()
        self.response.headers['Content-Type'] = "application/json"

        sender_id = self.current_user_id()
        sender = User.get_by_id(sender_id) if sender_id else None
        if not sender:
            self.render_json(
                {"error": "you need to be logged in to upload files."}, 401)
            return

        if (not sender.is_staff and not sender.is_admin
                and not sender.is_domain_admin):
            self.render_json(
                {"error": "Only admin and staff can upload files."}, 403)
            return

        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        dest_student_id = self.request.POST.get('destId')
        name = self.request.POST.get('name', blob_info.filename)
        doc_type = self.request.POST.get('docType')

        if not dest_student_id:
            self.render_json({"error": 'No recipent was given.'}, 400)
            return

        if not doc_type:
            self.render_json({"error": 'A document should have a type.'}, 400)
            return

        try:
            new_file = models.Document.new_file(dest_student_id, blob_info,
                                                doc_type, sender, name)
        except (
                ValueError,
                ValidationError,
        ), e:
            self.render_json(
                {"error": "Failed to safe new file (%s)." % str(e)}, 400)
            return
    def get(self, keyId):
        self.set_session_store()

        keyId = str(urllib.unquote(keyId))

        viewer_id = self.current_user_id()
        viewer = User.get_by_id(viewer_id) if viewer_id else None
        if not viewer:
            self.error(401)
            return

        doc = models.Document.get_by_id(keyId)
        if (not viewer.is_staff and not viewer.is_admin
                and not viewer.is_domain_admin
                and viewer.student_id != doc.dest_ref.id()):
            self.error(403)

        blob_info = blobstore.BlobInfo.get(keyId)
        if blob_info.filename.endswith('.pdf'):
            self.send_blob(blob_info, content_type='application/pdf')
        else:
            self.send_blob(blob_info)
def stats_handler(data):
    pos, line = data

    if not line:
        raise StopIteration
    try:
        stats = json.loads(line)
    except (ValueError, TypeError):
        logging.error("Failed to process rosh review stats: %s", line)
        raise StopIteration

    student_email = stats.get('email', '').lower()
    student_id = User.email_to_student_id(student_email)
    if student_id is None:
        logging.error(
            "Failed to process rosh review stats: invalid student email (%s)",
            line
        )
        raise StopIteration

    student = Student.get_by_id(student_id)
    if student is None:
        logging.error(
            "Failed to process rosh review stats: student not found (%s)",
            line
        )
        raise StopIteration

    user_stats = RoshReviewUserStats.new_stats(
        student, stats, commit=False
    )
    user_topic_stats = user_stats.update_topic_stats(commit=False)

    yield op.db.Put(user_stats)
    for ts in user_topic_stats:
        yield op.db.Put(ts)

    yield op.counters.Increment('User stats', 1)
    yield op.counters.Increment('User topic stats', len(user_topic_stats))
    def post(self):
        self.set_session_store()
        self.response.headers['Content-Type'] = "application/json"

        sender_id = self.current_user_id()
        sender = User.get_by_id(sender_id) if sender_id else None
        if not sender:
            self.render_json(
                {"error": "you need to be logged in to upload files."}, 401)
            return

        if (not sender.is_staff and not sender.is_admin
                and not sender.is_domain_admin):
            self.render_json(
                {"error": "Only admin and staff can upload files."}, 403)
            return

        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        name = self.request.POST.get('name', blob_info.filename)

        exam = models.AssessmentExam.new_exam(name, blob_info.key())
        self.render_json(exam.summary())