Пример #1
0
def avatar(user, size=settings.AVATAR_DEFAULT_SIZE, **kwargs):
    if has_avatar(user):
        # Use assigned avatar
        alt = six.text_type(user)
        url = avatar_url(user, size)
        kwargs.update({'alt': alt})
        context = {
            'user': user,
            'url': url,
            'size': size,
            'kwargs': kwargs,
        }
        return render_to_string('avatar/avatar_tag.html', context)
    else:
        # Dynamically generate an avatar based on the user's initials
        letter = '?'
        if user.first_name:
            letter = user.first_name[0].upper()
            if user.last_name:
                letter += user.last_name[0].upper()
        elif user.username:
            letter = user.username[0].upper()

        colour_ix = int(hashlib.md5(str(user.id).encode()).hexdigest(), 16) % NUM_COLOURS
        context = {'colour_ix': colour_ix, 'letter': letter, 'size': size, 'font_size': (size / 2)}
        return render_to_string('accounts/dynamic_avatar.svg', context)
Пример #2
0
def recentCompetitionUserScoreboard(request):
    today = timezone.now()
    allPastRounds = Rounds.objects.filter(
        startdatetime__lte=today).order_by('startdatetime').reverse()
    if allPastRounds.count() > 0:
        mostRecentRound = allPastRounds[0]

        # now editing
        thisRoundSubmissions = RoundSubmissions.objects.filter(
            roundquestion__thisround__id=mostRecentRound.id,
            passed=True).order_by('-score', 'submitted_at')
        thisRoundSubmissions = getDistinctModelFields(
            thisRoundSubmissions, ['user', 'roundquestion'])
        users = {}
        for submission in thisRoundSubmissions:
            thisUser = submission.user
            if RoundUsers.objects.filter(
                    user=thisUser, thisround=mostRecentRound).exists() == True:
                if thisUser.id not in users.keys():
                    if has_avatar(thisUser) == True:
                        userAvatarUrl = get_primary_avatar(
                            thisUser.username).get_absolute_url()
                    elif thisUser.profile.profileImgUrl != '':
                        userAvatarUrl = thisUser.profile.profileImgUrl
                    else:
                        userAvatarUrl = ''
                    users[thisUser.id] = {
                        'userid': thisUser.id,
                        'profileImgUrl': userAvatarUrl,
                        'first_name': thisUser.first_name,
                        'last_name': thisUser.last_name,
                        'username': thisUser.username,
                    }

        competitionUsers = list(users.values())

        # editing ends

        return JsonResponse({'users': competitionUsers})
    else:
        competitionUsers = None
        return JsonResponse({'error': 'No competitions'})
Пример #3
0
    def test_has_avatar_True(self):
        upload_helper(self, "test.png")

        self.assertTrue(avatar_tags.has_avatar(self.user))
Пример #4
0
 def test_has_avatar_False_if_not_user_model(self):
     self.assertFalse(avatar_tags.has_avatar("Look, I'm a string"))
Пример #5
0
 def test_has_avatar_False_if_no_avatar(self):
     self.assertFalse(avatar_tags.has_avatar(self.user))
Пример #6
0
def overallUserScoreboard(request):
    allsubmissions = Submissions.objects.all().order_by(
        '-score', 'submitted_at')
    allsubmissions = getDistinctModelFields(allsubmissions,
                                            ['user', 'question'])
    users = []
    for submission in allsubmissions:
        score = 0
        isSuccess = submission.passed
        gotSubscore = submission.gotSubscore
        if isSuccess:
            score = submission.question.points
            if gotSubscore:
                score = score + submission.question.subscore

            username = submission.user.username
            if not any(d.get('username', None) == username for d in users):

                # user avatar
                thisUserObj = userObjFromId(submission.user.id)
                if has_avatar(thisUserObj) == True:
                    userAvatarUrl = get_primary_avatar(
                        thisUserObj).get_absolute_url()
                elif thisUserObj.profile.profileImgUrl != '':
                    userAvatarUrl = thisUserObj.profile.profileImgUrl
                else:
                    userAvatarUrl = ''

                users.append({
                    'username': username,
                    'score': score,
                    'profileImgUrl': userAvatarUrl,
                    'first_name': submission.user.first_name,
                    'last_name': submission.user.last_name,
                })
            else:
                for index, item in enumerate(users):
                    if item['username'] == username:
                        score = score + item['score']

                        # user avatar
                        thisUserObj = userObjFromId(submission.user.id)
                        if has_avatar(thisUserObj) == True:
                            userAvatarUrl = get_primary_avatar(
                                thisUserObj).get_absolute_url()
                        elif thisUserObj.profile.profileImgUrl != '':
                            userAvatarUrl = thisUserObj.profile.profileImgUrl
                        else:
                            userAvatarUrl = ''

                        users[index] = {
                            'username': username,
                            'score': score,
                            'profileImgUrl': userAvatarUrl,
                            'first_name': submission.user.first_name,
                            'last_name': submission.user.last_name,
                        }

    if len(users) > 0:
        users = sorted(users, key=itemgetter('score'), reverse=True)
        return JsonResponse({'users': users})
    else:
        return JsonResponse({'error': 'No submissions'})
Пример #7
0
    def test_has_avatar_True(self):
        upload_helper(self, "test.png")

        self.assertTrue(avatar_tags.has_avatar(self.user))
Пример #8
0
 def test_has_avatar_False_if_not_user_model(self):
     self.assertFalse(avatar_tags.has_avatar("Look, I'm a string"))
Пример #9
0
 def test_has_avatar_False_if_no_avatar(self):
     self.assertFalse(avatar_tags.has_avatar(self.user))