def create(self, validated_data): versions_data = validated_data.pop('versions') instance = Program(name=validated_data['name']) instance.save() for v in versions_data: version = Version(name=v['name'], program=instance) try: version.save() except IntegrityError: pass areas_data = validated_data.pop('areas') area_instances = [] for v in areas_data: area = Area(name=v['name'], program=instance) area.save() try: area_instances.append(area) except IntegrityError: raise NotAcceptable( detail={ 'message': 'A version with this name already exist.', 'version': v }) instance.save() # instance = Program.objects.get(id=instance.id) return instance
def handle(self, *args, **options): if Lift.objects.exists() or Program.objects.exists(): print('Program data already loaded...exiting.') print(ALREADY_LOADED_ERROR_MESSAGE) return print("Creating lifts.") for lift_name in LIFT_NAMES: lift = Lift(exercise=lift_name) lift.save() print("Loading program data.") for row in DictReader(open('./program_data.csv')): program = Program() program.username = row['Username'] program.program = row['Program'] program.version = row['Version'] program.description = row['Description'] raw_submission_date = row['Date'] submission_date = UTC.localize( datetime.strptime(raw_submission_date, DATETIME_FORMAT)) program.date = submission_date program.save() raw_lift_names = row['Lifts'] lift_names = [name for name in raw_lift_names.split(', ') if name] for lift_name in lift_names: lift = Lift.objects.get(exercise=lift_name) program.lifts.add(lift) program.save() print("Done.")
def get_program(program_id): """ Gets the program from the new database using the program_id from the old database API """ program_id = str(program_id) old_program = mapped_programs[program_id] try: program = Program.objects.get(title=old_program) except ObjectDoesNotExist: program = Program( name=old_program, title=old_program, slug=slugify(old_program), description=old_program, depth=3, ) home_page.add_child(instance=program) program.save() return program
def create(self, request): try: data = JSONParser().parse(request) serializer = ProgramSerializer(data=data) if serializer.is_valid(): pr = Program() pr.name = serializer.validated_data.get('name') pr.description = serializer.validated_data.get('description') pr.level = serializer.validated_data.get('level') pr.weeks = serializer.validated_data.get('weeks') pr.save() predef_routines = [] PF = serializer.validated_data.get('routines') for c in PF: predef_routines.append(c.id) pr.predef_routines.set(predef_routines) pr.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except Program.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST)