def main(): """ Imports data from 'dog_details.json', Raises an error if DogSerializer does not exist inside pugorugh/serializers.py """ try: from pugorugh.serializers import DogSerializer except ImportError: raise ImportError("DogSerializer not implemented in serializers.py") # Full path to dog_details.json leveraging # Pythons ability to find file path of this script from earlier. json_file = path.join(PROJECT_DIR, 'pugorugh', 'static', 'dog_details.json') # pass full file path to open() with open(json_file, 'r') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors)
def test_dogretrieveview_next_liked(self): self.apiclient.force_authenticate(user=self.user) response = self.apiclient.get('/api/dog/-1/liked/next/', format='json') self.assertEqual(response.status_code, 200) expected = DogSerializer(Dog.objects.get(id=1)) self.assertJSONEqual(response.content, expected.data) response = self.apiclient.get('/api/dog/1/liked/next/', format='json') self.assertEqual(response.status_code, 200) expected = DogSerializer(Dog.objects.get(id=1)) self.assertJSONEqual(response.content, expected.data)
def load_data(): filepath = path.join(PROJ_DIR, 'pugorugh', 'static', 'dog_details.json') with open(filepath, 'r') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors)
def load_data(): filepath = path.join(DATA_PATH, DATA_FILE) with open(filepath, 'r', encoding='utf-8') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors) print('load_data done.')
def load_data(): filepath = path.join(PROJ_DIR, 'pugorugh', 'static', 'dog_details.json') pdb.set_trace() with open(filepath, 'r', encoding='utf-8') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors) print('load_data done.')
def test_fields(self): dog = Dog.objects.get(id=1) data = DogSerializer(dog).data self.assertEqual( set(data.keys()), set(['id', 'name', 'image_filename', 'breed', 'age', 'gender', 'size', 'birthday', 'likes', 'joined', ] ))
def test_list_doglistcreateview(self): self.apiclient.force_authenticate(user=self.user) response = self.apiclient.get('/api/dog/', format='json') expected = [] for dog in Dog.objects.all(): expected.append(DogSerializer(dog).data) self.assertJSONEqual(response.content, expected)
def main(): try: from pugorugh.serializers import DogSerializer except ImportError: raise ImportError("DogSerializer not implemented in serializers.py") json_file = path.join(PROJECT_DIR, 'pugorugh', 'static', 'dog_details.json') with open(json_file, 'r') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors)
def load_data(): try: from pugorugh.serializers import DogSerializer except ImportError: raise ImportError( 'serializers.py must contain a properly ' 'implemented DogSerializer class for this import to work.') filepath = path.join('backend', 'pugorugh', 'static', 'dog_details.json') with open(filepath, 'r', encoding='utf-8') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors) print('load_data done.')
def put(self, request, *args, **kwargs): pk = self.kwargs.get('pk') status = self.kwargs.get('decision') dog = Dog.objects.get(id=pk) try: obj = UserDog.objects.get(user=self.request.user, dog=dog) except ObjectDoesNotExist: obj = UserDog.objects.create(user=self.request.user, dog=dog, status=status[0]) else: obj.status = status[0] obj.save() dog_object = DogSerializer(dog) return Response(dog_object.data)
# I have left the origninal data_import.py script in the project in case # the data_import script does not work for someone, but this script # does. import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") django.setup() from pugorugh.serializers import DogSerializer import json with open('pugorugh/static/dog_details.json', 'r') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors)
import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") django.setup() from pugorugh.serializers import DogSerializer import json with open('pugorugh/static/dog_details.json', 'r') as file: data = json.load(file) serializer = DogSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() else: print(serializer.errors)
def test_get_correct_value(self): serializer = DogSerializer(data=self.dog_1_data) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.data['name'], self.dog_1_data['name'])