-
-
Save folt/5e0beb71b333e98a43f8a41efb3c51c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
class SmsCode(v1_common_models.AbstractDefaultModel): | |
def __init__(self, *args, **kwargs): | |
super(SmsCode, self).__init__(*args, **kwargs) | |
phone = PhoneNumberField( | |
verbose_name='phone') | |
sms = models.IntegerField( | |
verbose_name='sms', | |
blank=True, | |
null=True) | |
inspection_count = models.IntegerField( | |
verbose_name='inspection', | |
default=0, | |
null=True) | |
country = models.ForeignKey( | |
v1_settings_models.Country, | |
verbose_name='country', | |
blank=False, | |
null=False, | |
on_delete=models.CASCADE) | |
def save(self, *args, **kwargs): | |
if not self.sms: | |
self.sms = v1_common_utils.generator_random(1000, 9999) | |
super(SmsCode, self).save(*args, **kwargs) | |
def __str__(self): | |
return '{id}'.format(id=self.id) | |
class Meta: | |
verbose_name = 'sms code' | |
verbose_name_plural = 'sms codes' | |
# -------------------------- | |
# views.py | |
class DeviceCreateAPIView(CreateAPIView): | |
queryset = Device.objects.all() | |
serializer_class = DeviceCreateSerializer | |
permission_classes = [AllowAny] | |
# -------------------------- | |
# serializers.py | |
class DeviceCreateSerializer(serializers.ModelSerializer): | |
sms = serializers.CharField(required=True, max_length=6) | |
sms_id = serializers.CharField(required=True, max_length=50) | |
installation_id = serializers.CharField(required=True, max_length=50) | |
class Meta: | |
model = Device | |
fields = [ | |
'sms', | |
'sms_id', | |
'installation_id', | |
] | |
def validate_sms_id(self, sms_id): | |
""" | |
Check sms_id does exist. | |
""" | |
try: | |
sms_obj = v1_reg_models.SmsCode.active_objects.get(id=sms_id) | |
except v1_reg_models.SmsCode.DoesNotExist: | |
raise serializers.ValidationError('SMS does not exist.') | |
except Exception as e: | |
raise serializers.ValidationError(e) | |
if sms_obj.inspection_count > 3: | |
raise serializers.ValidationError('Attempts are exhausted.') | |
return sms_obj | |
def validate(self, data): | |
""" | |
Check sms. | |
""" | |
sms_obj = data['sms_id'] | |
data['phone'] = sms_obj.country | |
data['country'] = sms_obj.country | |
if data['sms'] != '0000': # TODO: sms_queryset.sms != data['sms'] | |
sms_obj.inspection_count += 1 | |
sms_obj.save() | |
raise serializers.ValidationError('Incorrect sms') | |
return data | |
def create(self, validated_data): | |
# check profile valid | |
try: | |
profile_qs = Profile.active_objects.get(phone=validated_data['phone']) | |
except Profile.DoesNotExist: | |
profile_qs = Profile.active_objects.create(phone=validated_data['phone'], country=validated_data['country']) | |
try: | |
device, obj = Device.objects.get_or_create( | |
profile=profile_qs, | |
installation_id=validated_data['installation_id']) | |
except Exception as e: | |
raise serializers.ValidationError(e) | |
return device | |
def to_representation(self, device): | |
# TODO: remove is_superuser | |
password = v1_common_utils.generator_hex(50) | |
device.set_password(password) | |
device.is_superuser = True | |
device.is_staff = True | |
device.save() | |
return { | |
'profile': device.profile_id, | |
'username': device.username, | |
'password': password, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment