예제 #1
0
파일: __init__.py 프로젝트: ilwoof/makahiki
def get_individual_standings(dorm=None,
                             round_name=None,
                             count=MAX_INDIVIDUAL_STANDINGS):
    """Retrieves standings across all floors for individual users."""

    floor_label = get_floor_label()
    title = floor_label.capitalize() + " vs. " + floor_label.capitalize(
    ) + ": "

    # Build up the query set.
    profiles = Profile.objects
    if dorm:
        profiles = profiles.filter(floor__dorm=dorm)
        title = "%s: " % dorm.name

    if round_name:
        profiles = profiles.filter(
            scoreboardentry__round_name=round_name).annotate(
                total_points=Sum("scoreboardentry__points"),
                last_awarded=Max(
                    "scoreboardentry__last_awarded_submission")).order_by(
                        "-total_points", "-last_awarded")[:count]
        title += round_name
    else:
        profiles = profiles.annotate(
            total_points=Sum("points"),
            last_awarded=Max("last_awarded_submission")).order_by(
                "-total_points", "-last_awarded")[:count]
        title += "Overall"

    # Construct the standings info dictionary.
    info = []
    for i, profile in enumerate(profiles):
        if profile.first_name and profile.last_name:
            initials = profile.first_name[0] + profile.last_name[0]
        else:
            initials = profile.name[0]

        label = ""
        if profile.floor:
            label = "%s: %s %s (%s)" % (
                profile.floor.dorm.name,
                floor_label,
                profile.floor.number,
                initials,
            )

        info.append({
            "points": profile.total_points,
            "rank": i + 1,
            "label": label,
        })

    return json.dumps({
        "title": title,
        "info": info,
    })
예제 #2
0
def get_standings_for_user(user, group="floor", round_name=None, is_me=True):
    """Generates standings for a user to be used in the standings widget.  
  Generates either floor-wide standings or standings based on all users.
  Returns a json structure for insertion into the javascript code."""

    standings_type = "individual"

    # Check for valid standings parameter.
    if group != "floor" and group != "all":
        raise StandingsException("Unknown standings type %s. Valid types are 'all' and 'floor'." % standings_type)

    user_profile = Profile.objects.get(user=user)

    if not user_profile.floor:
        # Nothing we can do.
        raise StandingsException("User has no floor for standings.")

    title = user_entry = entries = None
    if round_name:
        # Calculate standings for round.
        user_entry, created = user_profile.scoreboardentry_set.get_or_create(round_name=round_name)

        if not settings.COMPETITION_ROUNDS or not settings.COMPETITION_ROUNDS.has_key(round_name):
            # Nothing we can do again.
            raise StandingsException("Unknown round name %s" % round_name)

        if group == "floor":
            entries = ScoreboardEntry.objects.filter(profile__floor=user_profile.floor, round_name=round_name).order_by(
                "-points", "-last_awarded_submission"
            )
            title = "Your standings in %s %s, %s" % (get_floor_label(), user_profile.floor.number, round_name)
        else:
            entries = ScoreboardEntry.objects.filter(round_name=round_name).order_by(
                "-points", "-last_awarded_submission"
            )
            title = "Your standings in all dorms, %s" % round_name

    else:
        # Calculate overall standings.
        user_entry = user_profile

        if group == "floor":
            entries = Profile.objects.filter(floor=user_profile.floor).order_by("-points", "-last_awarded_submission")
            title = "Individual standings, %s" % user_profile.floor

        else:
            entries = Profile.objects.all().order_by("-points", "-last_awarded_submission")
            title = "Individual standings, Everyone"

    info, user_index = _calculate_user_standings(user_entry, entries)

    # Construct JSON return dictionary.
    return json.dumps({"title": title, "info": info, "myindex": user_index, "type": standings_type})
