Esempio n. 1
0
def create_email_batch(
    date_string, sender, recipients, subject, byline, email_text, story_text, mediaitems=None, attachment=None
):
    """
    Task for emailing published articles.
    Calls a subtask for each recipient, so it sends one email at a time.
    2 second sleep to avoid AWS rate limits
    Task is called by story management command send_article
    message_users is an async_messages object and notification is sent to all Editors
    """

    for r in recipients:
        send_published_article.delay(
            date_string, sender, r, subject, byline, email_text, story_text, attachment, mediaitems
        )
        time.sleep(2)
    msg = (
        'Emails for story: "'
        + subject
        + '" -- '
        + byline
        + " has been sent on "
        + datetime.datetime.now().strftime("%m/%d/%Y - at %I:%M %p")
    )
    recip = UserProfile.objects.filter(user_type="Editor")
    message_users(recip, msg, constants.SUCCESS)
Esempio n. 2
0
def initial_parser(id):
    try:
        staff = User.objects.filter(is_staff=True)
        created_fund = Fund.objects.get(id=id)
        processing_fund = FundParser(created_fund.isin_ticker,
                                     created_fund.bloombreg_ticker)
        processing_fund.parse()
        # saving fund data
        created_fund.name = processing_fund.name
        created_fund.fund_manager = processing_fund.fund_manager
        created_fund.birth_date = processing_fund.birth_date
        created_fund.country = Country(code=pycountry.countries.get(
            name=processing_fund.country).alpha2)
        created_fund.slug = slugify(processing_fund.name, to_lower=True)
        created_fund.currency = processing_fund.currency
        created_fund.total_assets = round(float(processing_fund.total_assets),
                                          3)
        created_fund.range_52_weeks = processing_fund.range_52_weeks
        created_fund.return_1_year = round(
            float(processing_fund.return_1_year.strip('%')), 2)
        created_fund.investment_strategy = str(
            processing_fund.investment_strategy)
        created_fund.save()

        with transaction.atomic():
            # cleaning all previous history in case if Reparse method
            NAV.objects.filter(fund=created_fund).delete()

            # saving history data
            for price_item in processing_fund.price_history:
                NAV(
                    fund=created_fund,
                    date=price_item[0],
                    price=round(float(price_item[1]), 2),
                ).save()

            # cleaning all previous TopFundHolding in case if Reparse method
            TopFundHolding.objects.filter(fund=created_fund).delete()

            # saving top_fund_holdings data
            for top_fund_item in processing_fund.top_fund_holdings:
                TopFundHolding(fund=created_fund,
                               portfolio_weight=round(
                                   float(top_fund_item[1].strip('%')), 2),
                               name=top_fund_item[0]).save()
            # sending async message
            message_users(
                staff, "{} successfully downloaded".format(created_fund.name))
    except Exception as error:
        created_fund.name += ' (error during parsing)'
        created_fund.save()
        message_users(staff, "{} error".format(str(error)), constants.WARNING)
        logging.exception(
            '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n', error)
 def test_message_appears_all_users(self):
     message_users(User.objects.all(), "Hello")
     response = self.client.get('/')
     msgs = list(response.context['messages'])
     self.assertEqual(1, len(msgs))
     self.assertEqual('Hello', str((msgs)[0]))
Esempio n. 4
0
 def test_message_appears_all_users(self):
     message_users(User.objects.all(), "Hello")
     response = self.client.get('/')
     messages = list(response.context['messages'])
     self.assertEqual(1, len(messages))
     self.assertEqual('Hello', str(messages[0]))
