Ejemplo n.º 1
0
    def test_avatar(self):
        profile = mommy.make(Profile, user=self.user)

        self.assertTrue(has_gravatar(self.email))
        self.assertFalse(has_gravatar(self.no_email))
        self.assertFalse(profile.avatar)

        profile.avatar = profile.user.email

        self.assertTrue(profile.avatar)
Ejemplo n.º 2
0
    def test_avatar(self):
        profile = mommy.make(Profile, user=self.user)

        self.assertTrue(has_gravatar(self.email))
        self.assertFalse(has_gravatar(self.no_email))
        self.assertFalse(profile.avatar)

        profile.avatar = profile.user.email

        self.assertTrue(profile.avatar)
Ejemplo n.º 3
0
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')

        if not kwargs.get('username'):
            raise ValueError('Users must have a valid username.')

        if not kwargs.get('first_name'):
            raise ValueError('Users must have a valid first name.')

        if not kwargs.get('last_name'):
            raise ValueError('Users must have a valid last name.')

        first_name = kwargs.get('first_name')
        last_name = kwargs.get('last_name')
        if has_gravatar(email):
            gravatar_url = get_gravatar_url(email, size=25)
            has_gravatar_1 = True
        else:
            gravatar_url = first_name[:1].upper() + last_name[:1].upper()
            has_gravatar_1 = False

        account = self.model(
            email=self.normalize_email(email), username=kwargs.get('username'), first_name=kwargs.get('first_name'),
            last_name=kwargs.get('last_name'), gravatar_url=gravatar_url, has_gravatar=has_gravatar_1
        )

        account.set_password(password)
        account.save()

        return account
Ejemplo n.º 4
0
    def get_context_data(self, **kwargs):
        context = super(BaseUsersMultiFormView,
                        self).get_context_data(**kwargs)

        user_id = self.kwargs['user_id']
        context['user_id'] = user_id

        if getattr(self.object, 'use_gravatar', None):
            context['using_gravatar'] = True

        if has_gravatar(getattr(self.object, 'name', None)):
            gravatar_image = get_gravatar_url(
                getattr(self.object, 'name', None),
                size=idm_utils.AVATAR_SIZE['img_original'])
            context['gravatar_image'] = gravatar_image
        if hasattr(
                self.object,
                'img_original') and getattr(self.object, 'img_original') != '':
            uploaded_image = getattr(self.object, 'img_original')
            uploaded_image = settings.MEDIA_URL + uploaded_image
        else:
            uploaded_image = (settings.STATIC_URL +
                              'dashboard/img/logos/original/user.png')
            context['no_uploaded_image'] = True
        context['uploaded_image'] = uploaded_image
        return context
Ejemplo n.º 5
0
    def update_vcard(self, force=False):
        """
        Update vcard if not updated within `XMPP_UPDATE_VCARD_HOURS` (default False)
        or if XMPP_UPDATE_VCARD is not False
        """
        if not getattr(settings, 'XMPP_UPDATE_VCARD', True):
            # Never ever update vCard
            return False

        update_delta = getattr(settings, 'XMPP_UPDATE_VCARD_HOURS', False)
        if not update_delta:
            return False

        if not force:
            if self.updated and self.updated > timezone.now()-datetime.timedelta(hours=update_delta):
                return False

        lg.info("Updating vCard for %s" % self.jid)
        try:
            con = self.get_connection()
            con.set_vcard(self.user.get_full_name() or self.user.username)
            if gravatar_available and has_gravatar(self.user.email):
                try:
                    avatar_data = urllib.urlopen(get_gravatar_url(self.user.email)).read()
                    con.set_avatar(avatar_data, mime_type='image/jpeg')
                except Exception as e:
                    lg.exception("Failed to set XMPP avatar for %s" % self.jid, e)
            con.disconnect()
        except Exception as e:
            lg.exception("Failed to update vCard for %s" % self.jid, e)

        self.updated = timezone.now()
        self.save()
Ejemplo n.º 6
0
def vosae_user_init(vosae_user):
    from django_gravatar.helpers import has_gravatar

    # Checks for gravatar email
    if not vosae_user.settings.gravatar_email:
        if has_gravatar(vosae_user.email):
            vosae_user.settings.gravatar_email = vosae_user.email
            vosae_user.update(set__settings__gravatar_email=vosae_user.email)