예제 #3
0
def competition(request):
  """Provides access to standard competition constants within a template."""
  user = request.user
  
  # We may want to retrieve theme settings for insertion into CSS.
  theme_name, theme_dict = get_theme()
  
  # Get user-specific information.
  floor_count = Floor.objects.count()
  overall_member_count = Profile.objects.count()
  floor_member_count = None
  quests = None
  notifications = None
    
  if user.is_authenticated():
    quests = get_quests(user)
    notifications = get_unread_notifications(user, limit=3)
    if user.get_profile().floor:
      floor_member_count = user.get_profile().floor.profile_set.count()
  
  # Get current round info.
  current_round = get_current_round() or "Overall"
  
  # Get Facebook info.
  try:
    facebook_app_id = settings.FACEBOOK_APP_ID
  except AttributeError:
    facebook_app_id = None
    
  return {
    "STATIC_URL": settings.STATIC_URL,
    "COMPETITION_NAME": settings.COMPETITION_NAME,
    "COMPETITION_POINT_NAME": settings.COMPETITION_POINT_NAME or "point",
    "THEME_NAME": theme_name, 
    "THEME": theme_dict,
    "FLOOR_COUNT": floor_count,
    "FLOOR_MEMBER_COUNT": floor_member_count,
    "OVERALL_MEMBER_COUNT": overall_member_count,
    "ROUNDS": get_rounds_for_header(),
    "FLOOR_LABEL": get_floor_label(),
    "CURRENT_ROUND": current_round,
    "CURRENT_ROUND_INFO": get_current_round_info(),
    "FACEBOOK_APP_ID": facebook_app_id,
    "QUESTS": quests,
    "NOTIFICATIONS": notifications,
    "IN_COMPETITION": in_competition(),
    "SPREADSHEETS": {
      "THIRTY_DAYS": settings.ENERGY_THIRTY_DAYS_URL,
      "ENERGY_GOAL": settings.ENERGY_GOAL_URL,
      "POWER": settings.POWER_GAUGE_URL,
    }
  }
예제 #4
0
def competition(request):
    """Provides access to standard competition constants within a template."""
    user = request.user

    # We may want to retrieve theme settings for insertion into CSS.
    theme_name, theme_dict = get_theme()

    # Get user-specific information.
    floor_count = Floor.objects.count()
    overall_member_count = Profile.objects.count()
    floor_member_count = None
    quests = None
    quest_help = None
    notifications = None

    # Try to load quest_help from the database.
    try:
        quest_help = HelpTopic.objects.get(slug="quests", category="widget")
    except HelpTopic.DoesNotExist:
        pass

    if user.is_authenticated():
        quests = get_quests(user)
        notifications = get_unread_notifications(user, limit=3)
        if user.get_profile().floor:
            floor_member_count = user.get_profile().floor.profile_set.count()

    # Get current round info.
    current_round = get_current_round()

    # Get Facebook info.
    try:
        facebook_app_id = settings.FACEBOOK_APP_ID
    except AttributeError:
        facebook_app_id = None

    return {
        "COMPETITION_NAME": settings.COMPETITION_NAME,
        "COMPETITION_POINT_NAME": settings.COMPETITION_POINT_NAME or "point",
        "THEME_NAME": theme_name,
        "THEME": theme_dict,
        "FLOOR_COUNT": floor_count,
        "FLOOR_MEMBER_COUNT": floor_member_count,
        "OVERALL_MEMBER_COUNT": overall_member_count,
        "ROUNDS": get_rounds_for_header(),
        "FLOOR_LABEL": get_floor_label(),
        "CURRENT_ROUND": current_round,
        "FACEBOOK_APP_ID": facebook_app_id,
        "QUESTS": quests,
        "QUEST_HELP": quest_help,
        "NOTIFICATIONS": notifications,
    }
