Exemplo n.º 1
0
    def get(self, email_or_username=None, subpath=None):
        """Render a student profile.

        Keyword arguments:
        email_or_username -- matches the first grouping in /profile/(.+?)/(.*)
        subpath -- matches the second grouping, and is ignored server-side,
        but is used to route client-side

        """
        current_user_data = UserData.current() or UserData.pre_phantom()

        if current_user_data.is_pre_phantom and email_or_username is None:
            # Pre-phantom users don't have any profiles - just redirect them
            # to the homepage if they try to view their own.
            self.redirect(util.create_login_url(self.request.uri))
            return

        if not email_or_username:
            user_data = current_user_data
        elif email_or_username == 'nouser' and current_user_data.is_phantom:
            user_data = current_user_data
        else:
            user_data = UserData.get_from_url_segment(email_or_username)
            if (models.UniqueUsername.is_valid_username(email_or_username)
                    and user_data
                    and user_data.username
                    and user_data.username != email_or_username):
                # The path segment is a username and resolved to the user,
                # but is not actually their canonical name. Redirect to the
                # canonical version.
                if subpath:
                    self.redirect("/profile/%s/%s" % (user_data.username,
                                                      subpath))
                else:
                    self.redirect("/profile/%s" % user_data.username)
                return


        profile = UserProfile.from_user(user_data, current_user_data)

        if profile is None:
            self.render_jinja2_template('noprofile.html', {})
            return

        is_self = user_data.user_id == current_user_data.user_id
        show_intro = False

        if is_self:
            bingo([
                'suggested_activity_visit_profile',
            ])

            promo_record = models.PromoRecord.get_for_values(
                    "New Profile Promo", user_data.user_id)

            if promo_record is None:
                # The user has never seen the new profile page! Show a tour.
                if subpath:
                    # But if they're not on the root profile page, force them.
                    self.redirect("/profile")
                    return

                show_intro = True
                models.PromoRecord.record_promo("New Profile Promo",
                                                user_data.user_id,
                                                skip_check=True)

        has_full_access = is_self or user_data.is_visible_to(current_user_data)
        tz_offset = self.request_int("tz_offset", default=0)

        template_values = {
            'show_intro': show_intro,
            'profile': profile,
            'tz_offset': tz_offset,
            'count_videos': models.Setting.count_videos(),
            'count_exercises': models.Exercise.get_count(),
            'user_data_student': user_data if has_full_access else None,
            'profile_root': user_data.profile_root,
            "view": self.request_string("view", default=""),
        }
        self.render_jinja2_template('viewprofile.html', template_values)
Exemplo n.º 2
0
    def get(self, email_or_username=None, subpath=None):
        """Render a student profile.

        Keyword arguments:
        email_or_username -- matches the first grouping in /profile/(.+?)/(.*)
        subpath -- matches the second grouping, and is ignored server-side,
        but is used to route client-side

        """
        current_user_data = UserData.current() or UserData.pre_phantom()

        if current_user_data.is_pre_phantom and email_or_username is None:
            # Pre-phantom users don't have any profiles - just redirect them
            # to the homepage if they try to view their own.
            self.redirect(util.create_login_url(self.request.uri))
            return

        if not email_or_username:
            user_data = current_user_data
        elif email_or_username == 'nouser' and current_user_data.is_phantom:
            user_data = current_user_data
        else:
            user_data = UserData.get_from_url_segment(email_or_username)
            if (models.UniqueUsername.is_valid_username(email_or_username)
                    and user_data and user_data.username
                    and user_data.username != email_or_username):
                # The path segment is a username and resolved to the user,
                # but is not actually their canonical name. Redirect to the
                # canonical version.
                if subpath:
                    self.redirect("/profile/%s/%s" %
                                  (user_data.username, subpath))
                else:
                    self.redirect("/profile/%s" % user_data.username)
                return

        profile = UserProfile.from_user(user_data, current_user_data)

        if profile is None:
            self.render_jinja2_template('noprofile.html', {})
            return

        is_self = user_data.user_id == current_user_data.user_id
        show_intro = False

        if is_self:
            bingo([
                'suggested_activity_visit_profile',
            ])

            promo_record = models.PromoRecord.get_for_values(
                "New Profile Promo", user_data.user_id)

            if promo_record is None:
                # The user has never seen the new profile page! Show a tour.
                if subpath:
                    # But if they're not on the root profile page, force them.
                    self.redirect("/profile")
                    return

                show_intro = True
                models.PromoRecord.record_promo("New Profile Promo",
                                                user_data.user_id,
                                                skip_check=True)

        has_full_access = is_self or user_data.is_visible_to(current_user_data)
        tz_offset = self.request_int("tz_offset", default=0)

        template_values = {
            'show_intro': show_intro,
            'profile': profile,
            'tz_offset': tz_offset,
            'count_videos': models.Setting.count_videos(),
            'count_exercises': models.Exercise.get_count(),
            'user_data_student': user_data if has_full_access else None,
            'profile_root': user_data.profile_root,
            "view": self.request_string("view", default=""),
        }
        self.render_jinja2_template('viewprofile.html', template_values)