import os
from pathlib import Path
from os.path import join
from datetime import timedelta
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# SECURITY WARNING: keep the secret key used in production secret!
if DEBUG:
SECRET_KEY = '***'
else:
SECRET_KEY = os.environ.get('SECRET_KEY', None)
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My Apps
'locaAPI.apps.LocaApiConfig',
'accounts.apps.AccountsConfig',
'tags.apps.TagsConfig',
'businessCard.apps.BusinessCardConfig',
'experience.apps.ExperienceConfig',
# Needed libraries
'pandas',
'rest_framework',
'URLSearchParams',
'openpyxl',
'requests',
'django_filters',
'random_username',
'storages',
'rest_framework_simplejwt',
'boto3',
]
AUTHENTICATION_BACKENDS = (
# ... your other backends
'accounts.auth_backend.MyAuth',
'django.contrib.auth.backends.ModelBackend',
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
else:
# Digital Ocean PostgreSQL Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': os.environ.get("DJANGO_DB_PASSWORD", None),
'HOST': '***,
'PORT': '25060',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
AUTH_USER_MODEL = 'accounts.Account'
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
@api_view([HTTPMethod.post])
@permission_classes([IsAuthenticated])
def check(request):
try:
head = int(request.data['head'])
except KeyError:
head = -1
try:
city = request.data['city']
except KeyError:
city = 'all'
print(head, city)
delete_existing_data()
populate_tags()
populate_subTags()
populate_cities()
populate_business_main(head=head, city=city)
return Response({'success': True})
import pandas as pd
from config.settings import BASE_DIR
from businessCard.models import BusinessCard, City, WorkSchedule
from experience.models import Experience
from tags.models import Tag, SubTag
from config.static import TAGS
df = pd.read_excel(BASE_DIR / "files" / "firms_221597_19.05.2022.xlsx", sheet_name='firms_221597_19.05.2022')
df_category = pd.read_excel(BASE_DIR / "files" / "category.xlsx", sheet_name='Лист2')
def populate_tags():
for tag_name in TAGS:
tag = Tag(name=tag_name)
tag.save()
def populate_subTags():
housing = Tag.objects.get(name__exact=TAGS[0])
food = Tag.objects.get(name__exact=TAGS[1])
entertain = Tag.objects.get(name__exact=TAGS[2])
housingSubTags = df_category[TAGS[0]].dropna()
foodSubTags = df_category[TAGS[1]].dropna()
entertainSubTags = df_category[TAGS[2]].dropna()
for subTag_name in housingSubTags:
subTag = SubTag(name=subTag_name)
subTag.tag = housing
subTag.save()
for subTag_name in foodSubTags:
subTag = SubTag(name=subTag_name)
subTag.tag = food
subTag.save()
for subTag_name in entertainSubTags:
subTag = SubTag(name=subTag_name)
subTag.tag = entertain
subTag.save()
def delete_all_existing():
if SubTag.objects.exists():
SubTag.objects.all().delete()
if Tag.objects.exists():
Tag.objects.all().delete()
if City.objects.exists():
City.objects.all().delete()
if WorkSchedule.objects.exists():
WorkSchedule.objects.all().delete()
if BusinessCard.objects.exists():
BusinessCard.objects.all().delete()
if Experience.objects.exists():
Experience.objects.delete()
def delete_existing_data():
if SubTag.objects.exists():
SubTag.objects.all().delete()
if Tag.objects.exists():
Tag.objects.all().delete()
if City.objects.exists():
City.objects.all().delete()
if BusinessCard.objects.exists():
BusinessCard.objects.all().delete()
if WorkSchedule.objects.exists():
WorkSchedule.objects.all().delete()
def get_subTags(strSubTags):
subTags = []
subTags = strSubTags.split('|')
return subTags
def populate_work_schedule(data: str, business: BusinessCard):
try:
days = data.split('|')
for day in days:
content = day.split(": ")
exact_day = content[0]
if content[1] == '-':
start_time = 'off'
end_time = 'off'
else:
time = content[1].split(' до ')
start_time = time[0][2:]
end_time = time[1]
work_schedule = WorkSchedule()
work_schedule.day_of_week = exact_day
work_schedule.start_time = start_time
work_schedule.end_time = end_time
work_schedule.business_card = business
work_schedule.save()
except AttributeError:
pass
def populate_cities():
city_names = df['Город'].unique()
for city_name in city_names:
city = City(name=city_name)
city.save()
def populate_business_main(city: str, head: int):
df_pop = pd.read_excel(BASE_DIR / "files" / "firms_221597_19.05.2022.xlsx", sheet_name='firms_221597_19.05.2022')
if city == 'all':
populate_businesses_by_head(df_pop, head=head)
else:
populate_businesses_by_city(df_pop, city=city, head=head)
def populate_businesses_by_city(df_pop, city: str, head: int):
df_city = df_pop.loc[(df['Город'] == city)]
print(city)
populate_businesses_by_head(df_city, head=head)
def populate_businesses_by_head(df_pop, head: int):
if head == -1:
populate_businesses_all(df_pop)
else:
for index, row in df_pop.iterrows():
if head > 0:
populate_business(row)
head -= 1
else:
break
def populate_businesses_all(df_pop):
for index, row in df_pop.iterrows():
populate_business(row)
def populate_business(row):
businessCard = BusinessCard()
businessCard.name = row['Наименование организации']
businessCard.street = row['Улица']
businessCard.house = row['Номер дома']
businessCard.phones = row['Телефоны']
businessCard.website_url = row['Сайты']
businessCard.latitude = row['Координаты, широта']
businessCard.longitude = row['Координаты, долгота']
businessCard.social_links = row['Социальные сети']
businessCard.city = City.objects.get(name__exact=row['Город'])
businessCard.save()
listSubTags = get_subTags(row['Рубрики'])
print(listSubTags, businessCard.city, businessCard.name)
for subTag in listSubTags:
try:
subTag_Ins = SubTag.objects.get(name__exact=subTag)
except SubTag.DoesNotExist:
continue
businessCard.subTags.add(subTag_Ins)
populate_work_schedule(data=row['Время работы'], business=businessCard)