예제 #5
0
def competition(request):
    """Provides access to standard competition constants within a template."""
    user = request.user

    # We may want to retrieve theme settings for insertion into CSS.
    theme_name, theme_dict = get_theme()

    # Get user-specific information.
    floor_count = Floor.objects.count()
    overall_member_count = Profile.objects.count()
    floor_member_count = None
    quests = None
    notifications = None

    if user.is_authenticated():
        quests = get_quests(user)
        notifications = get_unread_notifications(user, limit=3)
        if user.get_profile().floor:
            floor_member_count = user.get_profile().floor.profile_set.count()

    # Get current round info.
    current_round = get_current_round() or "Overall"

    # Get Facebook info.
    try:
        facebook_app_id = settings.FACEBOOK_APP_ID
    except AttributeError:
        facebook_app_id = None

    return {
        "STATIC_URL": settings.STATIC_URL,
        "COMPETITION_NAME": settings.COMPETITION_NAME,
        "COMPETITION_POINT_NAME": settings.COMPETITION_POINT_NAME or "point",
        "THEME_NAME": theme_name,
        "THEME": theme_dict,
        "FLOOR_COUNT": floor_count,
        "FLOOR_MEMBER_COUNT": floor_member_count,
        "OVERALL_MEMBER_COUNT": overall_member_count,
        "ROUNDS": get_rounds_for_header(),
        "FLOOR_LABEL": get_floor_label(),
        "CURRENT_ROUND": current_round,
        "CURRENT_ROUND_INFO": get_current_round_info(),
        "FACEBOOK_APP_ID": facebook_app_id,
        "QUESTS": quests,
        "NOTIFICATIONS": notifications,
        "IN_COMPETITION": in_competition(),
        "SPREADSHEETS": {
            "THIRTY_DAYS": settings.ENERGY_THIRTY_DAYS_URL,
            "ENERGY_GOAL": settings.ENERGY_GOAL_URL,
            "POWER": settings.POWER_GAUGE_URL,
        }
    }
예제 #6
0
파일: __init__.py 프로젝트: ilwoof/makahiki
def get_floor_standings(dorm=None, round_name=None):
    """Retrieves standings across all floors grouped by floor."""

    floor_label = get_floor_label()

    title = floor_label.capitalize() + " vs. " + floor_label.capitalize(
    ) + ": "

    # Build up the query set.
    floors = Floor.objects
    if dorm:
        floors = floors.filter(dorm=dorm)
        title = "%s: " % dorm.name

    if round_name:
        floors = floors.filter(
            profile__scoreboardentry__round_name=round_name).annotate(
                points=Sum("profile__scoreboardentry__points"),
                last_awarded_submission=Max(
                    "profile__scoreboardentry__last_awarded_submission")
            ).order_by("-points", "-last_awarded_submission")
        title += round_name
    else:
        floors = floors.annotate(
            points=Sum("profile__points"),
            last_awarded_submission=Max(
                "profile__last_awarded_submission")).order_by(
                    "-points", "-last_awarded_submission")
        title += "Overall"

    # Construct the standings info dictionary.
    info = []
    for i, floor in enumerate(floors):
        if dorm:
            # We don't need to put the dorm name if it's standings for a dorm.
            label = floor.number
        else:
            label = "%s: %s %s" % (floor.dorm.name, floor_label, floor.number)
        info.append({
            "points": floor.points,
            "rank": i + 1,
            "label": label,
        })

    return json.dumps({
        "title": title,
        "info": info,
    })
예제 #7
0
def get_individual_standings(dorm=None, round_name=None, count=MAX_INDIVIDUAL_STANDINGS):
    """Retrieves standings across all floors for individual users."""

    floor_label = get_floor_label()
    title = floor_label.capitalize() + " vs. " + floor_label.capitalize() + ": "

    # Build up the query set.
    profiles = Profile.objects
    if dorm:
        profiles = profiles.filter(floor__dorm=dorm)
        title = "%s: " % dorm.name

    if round_name:
        profiles = (
            profiles.filter(scoreboardentry__round_name=round_name)
            .annotate(
                total_points=Sum("scoreboardentry__points"),
                last_awarded=Max("scoreboardentry__last_awarded_submission"),
            )
            .order_by("-total_points", "-last_awarded")[:count]
        )
        title += round_name
    else:
        profiles = profiles.annotate(total_points=Sum("points"), last_awarded=Max("last_awarded_submission")).order_by(
            "-total_points", "-last_awarded"
        )[:count]
        title += "Overall"

    # Construct the standings info dictionary.
    info = []
    for i, profile in enumerate(profiles):
        if profile.first_name and profile.last_name:
            initials = profile.first_name[0] + profile.last_name[0]
        else:
            initials = profile.name[0]

        label = ""
        if profile.floor:
            label = "%s: %s %s (%s)" % (profile.floor.dorm.name, floor_label, profile.floor.number, initials)

        info.append({"points": profile.total_points, "rank": i + 1, "label": label})

    return json.dumps({"title": title, "info": info})