Ejemplo n.º 7
0
 def gravatar(self):
     email = EmailAddress.objects.get_primary(user=self.user).email
     return {
         'url': get_gravatar_url(email, size=150),
         'exists': has_gravatar(email),
         'profile': get_gravatar_profile_url(email),
         'hash': calculate_gravatar_hash(email)
     }
    def handle(self, *args, **options):
        for user_det in UserProfile.objects.all():
            if has_gravatar(user_det.user.email):
                user_det.gravatar_img = get_gravatar_url(user_det.user.email, size=200)
            else:
                user_det.gravatar_img = "http://dummyimage.com/200x200/fff/000.png&text=" + str(user_det.user.username[0:2])
            user_det.save()

        print "Updated gravatars!"
Ejemplo n.º 9
0
    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()

        email = cleaned_data.get("email")
        use_gravatar = cleaned_data.get("use_gravatar")

        if email and use_gravatar and not has_gravatar(email):
            raise forms.ValidationError("We couldn't find your Gravatar. Please make sure the e-mail you wrote is correct, or uncheck the Gravatar box", code='invalid')

        return cleaned_data
Ejemplo n.º 10
0
 def handle(self, *args, **options):
     for user in Account.objects.all():
         if has_gravatar(user.email):
             user.gravatar_url = get_gravatar_url(user.email, size=25)
             user.has_gravatar = True
         else:
             user.gravatar_url = user.first_name[:1].upper(
             ) + user.last_name[:1].upper()
             user.has_gravatar = False
         user.save()
Ejemplo n.º 11
0
 def post(self, request, *args, **kwargs):
     email = request.POST.get('email', None)
     if email:
         gravatar_exists = has_gravatar(email)
         if gravatar_exists:
             return Response({
                 'url': get_gravatar_url(email),
             })
     return Response({
         'message': 'Gravatar does not exist',
     })
Ejemplo n.º 12
0
    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()

        email = cleaned_data.get("email")
        use_gravatar = cleaned_data.get("use_gravatar")

        if email and use_gravatar and not has_gravatar(email):
            raise forms.ValidationError(
                "We couldn't find your Gravatar. Please make sure the e-mail you wrote is correct, or uncheck the Gravatar box",
                code='invalid')

        return cleaned_data
Ejemplo n.º 13
0
    def get_avatar_image(self):
        avatar_type = self.avatar_type
        if avatar_type == 'gravatar':
            gravatar_exists = has_gravatar(self.email)
            if gravatar_exists:
                return get_gravatar_url(self.email)
        else:
            if os.path.exists(self.avatar_image.file.name):
                return self.avatar_image
            else:
                return self.get_default_avatar_image()

        return UserProfile.get_default_avatar_image()
Ejemplo n.º 14
0
def userProfile(request, id=None):
    instance = get_object_or_404(User, pk=id)
    if request.user == instance:
        return redirect("profile")
    gravatar_exists = has_gravatar(instance.email)
    instanceInfo = UserInfo.objects.filter(user=instance).first()
    print(gravatar_exists)
    context = {
        "exists": gravatar_exists,
        "instance": instance,
        "instanceInfo": instanceInfo,
    }
    return render(request, "userProfile.html", context)
Ejemplo n.º 15
0
    def get_avatar_image(self):
        avatar_type = self.avatar_type
        if avatar_type == 'gravatar':
            gravatar_exists = has_gravatar(self.email)
            if gravatar_exists:
                return get_gravatar_url(self.email)
        else:
            if os.path.exists(self.avatar_image.file.name):
                return self.avatar_image
            else:
                return self.get_default_avatar_image()

        return UserProfile.get_default_avatar_image()
Ejemplo n.º 16
0
 def get_avatar_image(self):
     avatar_type = self.avatar_type
     if avatar_type == 'gravatar':
         gravatar_exists = has_gravatar(self.email)
         if gravatar_exists:
             return get_gravatar_url(self.email)
     elif avatar_type == 'social' or avatar_type == '':
         try:
             social_account = SocialAccount.objects.filter(user=self.user)[0]
             if social_account:
                 provider = social_account.get_provider_account()
                 return provider.get_avatar_url()
         except Exception, ex:
             pass
Ejemplo n.º 17
0
def profile(request):
    gravatar_exists = has_gravatar(request.user.email)
    instance = UserInfo.objects.filter(user=request.user).first()
    # print(gravatar_exists)
    # print(request.user.info.points)
    if instance.phone == "0":
        phoneAvailable = False
    else:
        phoneAvailable = True
    context = {
        "exists": gravatar_exists,
        "instance": instance,
        "phone": phoneAvailable,
    }
    return render(request, "profile.html", context)