Esempio n. 5
0
    def run(self, queue):
        """
        Fetch the issue with the given number for the current repository
        """
        super(FetchIssueByNumber, self).run(queue)

        gh = self.gh
        if not gh:
            return  # it's delayed !

        repository_id, issue_number = self.identifier.hget().split('#')

        repository = self.repository

        users_to_inform = self.users_to_inform.smembers()
        if users_to_inform:
            try:
                users_to_inform = GithubUser.objects.filter(id__in=users_to_inform)
            except GithubUser.DoesNotExist:
                users_to_inform = []

        try:
            issue = repository.issues.get(number=issue_number)
        except Issue.DoesNotExist:
            issue = Issue(repository=repository, number=issue_number)

        force_fetch = self.force_fetch.hget() == '1'
        force_fetch_all = self.force_fetch_all.hget() == '1'
        try:
            # prefetch full data if wanted
            if force_fetch and not force_fetch_all:
                if repository.has_issues:
                    issue.fetch(gh, force_fetch=True)
                if issue.is_pull_request:
                    issue.fetch_pr(gh, force_fetch=True)

            # now the normal fetch, if we previously force fetched they'll result in 304
            # except if force_fetch_all
            issue.fetch_all(gh, force_fetch=force_fetch_all)
        except ApiNotFoundError, e:
            # we have a 404, but... check if it's the issue itself
            try:
                issue.fetch(gh)
            except ApiNotFoundError:
                # ok the issue doesn't exist anymore, delete id
                if users_to_inform:
                    message_users(users_to_inform,
                        'The %s <strong>#%d</strong> from <strong>%s</strong> you asked to fetch from Github doesn\'t exist anymore!' % (
                            issue.type, issue.number, issue.repository.full_name),
                        constants.ERROR)
                issue.delete()
                self.deleted.hset(1)
                return False
            else:
                if users_to_inform:
                    message_users(users_to_inform,
                        'The %s <strong>#%d</strong> from <strong>%s</strong> you asked to fetch from Github couldn\'t be fetched!' % (
                            issue.type, issue.number, issue.repository.full_name),
                        constants.ERROR)

                raise e
Esempio n. 6
0
                        constants.ERROR)
                issue.delete()
                self.deleted.hset(1)
                return False
            else:
                if users_to_inform:
                    message_users(users_to_inform,
                        'The %s <strong>#%d</strong> from <strong>%s</strong> you asked to fetch from Github couldn\'t be fetched!' % (
                            issue.type, issue.number, issue.repository.full_name),
                        constants.ERROR)

                raise e
        else:
            if users_to_inform:
                message_users(users_to_inform,
                    'The %s <strong>#%d</strong> from <strong>%s</strong> you asked to fetch from Github was updated' % (
                        issue.type, issue.number, issue.repository.full_name),
                    constants.SUCCESS)

        return True

    def success_message_addon(self, queue, result):
        result = ''
        if self.force_fetch_all.hget() == '1':
            result += ' [force_fetch=all]'
        elif self.force_fetch.hget() == '1':
            result += ' [force_fetch=1]'
        if result is False:
            result += ' [deleted]'
        return result

Esempio n. 7
0
def message_admins(*args, **kwargs):
    """ Asynchronously send messages to all administrators.

    :param preference: if set, the corresponding preference of each admin
        user is checked to see if he/she wants to receive the message or not.
    :type preference: str, unicode or None (default)

    :param timestamping: if ``True``, a timestamp is prepended to the message.
        The timestamp will be translated in the user langage and timezone.
    :type timestamping: bool (default: ``True``)

    """

    timestamping = kwargs.pop('timestamping', True)
    preference = kwargs.pop('preference', None)

    admins = User.objects.filter(is_active=True).filter(
        Q(is_superuser=True) | Q(is_staff=True)).filter(
        preferences__staff__super_powers_enabled=True)

    if preference is not None:
        query_kwargs = {'preference__staff__{}'.format(preference): True}
        admins.filter(**query_kwargs)

    if timestamping:
        args = list(args)

        # This is an UTC datetime.
        dtnow = now()

        initial_lang = translation.get_language()
        prev_lang = initial_lang

        for admin in admins:
            uargs = args[:]

            # default datetime is UTC.
            udtnow = dtnow

            try:
                if admin.account.timezone:
                    udtnow = dtnow.astimezone(pytz.timezone(
                                              admin.account.timezone))

                if admin.account.language:
                    if prev_lang != admin.account.language:
                        translation.activate(admin.account.language)
                        prev_lang = admin.account.language

                    uargs[0] = _(u'{}: {}').format(
                        formats.date_format(udtnow, 'SHORT_DATETIME_FORMAT'),
                        uargs[0])

            except ObjectDoesNotExist:
                # Mostly "user has no account"
                LOGGER.warning('Admin %s has no account ?', admin)

            message_user(admin, *uargs, **kwargs)

        if initial_lang is not None:
            translation.activate(initial_lang)

    else:
        message_users(admins, *args, **kwargs)