fix comment country

This commit is contained in:
alex 2019-11-12 16:19:03 +03:00
parent 6ba7ac8d82
commit 0db7972b5a
5 changed files with 20 additions and 18 deletions

View File

@ -6,4 +6,4 @@ from . import models
@admin.register(models.Comment)
class CommentModelAdmin(admin.ModelAdmin):
"""Model admin for model Comment"""
raw_id_fields = ('user', 'country')
raw_id_fields = ('user',)

View File

@ -0,0 +1,17 @@
# Generated by Django 2.2.7 on 2019-11-12 13:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('comment', '0004_comment_old_id'),
]
operations = [
migrations.RemoveField(
model_name='comment',
name='country',
),
]

View File

@ -4,10 +4,9 @@ from django.db import models
from django.utils.translation import gettext_lazy as _
from account.models import User
from translation.models import Language
from utils.models import ProjectBaseMixin
from utils.querysets import ContentTypeQuerySetMixin
from translation.models import Language
from location.models import Country
class CommentQuerySet(ContentTypeQuerySetMixin):
@ -34,7 +33,6 @@ class Comment(ProjectBaseMixin):
text = models.TextField(verbose_name=_('Comment text'))
mark = models.PositiveIntegerField(blank=True, null=True, default=None, verbose_name=_('Mark'))
user = models.ForeignKey('account.User', related_name='comments', on_delete=models.CASCADE, verbose_name=_('User'))
country = models.ForeignKey(Country, verbose_name=_('Country'), on_delete=models.SET_NULL, null=True)
old_id = models.IntegerField(null=True, blank=True, default=None)
content_type = models.ForeignKey(generic.ContentType, on_delete=models.CASCADE)

View File

@ -22,7 +22,6 @@ def transfer_comments():
'id',
'comment',
'mark',
'locale',
'establishment_id',
'account_id',
)

View File

@ -1,14 +1,13 @@
from rest_framework import serializers
from comment.models import Comment, User
from establishment.models import Establishment
from location.models import Country
class CommentSerializer(serializers.Serializer):
id = serializers.IntegerField()
comment = serializers.CharField()
mark = serializers.DecimalField(max_digits=4, decimal_places=2)
locale = serializers.CharField()
account_id = serializers.IntegerField()
establishment_id = serializers.CharField()
@ -19,11 +18,9 @@ class CommentSerializer(serializers.Serializer):
'mark': data['mark'] * -1 if data['mark'] < 0 else data['mark'],
'content_object': self.get_content_object(data),
'user': self.get_account(data),
'country': self.get_country(data),
})
data.pop('establishment_id')
data.pop('account_id')
data.pop('locale')
return data
def create(self, validated_data):
@ -45,12 +42,3 @@ class CommentSerializer(serializers.Serializer):
if not user:
raise ValueError(f"User account not found with old_id {data['account_id']}")
return user
@staticmethod
def get_country(data):
locale = data['locale']
country_code = locale[:locale.index("-")] if len(locale) > 2 else locale
country = Country.objects.filter(code=country_code).first()
if not country:
raise ValueError(f"Country not found with code {country_code}")
return country