Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uprev to new pydantic and black formatting #275

Merged
merged 4 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
install:
pip install -r requirements.txt

.PHONY: format
format:
isort -rc -w 120 tcsocket
isort -rc -w 120 tests
black -S -l 120 --target-version py38 tcsocket tests

.PHONY: isort
isort:
isort -rc -w 120 tcsocket
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
socket-server
=============

[![Build Status](https://travis-ci.org/tutorcruncher/socket-server.svg?branch=master)](https://travis-ci.org/tutorcruncher/socket-server)
[![Build Status](https://github.com/tutorcruncher/socket-server/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/tutorcruncher/socket-server/branch/master/graph/badge.svg)](https://codecov.io/gh/tutorcruncher/socket-server)

Backend application for [TutorCruncher's](https://tutorcruncher.com) web integration.
Expand Down
10 changes: 8 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ addopts = --isort --aiohttp-loop uvloop --aiohttp-fast --tb=native
[flake8]
max-line-length = 120
max-complexity = 12
# remove E252 once https://github.com/PyCQA/pycodestyle/issues/753 is fixed
ignore = W504, E252
ignore = E203, W503, W504

[coverage:run]
source = tcsocket
Expand All @@ -23,3 +22,10 @@ exclude_lines =
raise AssertionError
raise NotImplementedError
raise NotImplemented

[isort]
line_length = 120
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
combine_as_imports=True
15 changes: 11 additions & 4 deletions tcsocket/app/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ async def geocode(request):
loc_data = await redis.get(loc_key)
if loc_data:
result = json.loads(loc_data)
logger.info('cached geocode result "%s|%s" > "%s"', location_str, region,
result.get('error') or result['pretty'])
logger.info(
'cached geocode result "%s|%s" > "%s"', location_str, region, result.get('error') or result['pretty']
)
return result

ip_key = 'geoip:' + ip_address
Expand Down Expand Up @@ -76,6 +77,12 @@ async def geocode(request):
else:
result = {'error': 'no_results'}
await redis.setex(loc_key, NINETY_DAYS, json.dumps(result).encode())
logger.info('new geocode result "%s|%s" > "%s" (%d from "%s")',
location_str, region, result.get('error') or result['pretty'], geo_attempts, ip_address)
logger.info(
'new geocode result "%s|%s" > "%s" (%d from "%s")',
location_str,
region,
result.get('error') or result['pretty'],
geo_attempts,
ip_address,
)
return result
31 changes: 7 additions & 24 deletions tcsocket/app/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os


def setup_logging(verbose: bool=False):
def setup_logging(verbose: bool = False):
"""
setup logging config for socket by updating the arq logging config
"""
Expand All @@ -15,38 +15,21 @@ def setup_logging(verbose: bool=False):
config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'socket.default': {
'format': '%(levelname)s %(name)s %(message)s',
},
},
'formatters': {'socket.default': {'format': '%(levelname)s %(name)s %(message)s'}},
'handlers': {
'socket.default': {
'level': log_level,
'class': 'logging.StreamHandler',
'formatter': 'socket.default',
},
'socket.default': {'level': log_level, 'class': 'logging.StreamHandler', 'formatter': 'socket.default'},
'sentry': {
'level': 'WARNING',
'class': 'raven.handlers.logging.SentryHandler',
'dsn': raven_dsn,
'release': os.getenv('COMMIT', None),
'name': os.getenv('SERVER_NAME', '-')
'name': os.getenv('SERVER_NAME', '-'),
},
},
'loggers': {
'socket': {
'handlers': ['socket.default', 'sentry'],
'level': log_level,
},
'gunicorn.error': {
'handlers': ['sentry'],
'level': 'ERROR',
},
'arq': {
'handlers': ['socket.default', 'sentry'],
'level': log_level,
},
'socket': {'handlers': ['socket.default', 'sentry'], 'level': log_level},
'gunicorn.error': {'handlers': ['sentry'], 'level': 'ERROR'},
'arq': {'handlers': ['socket.default', 'sentry'], 'level': log_level},
},
}
logging.config.dictConfig(config)
21 changes: 13 additions & 8 deletions tcsocket/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
from .middleware import middleware
from .settings import THIS_DIR, Settings
from .views import favicon, index, labels_list, qual_level_list, robots_txt, subject_list
from .views.appointments import (appointment_list, appointment_webhook, appointment_webhook_delete, book_appointment,
check_client, service_list)
from .views.appointments import (
appointment_list,
appointment_webhook,
appointment_webhook_delete,
book_appointment,
check_client,
service_list,
)
from .views.company import company_create, company_list, company_options, company_update
from .views.contractor import contractor_get, contractor_list, contractor_set
from .views.enquiry import clear_enquiry, enquiry
Expand All @@ -20,9 +26,7 @@ async def startup(app: web.Application):
settings: Settings = app['settings']
redis = await create_pool(settings.redis_settings)
app.update(
pg_engine=await create_engine(settings.pg_dsn),
redis=redis,
session=ClientSession(),
pg_engine=await create_engine(settings.pg_dsn), redis=redis, session=ClientSession(),
)


Expand All @@ -48,8 +52,9 @@ def setup_routes(app):
app.router.add_post(r'/{company}/webhook/contractor', contractor_set, name='webhook-contractor')
app.router.add_post(r'/{company}/webhook/clear-enquiry', clear_enquiry, name='webhook-clear-enquiry')
app.router.add_post(r'/{company}/webhook/appointments/{id:\d+}', appointment_webhook, name='webhook-appointment')
app.router.add_delete(r'/{company}/webhook/appointments/{id:\d+}', appointment_webhook_delete,
name='webhook-appointment-delete')
app.router.add_delete(
r'/{company}/webhook/appointments/{id:\d+}', appointment_webhook_delete, name='webhook-appointment-delete'
)

app.router.add_get(r'/{company}/contractors', contractor_list, name='contractor-list')
app.router.add_get(r'/{company}/contractors/{id:\d+}', contractor_get, name='contractor-get')
Expand All @@ -64,7 +69,7 @@ def setup_routes(app):
app.router.add_post(r'/{company}/book-appointment', book_appointment, name='book-appointment')


def create_app(loop, *, settings: Settings=None):
def create_app(loop, *, settings: Settings = None):
app = web.Application(middlewares=middleware)
settings = settings or Settings()
app['settings'] = settings
Expand Down
51 changes: 25 additions & 26 deletions tcsocket/app/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@

def lenient_connection(settings: Settings, retries=5):
try:
return psycopg2.connect(
password=settings.pg_password,
host=settings.pg_host,
port=settings.pg_port,
user=settings.pg_user,
)
return psycopg2.connect(password=settings.pg_password, dsn=settings.pg_dsn,)
except psycopg2.Error as e:
if retries <= 0:
raise
Expand Down Expand Up @@ -73,14 +68,14 @@ def populate_db(engine):
"""


def prepare_database(delete_existing: Union[bool, callable]) -> bool:
def prepare_database(delete_existing: Union[bool, callable], settings: Settings = None) -> bool:
"""
(Re)create a fresh database and run migrations.

:param delete_existing: whether or not to drop an existing database if it exists
:return: whether or not a database as (re)created
"""
settings = Settings()
settings = settings or Settings()

with psycopg2_cursor(settings) as cur:
cur.execute('SELECT EXISTS (SELECT datname FROM pg_catalog.pg_database WHERE datname=%s)', (settings.pg_name,))
Expand All @@ -96,13 +91,12 @@ def prepare_database(delete_existing: Union[bool, callable]) -> bool:
else:
print(f'dropping existing connections to "{settings.pg_name}"...')
cur.execute(DROP_CONNECTIONS, (settings.pg_name,))
print(f'dropping database "{settings.pg_name}" as it already exists...')
cur.execute(f'DROP DATABASE {settings.pg_name}')
else:
print(f'database "{settings.pg_name}" does not yet exist')

print(f'creating database "{settings.pg_name}"...')
cur.execute(f'CREATE DATABASE {settings.pg_name}')
logger.debug('dropping and re-creating the schema...')
cur.execute('drop schema public cascade;\ncreate schema public;')
else:
print(f'database "{settings.pg_name}" does not yet exist, creating')
cur.execute(f'CREATE DATABASE {settings.pg_name}')

engine = create_engine(settings.pg_dsn)
print('creating tables from model definition...')
Expand All @@ -122,9 +116,11 @@ def patch(func):

def run_patch(live, patch_name):
if patch_name is None:
print('available patches:\n{}'.format(
'\n'.join(' {}: {}'.format(p.__name__, p.__doc__.strip('\n ')) for p in patches)
))
print(
'available patches:\n{}'.format(
'\n'.join(' {}: {}'.format(p.__name__, p.__doc__.strip('\n ')) for p in patches)
)
)
return
patch_lookup = {p.__name__: p for p in patches}
try:
Expand Down Expand Up @@ -168,8 +164,11 @@ def print_tables(conn):
'float8': 'FLOAT',
}
for table_name, *_ in result:
r = conn.execute("SELECT column_name, udt_name, character_maximum_length, is_nullable, column_default "
"FROM information_schema.columns WHERE table_name=%s", table_name)
r = conn.execute(
"SELECT column_name, udt_name, character_maximum_length, is_nullable, column_default "
"FROM information_schema.columns WHERE table_name=%s",
table_name,
)
fields = []
for name, col_type, max_chars, nullable, dft in r:
col_type = type_lookup.get(col_type, col_type.upper())
Expand Down Expand Up @@ -204,11 +203,13 @@ def add_labels(conn):
add labels field to contractors
"""
conn.execute('ALTER TABLE contractors ADD labels VARCHAR(255)[]')
conn.execute("""
conn.execute(
"""
CREATE INDEX ix_contractors_labels
ON contractors
USING btree (labels);
""")
"""
)


@patch
Expand All @@ -220,11 +221,9 @@ def add_domains_options(conn):
conn.execute('ALTER TABLE companies ADD options JSONB')
updated = 0
for id, domain in conn.execute('SELECT id, domain FROM companies WHERE domain IS NOT NULL'):
conn.execute((
update(sa_companies)
.values({'domains': [domain, 'www.' + domain]})
.where(sa_companies.c.id == id)
))
conn.execute(
(update(sa_companies).values({'domains': [domain, 'www.' + domain]}).where(sa_companies.c.id == id))
)
updated += 1
print(f'domains updated for {updated} companies')
conn.execute('ALTER TABLE companies DROP COLUMN domain')
Expand Down
Loading