Skip to content

Commit

Permalink
[SDESK-7325] Move flask import to superdesk module
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Jul 30, 2024
1 parent b199e02 commit 7130db9
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 87 deletions.
13 changes: 8 additions & 5 deletions server/analytics/base_report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from flask import json, current_app as app
from eve_elastic.elastic import set_filters, ElasticCursor

from superdesk.core import json, get_app_config, get_current_app
from superdesk.resource_fields import ITEMS
from superdesk import get_resource_service, es_utils
from superdesk.resource import Resource
from superdesk.utils import ListCursor
Expand Down Expand Up @@ -272,8 +273,9 @@ def run_query(self, params, args):
filters = self._get_filters(types, excluded_stages)

# if the system has a setting value for the maximum search depth then apply the filter
if not app.settings["MAX_SEARCH_DEPTH"] == -1:
query["terminate_after"] = app.settings["MAX_SEARCH_DEPTH"]
max_search_depth = get_app_config("MAX_SEARCH_DEPTH")
if max_search_depth != -1:
query["terminate_after"] = max_search_depth

if filters:
set_filters(query, filters)
Expand All @@ -284,8 +286,9 @@ def run_query(self, params, args):

docs = self.elastic.search(query, types, params={})

app = get_current_app().as_any()
for resource in types:
response = {app.config["ITEMS"]: [doc for doc in docs if doc["_type"] == resource]}
response = {ITEMS: [doc for doc in docs if doc["_type"] == resource]}
getattr(app, "on_fetched_resource")(resource, response)
getattr(app, "on_fetched_resource_%s" % resource)(response)

Expand Down Expand Up @@ -330,7 +333,7 @@ def get(self, req, **lookup):
return ListCursor([report])

def get_utc_offset(self):
return get_timezone_offset(app.config["DEFAULT_TIMEZONE"], utcnow())
return get_timezone_offset(get_app_config("DEFAULT_TIMEZONE"), utcnow())

def format_date(self, date, end_of_day=False):
time_suffix = "T23:59:59" if end_of_day else "T00:00:00"
Expand Down
35 changes: 20 additions & 15 deletions server/analytics/commands/send_schedule_reports_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from datetime import datetime
from dateutil.rrule import rrule, HOURLY
import pytz
from unittest import mock
from os import urandom
from base64 import b64encode, b64decode

from superdesk.core import get_app_config
from superdesk import get_resource_service
from superdesk.utc import local_to_utc

Expand All @@ -16,25 +24,19 @@
from analytics.common import MIME_TYPES
from analytics.email_report.email_report import EmailReportService

from datetime import datetime
from flask import current_app as app
from dateutil.rrule import rrule, HOURLY
import pytz
from unittest import mock
from os import urandom
from base64 import b64encode, b64decode



def to_naive(date_str):
return datetime.strptime(date_str, "%Y-%m-%dT%H")


def to_utc(date_str):
return local_to_utc(app.config["DEFAULT_TIMEZONE"], datetime.strptime(date_str, "%Y-%m-%dT%H"))
return local_to_utc(get_app_config("DEFAULT_TIMEZONE"), datetime.strptime(date_str, "%Y-%m-%dT%H"))


def to_local(date_str):
local_tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"])
local_tz = pytz.timezone(get_app_config("DEFAULT_TIMEZONE"))
local_datetime = datetime.strptime(date_str, "%Y-%m-%dT%H")

return local_tz.localize(local_datetime)
Expand Down Expand Up @@ -94,8 +96,9 @@ def setUp(self):

def _test(self, report, start, end, expected_hits):
count = 0
default_timezone = get_app_config("DEFAULT_TIMEZONE")
for now in rrule(HOURLY, dtstart=to_naive(start), until=to_naive(end)):
local_tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"])
local_tz = pytz.timezone(default_timezone)
now_local = local_tz.localize(now)

response = self.should_send(report, now_local)
Expand All @@ -108,7 +111,7 @@ def _test(self, report, start, end, expected_hits):

if response:
# Update the last sent time to now
report["_last_sent"] = local_to_utc(app.config["DEFAULT_TIMEZONE"], now_local)
report["_last_sent"] = local_to_utc(default_timezone, now_local)
count += 1

self.assertEqual(len(expected_hits), count)
Expand Down Expand Up @@ -145,11 +148,12 @@ def test_run_hourly_png(self, mocked):
# Simulate running every hour for a few hours
start_date = to_naive("2018-06-30T00")
end_date = to_naive("2018-06-30T03")
local_tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"])
default_timezone = get_app_config("DEFAULT_TIMEZONE")
local_tz = default_timezone
with self.app.mail.record_messages() as outbox:
for now in rrule(HOURLY, dtstart=start_date, until=end_date):
now_local = local_tz.localize(now)
now_utc = local_to_utc(app.config["DEFAULT_TIMEZONE"], now_local)
now_utc = local_to_utc(default_timezone, now_local)

