示例#1
0
def generate_banner_instance_image(banner_instance_id):
    """
    Create an image for a banner image by adding the user's profile image to the
    banner image.
    """
    banner_instance = get_object_or_none(FacebookBannerInstance,
                                         id=banner_instance_id)
    if banner_instance is not None:
        banner_image_file = banner_instance.image

        # Grab the user's photo from Facebook
        try:
            r = requests.get(banner_instance.user.picture_url)
        except requests.exceptions.RequestException, e:
            log.error('Error downloading photo for user %s: %s' %
                      (banner_instance.user.id, e))
            return

        try:
            user_image = Image.open(StringIO(r.content))
            user_image.load()
            banner_image = Image.open(banner_image_file)
            banner_image.load()
        except ValueError, e:
            log.error('Error loading images for banner instance %s: %s' %
                      (banner_instance.id, e))
            return
示例#2
0
def get_linked_account(self):
    """
    Return the FacebookUser linked with this account, or None if no account has
    been linked.
    """
    return get_object_or_none(FacebookUser, _account_link__affiliates_user=self,
                              _account_link__is_active=True)
示例#3
0
    def create_link(self, facebook_user, affiliates_email):
        """
        Attempts to link a FacebookUser to an Affiliates user account. The link
        has to be confirmed via a link in the verification email before it is
        finalized.
        """
        affiliates_user = get_object_or_none(User, email=affiliates_email)
        if affiliates_user is None:
            return False

        # Exit early if the affiliates user already has an active account link.
        if affiliates_user.account_links.filter(is_active=True).exists():
            return False

        try:
            link = self.get(facebook_user=facebook_user)
        except self.model.DoesNotExist:
            link = self.model(facebook_user=facebook_user)
        link.affiliates_user = affiliates_user

        # Exit early if an active link already exists.
        if link.is_active:
            return False

        # Even if the link is old, generate a fresh activation code.
        token_generator = self.get_token_generator(link)
        link.activation_code = token_generator.generate_token()
        link.save()
        return link
示例#4
0
    def create_link(self, facebook_user, affiliates_email):
        """
        Attempts to link a FacebookUser to an Affiliates user account. The link
        has to be confirmed via a link in the verification email before it is
        finalized.
        """
        affiliates_user = get_object_or_none(User, email=affiliates_email)
        if affiliates_user is None:
            return False

        # Exit early if the affiliates user already has an active account link.
        if affiliates_user.account_links.filter(is_active=True).exists():
            return False

        try:
            link = self.get(facebook_user=facebook_user)
        except self.model.DoesNotExist:
            link = self.model(facebook_user=facebook_user)
        link.affiliates_user = affiliates_user

        # Exit early if an active link already exists.
        if link.is_active:
            return False

        # Even if the link is old, generate a fresh activation code.
        token_generator = self.get_token_generator(link)
        link.activation_code = token_generator.generate_token()
        link.save()
        return link
示例#5
0
def add_click(banner_instance_id):
    """Add a click to the specified banner instance."""
    banner_instance = get_object_or_none(FacebookBannerInstance,
                                         id=banner_instance_id)
    if banner_instance is not None:
        banner_instance.total_clicks += 1
        banner_instance.save()

        stats, created = (FacebookClickStats.objects
                          .get_or_create(hour=current_hour(),
                                         banner_instance=banner_instance))
        stats.clicks += 1
        stats.save()

        total_clicks = banner_instance.total_clicks

        # Notify admin of a banner meeting the click goal.
        if total_clicks == settings.FACEBOOK_CLICK_GOAL:
            subject = '[fb-affiliates-banner] Click Goal Reached!'
            message = render_to_string('facebook/click_goal_email.html', {
                'goal': settings.FACEBOOK_CLICK_GOAL,
                'banner_instance': banner_instance,
                'now': datetime.now()
            })
            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
                      [settings.FACEBOOK_CLICK_GOAL_EMAIL])

        # Notify user of click milestones.
        if total_clicks in CLICK_MILESTONES:
            message = CLICK_MILESTONES[total_clicks]
            AppNotification.objects.create(user=banner_instance.user,
                                           message=message,
                                           format_argument=total_clicks)
示例#6
0
def get_linked_account(self):
    """
    Return the FacebookUser linked with this account, or None if no account has
    been linked.
    """
    return get_object_or_none(FacebookUser,
                              _account_link__affiliates_user=self,
                              _account_link__is_active=True)
示例#7
0
    def _attr_for_locale(self, attr, locale):
        """
        Return an attribute's value on a locale associated with this banner. If
        the attribute is empty or no locale is found, return the attribute on
        this banner instead.
        """
        banner_locale = get_object_or_none(FacebookBannerLocale, banner=self,
                                           locale=locale)
        if banner_locale is None:
            # Try just the language code.
            lang = locale.split('-')[0]
            banner_locale = get_object_or_none(FacebookBannerLocale,
                                               banner=self, locale=lang)

        if banner_locale is None or not getattr(banner_locale, attr, None):
            return getattr(self, attr)
        else:
            return getattr(banner_locale, attr)
示例#8
0
    def activate_link(self, activation_code):
        """Verify activation code and activate the corresponding link."""
        link = get_object_or_none(self.model, activation_code=activation_code)
        if link is None or link.is_active:
            return None

        token_generator = self.get_token_generator(link)
        if not token_generator.verify_token(activation_code):
            return None

        link.is_active = True
        link.save()
        return link
示例#9
0
    def activate_link(self, activation_code):
        """Verify activation code and activate the corresponding link."""
        link = get_object_or_none(self.model, activation_code=activation_code)
        if link is None or link.is_active:
            return None

        token_generator = self.get_token_generator(link)
        if not token_generator.verify_token(activation_code):
            return None

        link.is_active = True
        link.save()
        return link
示例#10
0
def notify_ad_approval(sender, instance, **kwargs):
    """Notify the user when their banner instance passes review."""
    # Don't bother if the banner hasn't passed review yet.
    if instance.review_status != FacebookBannerInstance.PASSED:
        return

    # Don't bother if this instance is new.
    old_instance = get_object_or_none(FacebookBannerInstance, id=instance.id)
    if old_instance is None:
        return

    # Bother if the review status is changing.
    if old_instance.review_status != instance.review_status:
        # String is stored in apps/facebook/templates/facebook/strings.html
        # for localization.
        AppNotification.objects.create(user=instance.user,
                                       message='banner_approved')
示例#11
0
 def test_get(self):
     user = User.objects.create_user('get_object_or_none_test', '*****@*****.**',
                                     None)
     eq_(get_object_or_none(User, username='******'), user)
示例#12
0
 def test_none(self):
     eq_(get_object_or_none(User, username='******'), None)
示例#13
0
def update_user_info(user_id):
    user = get_object_or_none(FacebookUser, id=user_id)
    if user is not None:
        FacebookUser.objects.update_user_info(user)
示例#14
0
 def test_get(self):
     user = User.objects.create_user('get_object_or_none_test', '*****@*****.**',
                                     None)
     eq_(get_object_or_none(User, username='******'), user)
示例#15
0
 def test_none(self):
     eq_(get_object_or_none(User, username='******'), None)