Skip to content

Commit

Permalink
JSON конвертирование даты. Исключение на дублей игроков.
Browse files Browse the repository at this point in the history
  • Loading branch information
maryykmv committed Dec 19, 2023
1 parent 1dde183 commit 53a0751
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
51 changes: 50 additions & 1 deletion adaptive_hockey_federation/core/management/commands/fill-db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import json
import sqlite3
import subprocess

from django.core.management.base import BaseCommand

from adaptive_hockey_federation.core.config.dev_settings import RESOURSES_ROOT
from adaptive_hockey_federation.core.config.dev_settings import (
BASE_DIR,
FIXSTURES_DIR,
RESOURSES_ROOT,
)


class Command(BaseCommand):
Expand All @@ -19,3 +25,46 @@ def handle(self, *args, **options):
) as file:
print(run_parser)
file.write(run_parser)

conn = sqlite3.connect(BASE_DIR / 'db.sqlite3')
cursor = conn.cursor()
data = json.load(open(FIXSTURES_DIR / 'data.json'))
for item in data:
for key in item:
if item[key] is None:
item[key] = ''
try:
cursor.execute('''
INSERT INTO main_player (
surname,
name,
patronymic,
birthday,
gender,
level_revision,
position,
number,
is_captain,
is_assistent,
identity_document
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
item['surname'],
item['name'],
item['patronymic'],
item['date_of_birth'],
'',
# gender,
item['revision'],
item['position'],
item['player_number'],
item['is_captain'],
item['is_assistant'],
item['passport']
)
)
except Exception as e:
print(e)
print(item)
conn.commit()
11 changes: 5 additions & 6 deletions adaptive_hockey_federation/parser/docx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ def docx_parser(
names = find_names(columns_from_file, NAME)
surnames = find_surnames(columns_from_file, SURNAME)
patronymics = find_patronymics(columns_from_file, PATRONYMIC)
# dates_of_birth = find_dates_of_birth(
# columns_from_file,
# DATE_OF_BIRTH,
# )
dates_of_birth = find_dates_of_birth(
columns_from_file,
DATE_OF_BIRTH,
)
team = find_team(text_from_file, columns_from_file, TEAM)
players_number = find_players_number(columns_from_file, PLAYER_NUMBER)
positions = find_positions(columns_from_file, POSITION)
Expand All @@ -336,8 +336,7 @@ def docx_parser(
BaseUserInfo(
name=names[index],
surname=surnames[index],
date_of_birth=None,
# dates_of_birth[index],
date_of_birth=dates_of_birth[index].isoformat(),
team=team,
player_number=players_number[index],
position=positions[index],
Expand Down
48 changes: 2 additions & 46 deletions adaptive_hockey_federation/parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import json
import os
import sqlite3
from datetime import datetime
from pprint import pprint

import click
import docx # type: ignore

from adaptive_hockey_federation.core.config.dev_settings import (
BASE_DIR,
FIXSTURES_DIR,
)
from adaptive_hockey_federation.core.config.dev_settings import FIXSTURES_DIR
from adaptive_hockey_federation.parser.docx_parser import (
docx_parser,
find_numeric_statuses,
Expand Down Expand Up @@ -72,47 +67,8 @@ def parsing_file(path: str, result: bool) -> None:
pprint(data)

with open(FIXSTURES_DIR / 'data.json', 'w', encoding='utf8') as f:
json.dump(results_list, f, ensure_ascii=False, indent=4)

conn = sqlite3.connect(BASE_DIR / 'db.sqlite3')
cursor = conn.cursor()
data = json.load(open(FIXSTURES_DIR / 'data.json'))
for item in data:
for key in item:
if item[key] is None:
item[key] = ''
cursor.execute('''
INSERT INTO main_player (
surname,
name,
patronymic,
birthday,
gender,
level_revision,
position,
number,
is_captain,
is_assistent,
identity_document
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
item['surname'],
item['name'],
item['patronymic'],
datetime.today(),
'',
# gender,
item['revision'],
item['position'],
item['player_number'],
item['is_captain'],
item['is_assistant'],
item['passport']
)
)
json.dump(results_list, f, ensure_ascii=False, indent=4, default=str)

conn.commit()
results_list = list(results_list)

click.echo(f'Успешно обработано {len(files)} файлов.')
Expand Down
3 changes: 1 addition & 2 deletions adaptive_hockey_federation/parser/xlsx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def xlsx_parser(path: str) -> list[BaseUserInfo]:
team=data.get('Команда'),
name=data.get('ФИО игрока').split()[0], # type: ignore
surname=data.get('ФИО игрока').split()[1], # type: ignore
date_of_birth=None,
# data.get('Дата рождения'),
date_of_birth=data.get('Дата рождения'),
player_number=data.get('Номер игрока'),
position=data.get('Позиция'),
classification=data.get('Класс'),
Expand Down

0 comments on commit 53a0751

Please sign in to comment.