From 2b43a0302a72840080791ec214e0307fabe09cd8 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 15 Nov 2017 15:55:49 -0800 Subject: [PATCH] python: Sort imports in smaller apps. --- analytics/lib/counts.py | 26 +++++----- analytics/lib/fixtures.py | 4 +- analytics/lib/time_utils.py | 7 ++- analytics/management/commands/analyze_mit.py | 9 ++-- .../commands/analyze_user_activity.py | 8 ++-- .../commands/clear_analytics_tables.py | 5 +- .../management/commands/clear_single_stat.py | 7 ++- .../management/commands/client_activity.py | 5 +- .../commands/populate_analytics_db.py | 16 +++---- analytics/management/commands/realm_stats.py | 9 ++-- analytics/management/commands/stream_stats.py | 6 ++- .../commands/update_analytics_counts.py | 12 ++--- analytics/management/commands/user_stats.py | 4 +- analytics/migrations/0001_initial.py | 4 +- .../migrations/0002_remove_huddlecount.py | 1 - analytics/migrations/0003_fillstate.py | 2 +- analytics/migrations/0004_add_subgroup.py | 1 - analytics/migrations/0005_alter_field_size.py | 1 - ...0006_add_subgroup_to_unique_constraints.py | 1 - analytics/migrations/0007_remove_interval.py | 1 - .../migrations/0008_add_count_indexes.py | 1 - .../0009_remove_messages_to_stream_stat.py | 3 +- .../0010_clear_messages_sent_values.py | 2 +- .../migrations/0011_clear_analytics_tables.py | 3 +- analytics/models.py | 9 ++-- analytics/tests/test_counts.py | 35 +++++++------- analytics/tests/test_fixtures.py | 3 +- analytics/tests/test_views.py | 29 ++++++----- analytics/urls.py | 4 +- analytics/views.py | 48 +++++++++---------- .../commands/add_mock_conversation.py | 15 +++--- zilencer/management/commands/add_new_realm.py | 12 ++--- zilencer/management/commands/add_new_user.py | 5 +- .../management/commands/compare_messages.py | 2 - .../commands/mark_all_messages_unread.py | 5 +- .../commands/migrate_stream_notifications.py | 1 + zilencer/management/commands/populate_db.py | 35 +++++++------- .../commands/print_initial_password.py | 2 +- .../management/commands/profile_request.py | 15 +++--- .../management/commands/render_messages.py | 3 +- zilencer/management/commands/sync_api_key.py | 7 +-- zilencer/migrations/0001_initial.py | 3 +- .../migrations/0002_remote_zulip_server.py | 3 +- ...or_remotezulipserver_last_updated_field.py | 1 - .../0004_remove_deployment_model.py | 1 - ...05_remotepushdevicetoken_fix_uniqueness.py | 1 - zilencer/models.py | 5 +- zilencer/urls.py | 4 +- zilencer/views.py | 13 +++-- 49 files changed, 189 insertions(+), 210 deletions(-) diff --git a/analytics/lib/counts.py b/analytics/lib/counts.py index 0f406a06d9..2a9ed438b5 100644 --- a/analytics/lib/counts.py +++ b/analytics/lib/counts.py @@ -1,21 +1,21 @@ +import time +from collections import OrderedDict, defaultdict +from datetime import datetime, timedelta +from typing import Any, Callable, Dict, List, \ + Optional, Text, Tuple, Type, Union + from django.conf import settings from django.db import connection, models from django.db.models import F -from analytics.models import InstallationCount, RealmCount, \ - UserCount, StreamCount, BaseCount, FillState, Anomaly, installation_epoch, \ - last_successful_fill -from zerver.models import Realm, UserProfile, Message, Stream, \ - UserActivityInterval, RealmAuditLog, models -from zerver.lib.timestamp import floor_to_hour, ceiling_to_day, \ - ceiling_to_hour, verify_UTC - -from typing import Any, Callable, Dict, List, Optional, Text, Tuple, Type, Union - -from collections import defaultdict, OrderedDict -from datetime import timedelta, datetime +from analytics.models import Anomaly, BaseCount, \ + FillState, InstallationCount, RealmCount, StreamCount, \ + UserCount, installation_epoch, last_successful_fill from zerver.lib.logging_util import create_logger -import time +from zerver.lib.timestamp import ceiling_to_day, \ + ceiling_to_hour, floor_to_hour, verify_UTC +from zerver.models import Message, Realm, RealmAuditLog, \ + Stream, UserActivityInterval, UserProfile, models ## Logging setup ## diff --git a/analytics/lib/fixtures.py b/analytics/lib/fixtures.py index e6ee51db8e..8b90403865 100644 --- a/analytics/lib/fixtures.py +++ b/analytics/lib/fixtures.py @@ -1,9 +1,9 @@ -from analytics.lib.counts import CountStat - from math import sqrt from random import gauss, random, seed from typing import List +from analytics.lib.counts import CountStat + def generate_time_series_data(days=100, business_hours_base=10, non_business_hours_base=10, growth=1, autocorrelation=0, spikiness=1, holiday_rate=0, frequency=CountStat.DAY, partial_sum=False, random_seed=26): diff --git a/analytics/lib/time_utils.py b/analytics/lib/time_utils.py index 315c49db10..4b48bc97e0 100644 --- a/analytics/lib/time_utils.py +++ b/analytics/lib/time_utils.py @@ -1,10 +1,9 @@ -from zerver.lib.timestamp import floor_to_hour, floor_to_day, \ - verify_UTC -from analytics.lib.counts import CountStat - from datetime import datetime, timedelta from typing import List, Optional +from analytics.lib.counts import CountStat +from zerver.lib.timestamp import floor_to_day, floor_to_hour, verify_UTC + # If min_length is None, returns end_times from ceiling(start) to floor(end), inclusive. # If min_length is greater than 0, pads the list to the left. # So informally, time_range(Sep 20, Sep 22, day, None) returns [Sep 20, Sep 21, Sep 22], diff --git a/analytics/management/commands/analyze_mit.py b/analytics/management/commands/analyze_mit.py index 5dc4f7889e..58d5da4b0f 100644 --- a/analytics/management/commands/analyze_mit.py +++ b/analytics/management/commands/analyze_mit.py @@ -1,11 +1,12 @@ +import datetime +import logging +import time from typing import Any, Dict from django.core.management.base import BaseCommand, CommandParser -from zerver.models import Recipient, Message + from zerver.lib.timestamp import timestamp_to_datetime -import datetime -import time -import logging +from zerver.models import Message, Recipient def compute_stats(log_level): # type: (int) -> None diff --git a/analytics/management/commands/analyze_user_activity.py b/analytics/management/commands/analyze_user_activity.py index 02fdd32319..09f3b7172f 100644 --- a/analytics/management/commands/analyze_user_activity.py +++ b/analytics/management/commands/analyze_user_activity.py @@ -1,12 +1,12 @@ +import datetime from typing import Any, Dict -from zerver.lib.statistics import seconds_usage_between - from django.core.management.base import BaseCommand, CommandParser -from zerver.models import UserProfile -import datetime from django.utils.timezone import utc +from zerver.lib.statistics import seconds_usage_between +from zerver.models import UserProfile + def analyze_activity(options): # type: (Dict[str, Any]) -> None day_start = datetime.datetime.strptime(options["date"], "%Y-%m-%d").replace(tzinfo=utc) diff --git a/analytics/management/commands/clear_analytics_tables.py b/analytics/management/commands/clear_analytics_tables.py index 56ddd43e39..ff9226e442 100644 --- a/analytics/management/commands/clear_analytics_tables.py +++ b/analytics/management/commands/clear_analytics_tables.py @@ -1,12 +1,11 @@ import sys - from argparse import ArgumentParser +from typing import Any + from django.core.management.base import BaseCommand from analytics.lib.counts import do_drop_all_analytics_tables -from typing import Any - class Command(BaseCommand): help = """Clear analytics tables.""" diff --git a/analytics/management/commands/clear_single_stat.py b/analytics/management/commands/clear_single_stat.py index 779ff78544..ceba4872e9 100644 --- a/analytics/management/commands/clear_single_stat.py +++ b/analytics/management/commands/clear_single_stat.py @@ -1,11 +1,10 @@ import sys - from argparse import ArgumentParser +from typing import Any + from django.core.management.base import BaseCommand -from analytics.lib.counts import do_drop_single_stat, COUNT_STATS - -from typing import Any +from analytics.lib.counts import COUNT_STATS, do_drop_single_stat class Command(BaseCommand): help = """Clear analytics tables.""" diff --git a/analytics/management/commands/client_activity.py b/analytics/management/commands/client_activity.py index 91882a94c6..339af3df0b 100644 --- a/analytics/management/commands/client_activity.py +++ b/analytics/management/commands/client_activity.py @@ -1,14 +1,13 @@ +import datetime +from argparse import ArgumentParser from typing import Any -from argparse import ArgumentParser from django.db.models import Count, QuerySet from django.utils.timezone import now as timezone_now from zerver.lib.management import ZulipBaseCommand from zerver.models import UserActivity -import datetime - class Command(ZulipBaseCommand): help = """Report rough client activity globally, for a realm, or for a user diff --git a/analytics/management/commands/populate_analytics_db.py b/analytics/management/commands/populate_analytics_db.py index 2c9bbc476f..12d02b2881 100644 --- a/analytics/management/commands/populate_analytics_db.py +++ b/analytics/management/commands/populate_analytics_db.py @@ -1,19 +1,17 @@ +from datetime import datetime, timedelta +from typing import Any, Dict, List, Mapping, Optional, Text, Type, Union + from django.core.management.base import BaseCommand from django.utils.timezone import now as timezone_now -from analytics.lib.counts import COUNT_STATS, CountStat, do_drop_all_analytics_tables +from analytics.lib.counts import COUNT_STATS, \ + CountStat, do_drop_all_analytics_tables from analytics.lib.fixtures import generate_time_series_data from analytics.lib.time_utils import time_range -from analytics.models import BaseCount, RealmCount, \ - UserCount, FillState +from analytics.models import BaseCount, FillState, RealmCount, UserCount from zerver.lib.timestamp import floor_to_day -from zerver.models import Realm, UserProfile, Client, \ - RealmAuditLog - -from datetime import datetime, timedelta - -from typing import Any, Dict, List, Optional, Text, Type, Union, Mapping +from zerver.models import Client, Realm, RealmAuditLog, UserProfile class Command(BaseCommand): help = """Populates analytics tables with randomly generated data.""" diff --git a/analytics/management/commands/realm_stats.py b/analytics/management/commands/realm_stats.py index 6707279bf2..16d169467c 100644 --- a/analytics/management/commands/realm_stats.py +++ b/analytics/management/commands/realm_stats.py @@ -1,15 +1,14 @@ +import datetime +from argparse import ArgumentParser from typing import Any, List -from argparse import ArgumentParser -import datetime import pytz - from django.core.management.base import BaseCommand from django.db.models import Count from django.utils.timezone import now as timezone_now -from zerver.models import UserProfile, Realm, Stream, Message, Recipient, UserActivity, \ - Subscription, UserMessage, get_realm +from zerver.models import Message, Realm, Recipient, Stream, \ + Subscription, UserActivity, UserMessage, UserProfile, get_realm MOBILE_CLIENT_LIST = ["Android", "ios"] HUMAN_CLIENT_LIST = MOBILE_CLIENT_LIST + ["website"] diff --git a/analytics/management/commands/stream_stats.py b/analytics/management/commands/stream_stats.py index c6e4b46603..0c39226baf 100644 --- a/analytics/management/commands/stream_stats.py +++ b/analytics/management/commands/stream_stats.py @@ -1,9 +1,11 @@ +from argparse import ArgumentParser from typing import Any -from argparse import ArgumentParser from django.core.management.base import BaseCommand from django.db.models import Q -from zerver.models import Realm, Stream, Message, Subscription, Recipient, get_realm + +from zerver.models import Message, Realm, \ + Recipient, Stream, Subscription, get_realm class Command(BaseCommand): help = "Generate statistics on the streams for a realm." diff --git a/analytics/management/commands/update_analytics_counts.py b/analytics/management/commands/update_analytics_counts.py index 3cbf004899..788e101778 100644 --- a/analytics/management/commands/update_analytics_counts.py +++ b/analytics/management/commands/update_analytics_counts.py @@ -1,21 +1,19 @@ import os -from scripts.lib.zulip_tools import ENDC, WARNING - -from argparse import ArgumentParser import time +from argparse import ArgumentParser +from typing import Any, Dict +from django.conf import settings from django.core.management.base import BaseCommand +from django.utils.dateparse import parse_datetime from django.utils.timezone import now as timezone_now from django.utils.timezone import utc as timezone_utc -from django.utils.dateparse import parse_datetime -from django.conf import settings from analytics.lib.counts import COUNT_STATS, logger, process_count_stat +from scripts.lib.zulip_tools import ENDC, WARNING from zerver.lib.timestamp import floor_to_hour from zerver.models import Realm -from typing import Any, Dict - class Command(BaseCommand): help = """Fills Analytics tables. diff --git a/analytics/management/commands/user_stats.py b/analytics/management/commands/user_stats.py index c473d98f4a..3451636f7f 100644 --- a/analytics/management/commands/user_stats.py +++ b/analytics/management/commands/user_stats.py @@ -1,11 +1,11 @@ -from argparse import ArgumentParser import datetime +from argparse import ArgumentParser from typing import Any from django.core.management.base import BaseCommand from django.utils.timezone import now as timezone_now -from zerver.models import UserProfile, Realm, Stream, Message, get_realm +from zerver.models import Message, Realm, Stream, UserProfile, get_realm class Command(BaseCommand): help = "Generate statistics on user activity." diff --git a/analytics/migrations/0001_initial.py b/analytics/migrations/0001_initial.py index c30b4754d9..e349888bb9 100644 --- a/analytics/migrations/0001_initial.py +++ b/analytics/migrations/0001_initial.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -from django.db import models, migrations import django.db.models.deletion from django.conf import settings -import zerver.lib.str_utils +from django.db import migrations, models +import zerver.lib.str_utils class Migration(migrations.Migration): diff --git a/analytics/migrations/0002_remove_huddlecount.py b/analytics/migrations/0002_remove_huddlecount.py index c38d9ee44e..69796ebf58 100644 --- a/analytics/migrations/0002_remove_huddlecount.py +++ b/analytics/migrations/0002_remove_huddlecount.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0003_fillstate.py b/analytics/migrations/0003_fillstate.py index 5e75edfd9a..618b7ed177 100644 --- a/analytics/migrations/0003_fillstate.py +++ b/analytics/migrations/0003_fillstate.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from django.db import migrations, models -import zerver.lib.str_utils +import zerver.lib.str_utils class Migration(migrations.Migration): diff --git a/analytics/migrations/0004_add_subgroup.py b/analytics/migrations/0004_add_subgroup.py index e16e7733f0..a80c7561d4 100644 --- a/analytics/migrations/0004_add_subgroup.py +++ b/analytics/migrations/0004_add_subgroup.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0005_alter_field_size.py b/analytics/migrations/0005_alter_field_size.py index eed94b35db..7bcf4dccab 100644 --- a/analytics/migrations/0005_alter_field_size.py +++ b/analytics/migrations/0005_alter_field_size.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0006_add_subgroup_to_unique_constraints.py b/analytics/migrations/0006_add_subgroup_to_unique_constraints.py index ecb22060c8..eb41b4c3a5 100644 --- a/analytics/migrations/0006_add_subgroup_to_unique_constraints.py +++ b/analytics/migrations/0006_add_subgroup_to_unique_constraints.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0007_remove_interval.py b/analytics/migrations/0007_remove_interval.py index 0b9562c9bb..6b114e1527 100644 --- a/analytics/migrations/0007_remove_interval.py +++ b/analytics/migrations/0007_remove_interval.py @@ -3,7 +3,6 @@ from django.conf import settings from django.db import migrations - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0008_add_count_indexes.py b/analytics/migrations/0008_add_count_indexes.py index 7d95671840..d91b3a6545 100644 --- a/analytics/migrations/0008_add_count_indexes.py +++ b/analytics/migrations/0008_add_count_indexes.py @@ -2,7 +2,6 @@ # Generated by Django 1.10.5 on 2017-02-01 22:28 from django.db import migrations - class Migration(migrations.Migration): dependencies = [ diff --git a/analytics/migrations/0009_remove_messages_to_stream_stat.py b/analytics/migrations/0009_remove_messages_to_stream_stat.py index 4570ca7ef9..9589096411 100644 --- a/analytics/migrations/0009_remove_messages_to_stream_stat.py +++ b/analytics/migrations/0009_remove_messages_to_stream_stat.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- +from django.db import migrations from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor from django.db.migrations.state import StateApps -from django.db import migrations - def delete_messages_sent_to_stream_stat(apps, schema_editor): # type: (StateApps, DatabaseSchemaEditor) -> None diff --git a/analytics/migrations/0010_clear_messages_sent_values.py b/analytics/migrations/0010_clear_messages_sent_values.py index 307ee2a140..57f3a16afb 100644 --- a/analytics/migrations/0010_clear_messages_sent_values.py +++ b/analytics/migrations/0010_clear_messages_sent_values.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- +from django.db import migrations from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor from django.db.migrations.state import StateApps -from django.db import migrations def clear_message_sent_by_message_type_values(apps, schema_editor): # type: (StateApps, DatabaseSchemaEditor) -> None diff --git a/analytics/migrations/0011_clear_analytics_tables.py b/analytics/migrations/0011_clear_analytics_tables.py index fdcefec177..08a4609208 100644 --- a/analytics/migrations/0011_clear_analytics_tables.py +++ b/analytics/migrations/0011_clear_analytics_tables.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- +from django.db import migrations from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor from django.db.migrations.state import StateApps -from django.db import migrations - def clear_analytics_tables(apps, schema_editor): # type: (StateApps, DatabaseSchemaEditor) -> None diff --git a/analytics/models.py b/analytics/models.py index 00f9fc96b6..f56a33a3f0 100644 --- a/analytics/models.py +++ b/analytics/models.py @@ -1,11 +1,10 @@ +import datetime +from typing import Any, Dict, Optional, Text, Tuple, Union + from django.db import models -from zerver.models import Realm, UserProfile, Stream, Recipient from zerver.lib.timestamp import floor_to_day - -import datetime - -from typing import Optional, Tuple, Union, Dict, Any, Text +from zerver.models import Realm, Recipient, Stream, UserProfile class FillState(models.Model): property = models.CharField(max_length=40, unique=True) # type: Text diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index c37d7faf0a..cd512f996a 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -1,4 +1,8 @@ +from datetime import datetime, timedelta +from typing import Any, Dict, List, Optional, Text, Tuple, Type, Union + +import ujson from django.apps import apps from django.db import models from django.db.models import Sum @@ -6,23 +10,20 @@ from django.test import TestCase from django.utils.timezone import now as timezone_now from django.utils.timezone import utc as timezone_utc -from analytics.lib.counts import CountStat, COUNT_STATS, process_count_stat, \ - do_fill_count_stat_at_hour, do_increment_logging_stat, DataCollector, \ - sql_data_collector, LoggingCountStat, do_aggregate_to_summary_table, \ - do_drop_all_analytics_tables, do_drop_single_stat, DependentCountStat -from analytics.models import BaseCount, InstallationCount, RealmCount, \ - UserCount, StreamCount, FillState, Anomaly, installation_epoch, \ - last_successful_fill -from zerver.lib.actions import do_create_user, do_deactivate_user, \ - do_activate_user, do_reactivate_user, update_user_activity_interval -from zerver.lib.timestamp import floor_to_day, TimezoneNotUTCException -from zerver.models import Realm, UserProfile, Message, Stream, Recipient, \ - Huddle, Client, UserActivityInterval, RealmAuditLog, get_client, get_user - -from datetime import datetime, timedelta -import ujson - -from typing import Any, Dict, List, Optional, Text, Tuple, Type, Union +from analytics.lib.counts import COUNT_STATS, CountStat, DataCollector, \ + DependentCountStat, LoggingCountStat, do_aggregate_to_summary_table, \ + do_drop_all_analytics_tables, do_drop_single_stat, \ + do_fill_count_stat_at_hour, do_increment_logging_stat, \ + process_count_stat, sql_data_collector +from analytics.models import Anomaly, BaseCount, \ + FillState, InstallationCount, RealmCount, StreamCount, \ + UserCount, installation_epoch, last_successful_fill +from zerver.lib.actions import do_activate_user, do_create_user, \ + do_deactivate_user, do_reactivate_user, update_user_activity_interval +from zerver.lib.timestamp import TimezoneNotUTCException, floor_to_day +from zerver.models import Client, Huddle, Message, Realm, \ + RealmAuditLog, Recipient, Stream, UserActivityInterval, \ + UserProfile, get_client, get_user class AnalyticsTestCase(TestCase): MINUTE = timedelta(seconds = 60) diff --git a/analytics/tests/test_fixtures.py b/analytics/tests/test_fixtures.py index ca43afa7de..69bfd06223 100644 --- a/analytics/tests/test_fixtures.py +++ b/analytics/tests/test_fixtures.py @@ -1,7 +1,6 @@ -from zerver.lib.test_classes import ZulipTestCase - from analytics.lib.counts import CountStat from analytics.lib.fixtures import generate_time_series_data +from zerver.lib.test_classes import ZulipTestCase # A very light test suite; the code being tested is not run in production. class TestFixtures(ZulipTestCase): diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index 99e03c5f2a..a52d79500d 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -1,20 +1,19 @@ -from django.utils.timezone import utc -from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.timestamp import ceiling_to_hour, ceiling_to_day, \ - datetime_to_timestamp -from zerver.models import Client, get_realm - -from analytics.lib.counts import CountStat, COUNT_STATS -from analytics.lib.time_utils import time_range -from analytics.models import RealmCount, UserCount, \ - FillState, last_successful_fill -from analytics.views import stats, get_chart_data, sort_by_totals, \ - sort_client_labels, rewrite_client_arrays - from datetime import datetime, timedelta -import mock +from typing import Dict, List, Optional -from typing import List, Dict, Optional +import mock +from django.utils.timezone import utc + +from analytics.lib.counts import COUNT_STATS, CountStat +from analytics.lib.time_utils import time_range +from analytics.models import FillState, \ + RealmCount, UserCount, last_successful_fill +from analytics.views import get_chart_data, rewrite_client_arrays, \ + sort_by_totals, sort_client_labels, stats +from zerver.lib.test_classes import ZulipTestCase +from zerver.lib.timestamp import ceiling_to_day, \ + ceiling_to_hour, datetime_to_timestamp +from zerver.models import Client, get_realm class TestStatsEndpoint(ZulipTestCase): def test_stats(self): diff --git a/analytics/urls.py b/analytics/urls.py index 24a3492ab0..b42d44686a 100644 --- a/analytics/urls.py +++ b/analytics/urls.py @@ -1,7 +1,7 @@ -from django.conf.urls import url, include -from zerver.lib.rest import rest_dispatch +from django.conf.urls import include, url import analytics.views +from zerver.lib.rest import rest_dispatch i18n_urlpatterns = [ # Server admin (user_profile.is_staff) visible stats pages diff --git a/analytics/views.py b/analytics/views.py index a3510c3096..f7bda31797 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -1,42 +1,40 @@ +import itertools +import json +import logging +import re +import time +from collections import defaultdict +from datetime import datetime, timedelta +from typing import Any, Callable, Dict, List, \ + Optional, Set, Text, Tuple, Type, Union + +import pytz from django.conf import settings from django.core import urlresolvers from django.db import connection from django.db.models import Sum from django.db.models.query import QuerySet -from django.http import HttpResponseNotFound, HttpRequest, HttpResponse +from django.http import HttpRequest, HttpResponse, HttpResponseNotFound +from django.shortcuts import render from django.template import RequestContext, loader from django.utils.timezone import now as timezone_now from django.utils.translation import ugettext as _ -from django.shortcuts import render from jinja2 import Markup as mark_safe -from analytics.lib.counts import CountStat, process_count_stat, COUNT_STATS +from analytics.lib.counts import COUNT_STATS, CountStat, process_count_stat from analytics.lib.time_utils import time_range -from analytics.models import BaseCount, InstallationCount, RealmCount, \ - UserCount, StreamCount, last_successful_fill - -from zerver.decorator import require_server_admin, zulip_login_required, \ - to_non_negative_int, to_utc_datetime +from analytics.models import BaseCount, InstallationCount, \ + RealmCount, StreamCount, UserCount, last_successful_fill +from zerver.decorator import require_server_admin, \ + to_non_negative_int, to_utc_datetime, zulip_login_required from zerver.lib.exceptions import JsonableError -from zerver.lib.request import has_request_variables, REQ +from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success -from zerver.lib.timestamp import ceiling_to_hour, ceiling_to_day, \ - timestamp_to_datetime, convert_to_UTC -from zerver.models import Realm, UserProfile, UserActivity, \ - UserActivityInterval, Client - -from collections import defaultdict -from datetime import datetime, timedelta -import itertools -import json -import logging -import pytz -import re -import time - -from typing import Any, Callable, Dict, List, Optional, Set, Text, \ - Tuple, Type, Union +from zerver.lib.timestamp import ceiling_to_day, \ + ceiling_to_hour, convert_to_UTC, timestamp_to_datetime +from zerver.models import Client, Realm, \ + UserActivity, UserActivityInterval, UserProfile @zulip_login_required def stats(request): diff --git a/zilencer/management/commands/add_mock_conversation.py b/zilencer/management/commands/add_mock_conversation.py index 94a20a7b7d..24cff6bae4 100644 --- a/zilencer/management/commands/add_mock_conversation.py +++ b/zilencer/management/commands/add_mock_conversation.py @@ -1,14 +1,15 @@ # -*- coding: utf-8 -*- -from django.core.management.base import BaseCommand -from zerver.lib.actions import create_stream_if_needed, \ - internal_prep_stream_message, do_create_user, do_send_messages, \ - do_add_reaction_legacy, bulk_add_subscriptions, do_change_avatar_fields -from zerver.lib.upload import upload_avatar_image -from zerver.models import get_realm, UserProfile, Message - from typing import Any, Dict, List +from django.core.management.base import BaseCommand + +from zerver.lib.actions import bulk_add_subscriptions, \ + create_stream_if_needed, do_add_reaction_legacy, do_change_avatar_fields, \ + do_create_user, do_send_messages, internal_prep_stream_message +from zerver.lib.upload import upload_avatar_image +from zerver.models import Message, UserProfile, get_realm + class Command(BaseCommand): help = """Add a mock conversation to the development environment. diff --git a/zilencer/management/commands/add_new_realm.py b/zilencer/management/commands/add_new_realm.py index c2eace8a58..ab87cf0e6d 100644 --- a/zilencer/management/commands/add_new_realm.py +++ b/zilencer/management/commands/add_new_realm.py @@ -1,11 +1,11 @@ -from zerver.lib.actions import do_create_user, do_create_realm -from zerver.lib.management import ZulipBaseCommand -from zerver.lib.onboarding import setup_initial_streams, \ - setup_initial_private_stream, send_initial_realm_messages -from zerver.models import Realm, UserProfile - from typing import Any +from zerver.lib.actions import do_create_realm, do_create_user +from zerver.lib.management import ZulipBaseCommand +from zerver.lib.onboarding import send_initial_realm_messages, \ + setup_initial_private_stream, setup_initial_streams +from zerver.models import Realm, UserProfile + class Command(ZulipBaseCommand): help = """Add a new realm and initial user for manual testing of the onboarding process.""" diff --git a/zilencer/management/commands/add_new_user.py b/zilencer/management/commands/add_new_user.py index 1c0c3686a5..70a5820e3c 100644 --- a/zilencer/management/commands/add_new_user.py +++ b/zilencer/management/commands/add_new_user.py @@ -1,10 +1,11 @@ +from typing import Any + from django.core.management.base import CommandParser + from zerver.lib.actions import do_create_user from zerver.lib.management import ZulipBaseCommand from zerver.models import Realm, UserProfile -from typing import Any - class Command(ZulipBaseCommand): help = """Add a new user for manual testing of the onboarding process. If realm is unspecified, will try to use a realm created by add_new_realm, diff --git a/zilencer/management/commands/compare_messages.py b/zilencer/management/commands/compare_messages.py index 9e4d73ae0f..5d377b5483 100644 --- a/zilencer/management/commands/compare_messages.py +++ b/zilencer/management/commands/compare_messages.py @@ -1,10 +1,8 @@ from typing import Any import ijson - from django.core.management.base import BaseCommand, CommandParser - class Command(BaseCommand): help = """ Render messages to a file. diff --git a/zilencer/management/commands/mark_all_messages_unread.py b/zilencer/management/commands/mark_all_messages_unread.py index e2f087d9d8..fbb0c542fe 100644 --- a/zilencer/management/commands/mark_all_messages_unread.py +++ b/zilencer/management/commands/mark_all_messages_unread.py @@ -2,10 +2,11 @@ from typing import Any from django.conf import settings +from django.core.cache import cache from django.core.management.base import BaseCommand from django.db.models import F -from django.core.cache import cache -from zerver.models import UserProfile, UserMessage + +from zerver.models import UserMessage, UserProfile class Command(BaseCommand): help = """Script to mark all messages as unread.""" diff --git a/zilencer/management/commands/migrate_stream_notifications.py b/zilencer/management/commands/migrate_stream_notifications.py index 2777557691..29308d30b8 100644 --- a/zilencer/management/commands/migrate_stream_notifications.py +++ b/zilencer/management/commands/migrate_stream_notifications.py @@ -2,6 +2,7 @@ from typing import Any from django.core.management.base import BaseCommand + from zerver.models import Subscription class Command(BaseCommand): diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 9fa8376afc..0b47ae2e0b 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -1,27 +1,26 @@ -from django.core.management.base import BaseCommand, CommandParser -from django.utils.timezone import now as timezone_now -from django.db.models import F, Max +import itertools +import os +import random +from typing import Any, Callable, Dict, Iterable, List, \ + Mapping, Optional, Sequence, Set, Text, Tuple -from zerver.models import Message, UserProfile, Stream, Recipient, UserPresence, \ - Subscription, RealmAuditLog, get_huddle, Realm, RealmEmoji, UserMessage, \ - RealmDomain, clear_database, get_client, get_user_profile_by_id, \ - email_to_username, Service, get_user, DefaultStream, get_stream, \ - get_realm, get_system_bot - -from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS, do_send_messages, \ - do_change_is_admin +import ujson from django.conf import settings +from django.core.management.base import BaseCommand, CommandParser +from django.db.models import F, Max +from django.utils.timezone import now as timezone_now + +from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS, \ + do_change_is_admin, do_send_messages from zerver.lib.bulk_create import bulk_create_streams, bulk_create_users from zerver.lib.generate_test_data import create_test_data from zerver.lib.upload import upload_backend from zerver.lib.user_groups import create_user_group - - -import random -import os -import ujson -import itertools -from typing import Any, Callable, Dict, List, Iterable, Mapping, Optional, Sequence, Set, Tuple, Text +from zerver.models import DefaultStream, Message, Realm, RealmAuditLog, \ + RealmDomain, RealmEmoji, Recipient, Service, Stream, Subscription, \ + UserMessage, UserPresence, UserProfile, clear_database, \ + email_to_username, get_client, get_huddle, get_realm, get_stream, \ + get_system_bot, get_user, get_user_profile_by_id settings.TORNADO_SERVER = None # Disable using memcached caches to avoid 'unsupported pickle diff --git a/zilencer/management/commands/print_initial_password.py b/zilencer/management/commands/print_initial_password.py index 22673bc710..deded48d20 100644 --- a/zilencer/management/commands/print_initial_password.py +++ b/zilencer/management/commands/print_initial_password.py @@ -1,6 +1,6 @@ +from argparse import ArgumentParser from typing import Any -from argparse import ArgumentParser from zerver.lib.initial_password import initial_password from zerver.lib.management import ZulipBaseCommand diff --git a/zilencer/management/commands/profile_request.py b/zilencer/management/commands/profile_request.py index 04acc04f97..28aab92e20 100644 --- a/zilencer/management/commands/profile_request.py +++ b/zilencer/management/commands/profile_request.py @@ -1,13 +1,14 @@ -from typing import Any, Dict - -from django.http import HttpRequest, HttpResponse -from django.core.management.base import CommandParser -from zerver.lib.management import ZulipBaseCommand -from zerver.models import UserMessage, UserProfile -from zerver.views.messages import get_messages_backend import cProfile import logging +from typing import Any, Dict + +from django.core.management.base import CommandParser +from django.http import HttpRequest, HttpResponse + +from zerver.lib.management import ZulipBaseCommand from zerver.middleware import LogRequests +from zerver.models import UserMessage, UserProfile +from zerver.views.messages import get_messages_backend request_logger = LogRequests() diff --git a/zilencer/management/commands/render_messages.py b/zilencer/management/commands/render_messages.py index bde4750dab..8acaec6a5d 100644 --- a/zilencer/management/commands/render_messages.py +++ b/zilencer/management/commands/render_messages.py @@ -1,14 +1,13 @@ import os -import ujson from typing import Any, Iterator +import ujson from django.core.management.base import BaseCommand, CommandParser from django.db.models import QuerySet from zerver.lib.message import render_markdown from zerver.models import Message - def queryset_iterator(queryset, chunksize=5000): # type: (QuerySet, int) -> Iterator[Any] queryset = queryset.order_by('id') diff --git a/zilencer/management/commands/sync_api_key.py b/zilencer/management/commands/sync_api_key.py index f8b71e38f4..8a9b0ab3b3 100644 --- a/zilencer/management/commands/sync_api_key.py +++ b/zilencer/management/commands/sync_api_key.py @@ -1,9 +1,10 @@ +import os +from configparser import ConfigParser from typing import Any from django.core.management.base import BaseCommand -from zerver.models import get_realm, get_user, UserProfile -import os -from configparser import ConfigParser + +from zerver.models import UserProfile, get_realm, get_user class Command(BaseCommand): help = """Sync your API key from ~/.zuliprc into your development instance""" diff --git a/zilencer/migrations/0001_initial.py b/zilencer/migrations/0001_initial.py index d1b8229433..120ceffc8e 100644 --- a/zilencer/migrations/0001_initial.py +++ b/zilencer/migrations/0001_initial.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -from django.db import models, migrations - +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/zilencer/migrations/0002_remote_zulip_server.py b/zilencer/migrations/0002_remote_zulip_server.py index 3302182512..3d83a22a22 100644 --- a/zilencer/migrations/0002_remote_zulip_server.py +++ b/zilencer/migrations/0002_remote_zulip_server.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -from django.db import migrations, models import django.db.models.deletion - +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/zilencer/migrations/0003_add_default_for_remotezulipserver_last_updated_field.py b/zilencer/migrations/0003_add_default_for_remotezulipserver_last_updated_field.py index 8d96d6bab8..f38e94e847 100644 --- a/zilencer/migrations/0003_add_default_for_remotezulipserver_last_updated_field.py +++ b/zilencer/migrations/0003_add_default_for_remotezulipserver_last_updated_field.py @@ -2,7 +2,6 @@ # Generated by Django 1.10.5 on 2017-05-16 00:03 from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/zilencer/migrations/0004_remove_deployment_model.py b/zilencer/migrations/0004_remove_deployment_model.py index 90c5bccf89..4c14e92503 100644 --- a/zilencer/migrations/0004_remove_deployment_model.py +++ b/zilencer/migrations/0004_remove_deployment_model.py @@ -2,7 +2,6 @@ # Generated by Django 1.11.5 on 2017-10-12 06:27 from django.db import migrations - class Migration(migrations.Migration): dependencies = [ diff --git a/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py b/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py index 9a4051ee0d..3a4c71e9c5 100644 --- a/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py +++ b/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py @@ -4,7 +4,6 @@ from __future__ import unicode_literals from django.db import migrations, models - class Migration(migrations.Migration): dependencies = [ diff --git a/zilencer/models.py b/zilencer/models.py index 0e2e182251..9d87820782 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -1,8 +1,9 @@ -from django.db import models +import datetime from typing import Text +from django.db import models + import zerver.models -import datetime def get_remote_server_by_uuid(uuid): # type: (Text) -> RemoteZulipServer diff --git a/zilencer/urls.py b/zilencer/urls.py index 06804a80d2..b69b1af6c7 100644 --- a/zilencer/urls.py +++ b/zilencer/urls.py @@ -1,9 +1,9 @@ -from django.conf.urls import url, include from typing import Any -from zerver.lib.rest import rest_dispatch +from django.conf.urls import include, url import zilencer.views +from zerver.lib.rest import rest_dispatch i18n_urlpatterns = [] # type: Any diff --git a/zilencer/views.py b/zilencer/views.py index 6f3347dc4b..6e9ffff25a 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -1,20 +1,19 @@ -from django.utils.translation import ugettext as _ +from typing import Any, Dict, Optional, Text, Union, cast + +from django.http import HttpRequest, HttpResponse from django.utils import timezone -from django.http import HttpResponse, HttpRequest - -from zilencer.models import RemotePushDeviceToken, RemoteZulipServer +from django.utils.translation import ugettext as _ from zerver.lib.exceptions import JsonableError from zerver.lib.push_notifications import send_android_push_notification, \ send_apple_push_notification +from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_error, json_success -from zerver.lib.request import has_request_variables, REQ from zerver.lib.validator import check_int from zerver.models import UserProfile from zerver.views.push_notifications import validate_token - -from typing import Any, Dict, Optional, Union, Text, cast +from zilencer.models import RemotePushDeviceToken, RemoteZulipServer def validate_entity(entity): # type: (Union[UserProfile, RemoteZulipServer]) -> None