Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Merge branch 'fix/gravatar' into 'development'
Browse files Browse the repository at this point in the history
Fix gravatar retrieval

See merge request cn-tsn/project/dbas/dbas!659
  • Loading branch information
n2o committed Feb 4, 2019
2 parents 00567fb + a7c049b commit 73853d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
4 changes: 2 additions & 2 deletions api/v2/graphql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dbas.database import DBDiscussionSession
from dbas.database.discussion_model import Statement, Issue, TextVersion, User, Language, StatementReference, \
StatementOrigins, PremiseGroup, Premise, Argument
from dbas.lib import get_public_profile_picture
from dbas.lib import get_profile_picture
from graph.lib import get_d3_data


Expand Down Expand Up @@ -129,7 +129,7 @@ class UserGraph(SQLAlchemyObjectType):
description="The profile picture of the user")

def resolve_profile_picture(self, info, **kwargs):
return get_public_profile_picture(self, **kwargs)
return get_profile_picture(self, **kwargs)

class Meta:
model = User
Expand Down
43 changes: 8 additions & 35 deletions dbas/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import datetime
from enum import Enum, auto
from html import escape, unescape
from random import randint
from typing import List, Optional
from urllib import parse
from uuid import uuid4
Expand Down Expand Up @@ -1128,51 +1129,23 @@ def __get_all_premises_of_argument(argument):
return ret_list


def get_profile_picture(user: User, size: int = 80, ignore_privacy_settings: bool = False):
def get_profile_picture(user: User, size: int = 80, ignore_privacy_settings: bool = False) -> str:
"""
Returns the url to a https://secure.gravatar.com picture, with the option wavatar and size of 80px
Returns the user's profile picture with the specified size.
:param user: User
:param size: Integer, default 80
:param ignore_privacy_settings:
:return: String
"""
additional_id = ''
if user and isinstance(user, User):
additional_id = '' if user.settings.should_show_public_nickname or ignore_privacy_settings else 'x'

return __get_gravatar(user, additional_id, size)


def get_public_profile_picture(user: User, size: int = 80):
"""
Returns the url to a https://secure.gravatar.com picture, with the option wavatar and size of 80px
If the user doesn't want an public profile, an anonymous image will be returned
:param user: User
:param size: Integer, default 80
:return: String
"""
additional_id = ''
if user.settings.should_show_public_nickname:
additional_id = 'x'
if len(str(user.oauth_provider)) > 0:
additional_id = '{}{}'.format(user.oauth_provider, user.oauth_provider_id)

return __get_gravatar(user, additional_id, size)


def __get_gravatar(user, additional_id, size):
if user:
if str(user.email) == 'None':
email = (user.nickname + additional_id).encode('utf-8')
else:
email = (user.email + additional_id).encode('utf-8')
additional_id = '' if user.settings.should_show_public_nickname or ignore_privacy_settings else 'x'
email = (user.email + additional_id).encode('utf-8')
else:
email = 'unknown'.encode('utf-8')
gravatar_url = 'https://secure.gravatar.com/avatar/{}?'.format(hashlib.md5(email.lower()).hexdigest())
gravatar_url += parse.urlencode({'d': 'wavatar', 's': str(size)})
email = str(randint(0, 999999)).encode('utf-8')

gravatar_url = 'https://secure.gravatar.com/avatar/{}?'.format(hashlib.md5(email.lower()).hexdigest())
gravatar_url += parse.urlencode({'d': 'identicon', 's': str(size)})
return gravatar_url


Expand Down
5 changes: 3 additions & 2 deletions dbas/strings/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from dbas.database.discussion_model import Statement, User, TextVersion, Issue, StatementToIssue
from dbas.handler.history import get_seen_statements_from
from dbas.helper.url import UrlManager
from dbas.lib import get_public_profile_picture, nick_of_anonymous_user, get_enabled_statement_as_query
from dbas.lib import nick_of_anonymous_user, get_enabled_statement_as_query, \
get_profile_picture
from dbas.strings.fuzzy_modes import FuzzyMode
from search import SEARCH_HOST, SEARCH_PORT
from search.requester import elastic_search, get_statements_with_similarity_to
Expand Down Expand Up @@ -351,7 +352,7 @@ def get_strings_for_public_nickname(search_value: str, nickname: str) -> list:
'distance': dist,
'text': user.public_nickname,
'html': __highlight_fuzzy_string(user.public_nickname, search_value),
'avatar': get_public_profile_picture(user)
'avatar': get_profile_picture(user)
})

return_array = __sort_array(return_array)
Expand Down

0 comments on commit 73853d0

Please sign in to comment.