from copy import deepcopy

from django.utils.crypto import get_random_string
from rest_framework import serializers


class Utils:
	def __init__(self):
		pass

	@staticmethod
	def duplicate(obj, attr_dict=None):
		new_obj = deepcopy(obj)
		new_obj.id = None
		if attr_dict is not None:
			Utils.copy_properties(new_obj, attr_dict)
		new_obj.save()
		return new_obj

	@staticmethod
	def copy_properties(obj, attr_dict):
		for key, value in attr_dict.iteritems():
			setattr(obj, key, value)

	@staticmethod
	def hash_random_4():
		return get_random_string(length=4)

	@staticmethod
	def get_serializer_abstract(Model):
		class SerializerNoModel(serializers.Serializer):
			def create(self, validated_data):
				return Model(id=None, **validated_data)

			def update(self, instance, validated_data):
				for field, value in validated_data.items():
					setattr(instance, field, value)
				return instance

		return SerializerNoModel


class EmptyClass:
	pass