Ejemplo n.º 18
0
 def last_modification(self):
     """
         returns the avatar last_modification
     """
     try:
         if has_gravatar(self.user.email):
             info = urlopen(get_gravatar_url(email=self.user.email, size=self.size)).info()
             return datetime.strptime(info['Last-Modified'], "%a, %d %b %Y %H:%M:%S GMT")
     except NameError:
         pass
     if AVATAR_STORAGE_BACKEND.exists(self.path()):
         try:
             return AVATAR_STORAGE_BACKEND.modified_time(self.path())
         except AttributeError:
             return timezone.now()
     else:
         return None
Ejemplo n.º 19
0
 def last_modification(self):
     """
         returns the avatar last_modification
     """
     try:
         if has_gravatar(self.user.email):
             info = urlopen(get_gravatar_url(email=self.user.email, size=self.size)).info()
             return datetime.strptime(info['Last-Modified'], "%a, %d %b %Y %H:%M:%S GMT")
     except NameError:
         pass
     if AVATAR_STORAGE_BACKEND.exists(self.path()):
         try:
             return AVATAR_STORAGE_BACKEND.modified_time(self.path())
         except AttributeError:
             return timezone.now()
     else:
         return None
Ejemplo n.º 20
0
 def get_avatar_url(self):
     """
         returns the url of the avatar on the storage backed
     """
     try:
         if has_gravatar(self.user.email):
             self.css_class = "gravatar"
             self.url = escape(get_gravatar_url(email=self.user.email, size=self.size))
             return self.url
     except NameError:
         pass
     self.css_class = "initial-avatar"
     if AVATAR_STORAGE_BACKEND.exists(self.path()):
         self.url = AVATAR_STORAGE_BACKEND.url(self.path())
     else:
         self.url = self.genavatar()
     return self.url
Ejemplo n.º 21
0
def get_avatar(obj, avatar_type, default_avatar):
    """Gets the object avatar or a default one."""

    if type(obj) == dict:
        use_gravatar = obj.get('use_gravatar', None)
        email = obj.get('name', None)
        avatar = obj.get(avatar_type, None)
    else:
        use_gravatar = getattr(obj, 'use_gravatar', None)
        email = getattr(obj, 'name', None)
        avatar = getattr(obj, avatar_type, None)

    if use_gravatar and has_gravatar(email):
        return get_gravatar_url(email, size=AVATAR_SIZE[avatar_type])
    if avatar and avatar != '':
        return settings.MEDIA_URL + avatar
    else:
        return settings.STATIC_URL + default_avatar
Ejemplo n.º 22
0
 def get_context_data(self, **kwargs):
     context = super(DetailUserView, self).get_context_data(**kwargs)
     user_id = self.kwargs['user_id']
     user = fiware_api.keystone.user_get(self.request, user_id)
     user_email = getattr(user, 'name', None)
     context['user'] = user
     if getattr(user, 'use_gravatar', None) and has_gravatar(user_email):
         image = get_gravatar_url(user_email, size=idm_utils.AVATAR_SIZE['img_original'])
     elif hasattr(user, 'img_original') and getattr(user, 'img_original') != '':
         image = getattr(user, 'img_original')
         image = settings.MEDIA_URL + image
     else:
         image = settings.STATIC_URL + 'dashboard/img/logos/original/user.png'
     context['image'] = image
     context['index_app'] = self.request.GET.get('index_app', 0)
     context['index_org'] = self.request.GET.get('index_org', 0)
     if self._can_edit():
         context['edit'] = True
     return context
Ejemplo n.º 23
0
def avatar_image(user, size=150):
    profile = user.get_profile()
    avatar_type = profile.avatar_type

    if avatar_type == 'gravatar':
        gravatar_exists = has_gravatar(user.email)
        if gravatar_exists:
            return get_gravatar_url(user.email, size)
    elif avatar_type == 'social' or avatar_type == '':
        try:
            social_account = SocialAccount.objects.filter(user = user)[0]
            if social_account:
                provider = social_account.get_provider_account()
                return provider.get_avatar_url()
        except:
            return urlparse.urljoin(settings.STATIC_URL, "img/default-avatar-32.png")
    elif avatar_type == 'custom':
        return profile.avatar_image.url

    return settings.STATIC_URL + "/images/default-avatar-32.png"
Ejemplo n.º 24
0
 def get_context_data(self, **kwargs):
     context = super(DetailUserView, self).get_context_data(**kwargs)
     user_id = self.kwargs['user_id']
     user = fiware_api.keystone.user_get(self.request, user_id)
     user_email = getattr(user, 'name', None)
     context['user'] = user
     if getattr(user, 'use_gravatar', None) and has_gravatar(user_email):
         image = get_gravatar_url(
             user_email, size=idm_utils.AVATAR_SIZE['img_original'])
     elif hasattr(user,
                  'img_original') and getattr(user, 'img_original') != '':
         image = getattr(user, 'img_original')
         image = settings.MEDIA_URL + image
     else:
         image = settings.STATIC_URL + 'dashboard/img/logos/original/user.png'
     context['image'] = image
     context['index_app'] = self.request.GET.get('index_app', 0)
     context['index_org'] = self.request.GET.get('index_org', 0)
     if self._can_edit():
         context['edit'] = True
     return context
Ejemplo n.º 25
0
    def get_avatar_url(self):
        """
            returns the url of the avatar on the storage backed
        """
        try:
            if has_gravatar(self.user.email):
                self.css_class = "gravatar"
                self.url = escape(get_gravatar_url(email=self.user.email, size=self.size))
                return self.url
        except NameError:
            pass

        self.css_class = "initial-avatar"
        if AVATAR_STORAGE_BACKEND.exists(self.path()):
            self.url = AVATAR_STORAGE_BACKEND.url(self.path())
        else:
            self.url = self.genavatar()
        if AVATAR_HIGH_RESOLUTION:
            self.high_res_url = AVATAR_STORAGE_BACKEND.url(self.path(high_res=True))
            return self.url, self.high_res_url
        return self.url
Ejemplo n.º 26
0
    def get_context_data(self, **kwargs):
        context = super(BaseUsersMultiFormView, self).get_context_data(**kwargs)

        user_id = self.kwargs['user_id']
        context['user_id'] = user_id

        if getattr(self.object, 'use_gravatar', None):
            context['using_gravatar'] = True
        
        if has_gravatar(getattr(self.object, 'name', None)):
            gravatar_image = get_gravatar_url(getattr(self.object, 'name', None), size=idm_utils.AVATAR_SIZE['img_original'])
            context['gravatar_image'] = gravatar_image
        if hasattr(self.object, 'img_original') and getattr(self.object, 'img_original') != '':
            uploaded_image = getattr(self.object, 'img_original')
            uploaded_image = settings.MEDIA_URL + uploaded_image
        else:
            uploaded_image = (settings.STATIC_URL + 
                'dashboard/img/logos/original/user.png')
            context['no_uploaded_image'] = True
        context['uploaded_image'] = uploaded_image
        return context
Ejemplo n.º 27
0
    def update_vcard(self, force=False):
        """
        Update vcard if not updated within `XMPP_UPDATE_VCARD_HOURS` (default False)
        or if XMPP_UPDATE_VCARD is not False
        """
        if not getattr(settings, 'XMPP_UPDATE_VCARD', True):
            # Never ever update vCard
            return False

        update_delta = getattr(settings, 'XMPP_UPDATE_VCARD_HOURS', False)
        if not update_delta:
            return False

        if not force:
            if self.updated and self.updated > timezone.now(
            ) - datetime.timedelta(hours=update_delta):
                return False

        lg.info("Updating vCard for %s" % self.jid)
        try:
            con = self.get_connection()
            con.set_vcard(self.user.get_full_name() or self.user.username)
            if gravatar_available and has_gravatar(self.user.email):
                try:
                    avatar_data = urllib.urlopen(
                        get_gravatar_url(self.user.email)).read()
                    con.set_avatar(avatar_data, mime_type='image/jpeg')
                except Exception as e:
                    lg.exception("Failed to set XMPP avatar for %s" % self.jid,
                                 e)
            con.disconnect()
        except Exception as e:
            lg.exception("Failed to update vCard for %s" % self.jid, e)

        self.updated = timezone.now()
        self.save()
Ejemplo n.º 28
0
 def get_avatar(self, size=25):
     # TODO: Add caching in future if rate limits are reached
     if has_gravatar(self.email):
         return get_gravatar_url(self.email, size=size)
     return f"https://www.gravatar.com/avatar/00000000000000000000000000000000?d=identicon&s={size}&f=y;'"
Ejemplo n.º 29
0
    def gravatar(self):
        if not has_gravatar(self.email):
            return None

        return get_gravatar_url(self.email)
Ejemplo n.º 30
0
 def _get_gravatar(self, size):
     if has_gravatar(self.user.email):
         return get_gravatar_url(self.user.email, size=size)
     else:
         return "http://robohash.org/%s.png" % self.user.email
Ejemplo n.º 31
0
def gravatar_exists(user):
    exists = has_gravatar(user.email)
    return exists