예제 #8
0
def get_floor_standings(dorm=None, round_name=None):
    """Retrieves standings across all floors grouped by floor."""

    floor_label = get_floor_label()

    title = floor_label.capitalize() + " vs. " + floor_label.capitalize() + ": "

    # Build up the query set.
    floors = Floor.objects
    if dorm:
        floors = floors.filter(dorm=dorm)
        title = "%s: " % dorm.name

    if round_name:
        floors = (
            floors.filter(profile__scoreboardentry__round_name=round_name)
            .annotate(
                points=Sum("profile__scoreboardentry__points"),
                last_awarded_submission=Max("profile__scoreboardentry__last_awarded_submission"),
            )
            .order_by("-points", "-last_awarded_submission")
        )
        title += round_name
    else:
        floors = floors.annotate(
            points=Sum("profile__points"), last_awarded_submission=Max("profile__last_awarded_submission")
        ).order_by("-points", "-last_awarded_submission")
        title += "Overall"

    # Construct the standings info dictionary.
    info = []
    for i, floor in enumerate(floors):
        if dorm:
            # We don't need to put the dorm name if it's standings for a dorm.
            label = floor.number
        else:
            label = "%s: %s %s" % (floor.dorm.name, floor_label, floor.number)
        info.append({"points": floor.points, "rank": i + 1, "label": label})

    return json.dumps({"title": title, "info": info})
예제 #9
0
파일: models.py 프로젝트: keokilee/makahiki
 def __unicode__(self):
   return "%s: %s %s" % (self.dorm.name, get_floor_label(), self.number)
예제 #10
0
파일: models.py 프로젝트: ilwoof/makahiki
 def __unicode__(self):
     return "%s: %s %s" % (self.dorm.name, get_floor_label(), self.number)
예제 #11
0
파일: __init__.py 프로젝트: ilwoof/makahiki
def get_standings_for_user(user, group="floor", round_name=None, is_me=True):
    """Generates standings for a user to be used in the standings widget.  
  Generates either floor-wide standings or standings based on all users.
  Returns a json structure for insertion into the javascript code."""

    standings_type = "individual"

    # Check for valid standings parameter.
    if group != "floor" and group != "all":
        raise StandingsException(
            "Unknown standings type %s. Valid types are 'all' and 'floor'." %
            standings_type)

    user_profile = Profile.objects.get(user=user)

    if not user_profile.floor:
        # Nothing we can do.
        raise StandingsException("User has no floor for standings.")

    title = user_entry = entries = None
    if round_name:
        # Calculate standings for round.
        user_entry, created = user_profile.scoreboardentry_set.get_or_create(
            round_name=round_name)

        if not settings.COMPETITION_ROUNDS or not settings.COMPETITION_ROUNDS.has_key(
                round_name):
            # Nothing we can do again.
            raise StandingsException("Unknown round name %s" % round_name)

        if group == "floor":
            entries = ScoreboardEntry.objects.filter(
                profile__floor=user_profile.floor,
                round_name=round_name,
            ).order_by("-points", "-last_awarded_submission")
            title = "Your standings in %s %s, %s" % (
                get_floor_label(), user_profile.floor.number, round_name)
        else:
            entries = ScoreboardEntry.objects.filter(
                round_name=round_name).order_by("-points",
                                                "-last_awarded_submission")
            title = "Your standings in all dorms, %s" % round_name

    else:
        # Calculate overall standings.
        user_entry = user_profile

        if group == "floor":
            entries = Profile.objects.filter(
                floor=user_profile.floor).order_by("-points",
                                                   "-last_awarded_submission")
            title = "Individual standings, %s" % user_profile.floor

        else:
            entries = Profile.objects.all().order_by(
                "-points", "-last_awarded_submission")
            title = "Individual standings, Everyone"

    info, user_index = _calculate_user_standings(user_entry, entries)

    # Construct JSON return dictionary.
    return json.dumps({
        "title": title,
        "info": info,
        "myindex": user_index,
        "type": standings_type,
    })