Add country transfer
This commit is contained in:
parent
72a84d39db
commit
106ddb044e
|
|
@ -1,25 +1,21 @@
|
|||
from transfer.serializers.news import NewsSerializer
|
||||
from transfer.models import PageTexts
|
||||
from news.models import NewsType
|
||||
from django.db.models import Value, IntegerField, F
|
||||
from transfer.serializers.location import CountrySerializer
|
||||
from transfer.models import Cities
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
def transfer_news():
|
||||
news_type, _ = NewsType.objects.get_or_create(name="News")
|
||||
def transfer_countries():
|
||||
queryset = Cities.objects.exclude(country_code_2__isnull=True).values_list("country_code_2", flat=True).distinct()
|
||||
|
||||
queryset = PageTexts.objects.filter(page__type="News").annotate(news_type=Value(news_type.id, output_field=IntegerField()),
|
||||
playlist=Value(1, output_field=IntegerField()))
|
||||
queryset = queryset.annotate(attachment_file_name=F('page__attachment_file_name'))
|
||||
queryset = queryset.annotate(template=F('page__template'))
|
||||
|
||||
serialized_data = NewsSerializer(data=list(queryset.values()), many=True)
|
||||
serialized_data = CountrySerializer(data=list(queryset.values()), many=True)
|
||||
if serialized_data.is_valid():
|
||||
serialized_data.save()
|
||||
else:
|
||||
pprint(f"News serializer errors: {serialized_data.errors}")
|
||||
pprint(f"Country serializer errors: {serialized_data.errors}")
|
||||
|
||||
|
||||
data_types = {
|
||||
"news": [transfer_news]
|
||||
"dictionaries": [
|
||||
transfer_countries,
|
||||
|
||||
]
|
||||
}
|
||||
|
|
|
|||
26
apps/transfer/serializers/location.py
Normal file
26
apps/transfer/serializers/location.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from rest_framework import serializers
|
||||
from location.models import Country
|
||||
|
||||
|
||||
class CountrySerializer(serializers.ModelSerializer):
|
||||
country_code_2 = serializers.CharField()
|
||||
|
||||
class Meta:
|
||||
model = Country
|
||||
fields = (
|
||||
"country_code_2",
|
||||
)
|
||||
|
||||
def validate(self, data):
|
||||
data["code"] = self.get_country_code(data)
|
||||
del(data['country_code_2'])
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
# Some countries already in database
|
||||
country, _ = Country.objects.get_or_create(**validated_data)
|
||||
return Country
|
||||
|
||||
def get_country_code(self, obj):
|
||||
print(f"OBJECT: {obj}")
|
||||
return obj.get("country_code_2")
|
||||
|
|
@ -154,8 +154,8 @@ DATABASES = {
|
|||
},
|
||||
'legacy': {
|
||||
'ENGINE': 'django.db.backends.mysql',
|
||||
# 'HOST': '172.23.0.1',
|
||||
'HOST': '127.0.0.1',
|
||||
'HOST': '172.20.0.1',
|
||||
# 'HOST': '127.0.0.1',
|
||||
'PORT': 3306,
|
||||
'NAME': 'dev',
|
||||
'USER': 'dev',
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user