SendScheduledReports().run(now_utc)

Expand Down Expand Up @@ -214,11 +218,12 @@ def test_run_daily_jpeg(self, mocked):
start_date = to_naive("2018-06-30T00")
end_date = to_naive("2018-06-30T03")
should_have_updated = False
default_timezone = get_app_config("DEFAULT_TIMEZONE")
with self.app.mail.record_messages() as outbox:
for now in rrule(HOURLY, dtstart=start_date, until=end_date):
local_tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"])
local_tz = pytz.timezone(default_timezone)
now_local = local_tz.localize(now)
now_utc = local_to_utc(app.config["DEFAULT_TIMEZONE"], now_local)
now_utc = local_to_utc(default_timezone, now_local)

SendScheduledReports().run(now_utc)

Expand Down
16 changes: 7 additions & 9 deletions server/analytics/commands/send_scheduled_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from datetime import datetime

from superdesk.core import get_app_config
from superdesk import Command, command, Option, get_resource_service
from superdesk.logging import logger
from superdesk.errors import SuperdeskApiError
from superdesk.utc import utc_to_local, utcnow, local_to_utc

from flask import current_app as app
from datetime import datetime


class SendScheduledReports(Command):
"""
Expand All @@ -40,19 +40,17 @@ class SendScheduledReports(Command):
]

def run(self, now=None):
default_timezone = get_app_config("DEFAULT_TIMEZONE")
if now:
now_utc = (
now
if isinstance(now, datetime)
else local_to_utc(
app.config["DEFAULT_TIMEZONE"],
datetime.strptime(now, "%Y-%m-%dT%H"),
)
else local_to_utc(default_timezone, datetime.strptime(now, "%Y-%m-%dT%H"))
)
else:
now_utc = utcnow()

now_local = utc_to_local(app.config["DEFAULT_TIMEZONE"], now_utc)
now_local = utc_to_local(default_timezone, now_utc)

logger.info("Starting to send scheduled reports: {}".format(now_utc))

Expand Down Expand Up @@ -99,7 +97,7 @@ def should_send_report(scheduled_report, now_local):

last_sent = None
if scheduled_report.get("_last_sent"):
last_sent = utc_to_local(app.config["DEFAULT_TIMEZONE"], scheduled_report.get("_last_sent")).replace(
last_sent = utc_to_local(get_app_config("DEFAULT_TIMEZONE"), scheduled_report.get("_last_sent")).replace(
minute=0, second=0, microsecond=0
)

Expand Down
19 changes: 10 additions & 9 deletions server/analytics/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

from typing import NamedTuple
from os import path

from superdesk import get_resource_service
from superdesk.utc import utcnow, utc_to_local
from subprocess import check_call, PIPE
from flask import current_app as app
import pytz
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from math import floor
import re
import logging

from superdesk.core import get_app_config, get_current_app
from superdesk import get_resource_service
from superdesk.utc import utcnow, utc_to_local


ANALYTICS_PATH = path.abspath(path.dirname(path.realpath(__file__)))

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -168,6 +168,7 @@ def get_cv_by_qcode(name, field=None):


def get_elastic_version():
app = get_current_app()
return app.data.elastic.es.info()["version"]["number"]


Expand All @@ -181,7 +182,7 @@ def get_weekstart_offset_hr():
"""
offset = 0

start_of_week = app.config.get("START_OF_WEEK") or 0
start_of_week = get_app_config("START_OF_WEEK") or 0
if start_of_week == 0:
offset -= 24
elif start_of_week > 1:
Expand All @@ -196,7 +197,7 @@ def get_utc_offset_in_minutes(utc_datetime):
:param datetime utc_datetime: The date/time instance used to calculate utc offset
:return: UTC Offset in minutes
"""
timezone = pytz.timezone(app.config["DEFAULT_TIMEZONE"])
timezone = pytz.timezone(get_app_config("DEFAULT_TIMEZONE"))
return timezone.utcoffset(utc_datetime).total_seconds() / 60


Expand Down Expand Up @@ -267,7 +268,7 @@ def relative_to_absolute_datetime(value, format, now=None, offset=None):
raise

if now is None:
now = utc_to_local(app.config.get("DEFAULT_TIMEZONE"), utcnow())
now = utc_to_local(get_app_config("DEFAULT_TIMEZONE"), utcnow())

if values.get("offset"):
# Retrieve the offset value and granularity, then shift the datetime
Expand Down Expand Up @@ -310,7 +311,7 @@ def relative_to_absolute_datetime(value, format, now=None, offset=None):
if isoweekday == 7:
isoweekday = 0

start_of_week = app.config.get("START_OF_WEEK") or 0
start_of_week = get_app_config("START_OF_WEEK") or 0
offset = 7 - start_of_week + isoweekday

if offset < 7:
Expand Down
8 changes: 4 additions & 4 deletions server/analytics/desk_activity_report/desk_activity_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from datetime import datetime

from superdesk.core import get_app_config
from superdesk.resource import Resource
from superdesk.errors import SuperdeskApiError

Expand All @@ -21,9 +24,6 @@
MAX_TERMS_SIZE,
)

from flask import current_app as app
from datetime import datetime


class DeskActivityReportResource(Resource):
"""Desk Activity Report schema"""
Expand Down Expand Up @@ -202,7 +202,7 @@ def gen_chart_config():
title=title,
subtitle=subtitle,
chart_type="highcharts",
start_of_week=app.config.get("START_OF_WEEK") or 0,
start_of_week=get_app_config("START_OF_WEEK") or 0,
timezone_offset=timezone_offset,
use_utc=False,
legend_title="Desk Transitions",
Expand Down
8 changes: 4 additions & 4 deletions server/analytics/email_report/analytics_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from superdesk.emails import SuperdeskMessage

import re
import unicodedata

Expand All @@ -18,7 +16,6 @@
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

from flask import current_app
from flask_mail import (
sanitize_subject,
sanitize_address,
Expand All @@ -27,6 +24,9 @@
formatdate,
)

from superdesk.core import get_current_app
from superdesk.emails import SuperdeskMessage

string_types = (str,)
text_type = str
message_policy = policy.SMTP
Expand All @@ -35,7 +35,7 @@
class AnalyticsMessage(SuperdeskMessage):
def _message(self):
"""Creates email as 'multipart/related' instead of 'multipart/mixed'"""
ascii_attachments = current_app.extensions["mail"].ascii_attachments
ascii_attachments = get_current_app().extensions["mail"].ascii_attachments
encoding = self.charset or "utf-8"

attachments = self.attachments or []
Expand Down
16 changes: 9 additions & 7 deletions server/analytics/email_report/email_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from email.charset import Charset, QP
from base64 import b64decode
from uuid import uuid4
from bson import ObjectId

from superdesk.core import get_current_app, get_app_config
from superdesk.flask import render_template
from superdesk.services import BaseService
from superdesk.resource import Resource
from superdesk.errors import SuperdeskApiError
Expand All @@ -23,12 +30,6 @@
from analytics.reports import generate_report
from .analytics_message import AnalyticsMessage

from flask import current_app as app, render_template
from email.charset import Charset, QP
from base64 import b64decode
from uuid import uuid4
from bson import ObjectId


class EmailReportResource(Resource):
"""Resource to email report charts"""
Expand Down Expand Up @@ -175,7 +176,7 @@ def _email_report(email, attachments):
kwargs={
"_id": str(ObjectId()),
"subject": email.get("subject"),
"sender": email.get("sender") or app.config["ADMINS"][0],
"sender": email.get("sender") or get_app_config("ADMINS")[0],
"recipients": email.get("recipients"),
"text_body": txt.get("body") or "",
"html_body": html.get("body") or "",
Expand Down Expand Up @@ -261,6 +262,7 @@ def send_email_report(
html_body=html_body.replace("\r", "").replace("\n", "<br>"),
reports=reports,
)
app = get_current_app()

return app.mail.send(msg)
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from eve_elastic.elastic import parse_date

from superdesk.core import get_app_config
from superdesk.resource import Resource
from superdesk.utc import utc_to_local

from analytics.stats.stats_report_service import StatsReportService
from analytics.chart_config import ChartConfig
from analytics.common import REPORT_CONFIG, CHART_TYPES

from flask import current_app as app
from eve_elastic.elastic import parse_date


class FeaturemdiaUpdatesReportResource(Resource):
"""Featuremedia Updates Report schema"""
Expand Down Expand Up @@ -175,7 +175,7 @@ def generate_highcharts_config(self, docs, args):
rows = []

def gen_date_str(date):
return utc_to_local(app.config["DEFAULT_TIMEZONE"], date).strftime("%d/%m/%Y %H:%M")
return utc_to_local(get_app_config("DEFAULT_TIMEZONE"), date).strftime("%d/%m/%Y %H:%M")

for item in items:
original_image = item.get("original_image") or {}
Expand Down
Loading

0 comments on commit 7130db9

Please sign in to comment.