def create(self, validated_data): group = None labels = None if 'group' in validated_data: group = validated_data.pop('group') if 'label' in validated_data: labels = validated_data.pop('label') mat_label = validated_data.pop('material_label') student_material = StudentMaterial.objects.create(**validated_data) if group: for g in group: try: student = User.objects.get(id=g) student_material.student_group.add(student) smu = StudentMaterialUser.objects.create(student=student, material=student_material, student_added_by=student_material.material_added_by) smu.save() send_basic_email.delay(student.id, 'UPD') except: pass if labels: for key,val in labels.iteritems(): try: label, created = StudentLabel.objects.get_or_create(label_name=val['label_name']) if created: label.label_created_by = student_material.material_added_by label.save() student_material.material_label.add(label) except ValueError, e: print "ERRR %s" %e pass
def perform_update(self, serializer): if serializer.is_valid(): objective = StudentObjective.objects.get( id=self.request.data['id']) send_basic_email.delay(objective.student.id, 'UPD') serializer.save(objective_updated_by=self.request.user, **self.request.data)
def perform_create(self, serializer): if serializer.is_valid(): studentId = self.request.data.pop('student') student = User.objects.get(id=studentId) send_basic_email.delay(student.id, 'UPD') serializer.save(student=student, objective_created_by=self.request.user, **self.request.data)
def create(self, validated_data): cur_user = validated_data.pop('user') user = User.objects.create_user(**validated_data) user.save() if cur_user.id: user.user_created_by = cur_user else: user.user_created_by = user user.save() send_basic_email.delay(user.id, 'CRE') return user
def update(self, instance, validated_data): instance.student = validated_data.get('student', instance.student) instance.file = validated_data.get('file', instance.file) instance.material_name = validated_data.get('material_name', instance.material_name) instance.material_notes = validated_data.get('material_notes', instance.material_notes) instance.material_updated_by = validated_data.get('material_updated_by', instance.material_updated_by) if 'group' in validated_data: upd_group = [int(x) for x in validated_data.pop('group')] group_student = instance.student_group.all().values_list('id', flat=True) out_group = set(upd_group) ^ set(group_student) in_group = set(upd_group) & set(group_student) if set(upd_group) != set(group_student): for student_id in out_group: if student_id in upd_group: student = User.objects.get(id=student_id) instance.student_group.add(student) smu = StudentMaterialUser.objects.create(student=student, material=instance, student_updated_by=instance.material_updated_by) smu.save() send_basic_email.delay(student.id, 'UPD') else: student = User.objects.get(id=student_id) instance.student_group.remove(student) smu = StudentMaterialUser.objects.get(student=student, material=instance) smu.delete() if 'label' in validated_data: upd_labels = [v['label_name'] for v in validated_data.pop('label')] group_labels = instance.material_label.all().values_list('label_name', flat=True) out_labels = set(upd_labels) ^ set(group_labels) in_labels = set(upd_labels) & set(group_labels) if set(upd_labels) != set(group_labels): for label_name in out_labels: if label_name in upd_labels: label, created = StudentLabel.objects.get_or_create(label_name=label_name) if created: label.save() instance.material_label.add(label) else: label = StudentLabel.objects.get(label_name=label_name) instance.material_label.remove(label) instance.save() return instance
def create(self, validated_data): cur_user = validated_data.pop('user') try: user = User.objects.create_user(**validated_data) user.save() if cur_user.id: user.user_created_by = cur_user else: user.user_created_by = user user.save() send_basic_email.delay(user.id, 'CRE') return user except Exception, e: return Response({ 'status': 'Bad request', 'message': 'Account could not be created with received data.' }, status=status.HTTP_400_BAD_REQUEST)
def create(self, validated_data): group = None if 'group' in validated_data: group = validated_data.pop('group') student_material = StudentMaterial.objects.create(**validated_data) if group: for g in group: student = User.objects.get(id=g) student_material.student_group.add(student) try: send_basic_email.delay(student.id, 'UPD') except: pass try: send_basic_email.delay(student_material.student.id, 'UPD') except: pass student_material.save() return student_material
def update(self, instance, validated_data): instance.first_name = validated_data.get('first_name', instance.first_name) instance.last_name = validated_data.get('last_name', instance.last_name) instance.username = validated_data.get('username', instance.username) instance.email = validated_data.get('email', instance.email) instance.play_level = validated_data.get('play_level', instance.play_level) instance.user_pic = validated_data.get('user_pic', instance.user_pic) instance.user_credit = validated_data.get('user_credit', instance.user_credit) instance.date_of_birth = validated_data.get('date_of_birth', instance.date_of_birth) instance.recurring_credit = validated_data.get('recurring_credit', instance.recurring_credit) instance.user_updated_by = validated_data.pop('user') if validated_data.get('is_active') == 'true': if instance.is_active == False: send_basic_email.delay(instance.id, 'ACT') instance.is_active = True else: instance.is_active = False if instance.location and instance.location.id == validated_data.get('location[id]'): instance.location = validated_data.get('location', instance.location) elif 'location[id]' in validated_data: location_id = validated_data.get('location[id]') location = Location.objects.get(id=location_id) instance.location = location instance.save() password = validated_data.get('password', None) confirm_password = validated_data.get('confirm_password', None) if password and confirm_password and password == confirm_password: instance.set_password(password) instance.save() update_session_auth_hash(self.context.get('request'), instance) return instance
def update(self, instance, validated_data): instance.first_name = validated_data.get('first_name', instance.first_name) instance.last_name = validated_data.get('last_name', instance.last_name) instance.username = validated_data.get('username', instance.username) instance.email = validated_data.get('email', instance.email) instance.play_level = validated_data.get('play_level', instance.play_level) instance.user_pic = validated_data.get('user_pic', instance.user_pic) instance.user_credit = validated_data.get('user_credit', instance.user_credit) instance.date_of_birth = validated_data.get('date_of_birth', instance.date_of_birth) instance.user_updated_by = validated_data.pop('user') if validated_data.get('is_active') == 'true': if instance.is_active == False: send_basic_email.delay(instance.id, 'ACT') instance.is_active = True else: instance.is_active = False if instance.location and instance.location.id == validated_data.get('location[id]'): instance.location = validated_data.get('location', instance.location) elif 'location[id]' in validated_data: location_id = validated_data.get('location[id]') location = Location.objects.get(id=location_id) instance.location = location instance.save() password = validated_data.get('password', None) confirm_password = validated_data.get('confirm_password', None) if password and confirm_password and password == confirm_password: instance.set_password(password) instance.save() update_session_auth_hash(self.context.get('request'), instance) return instance
def update(self, instance, validated_data): instance.student = validated_data.get('student', instance.student) instance.file = validated_data.get('file', instance.file) instance.material_name = validated_data.get('material_name', instance.material_name) instance.material_notes = validated_data.get('material_notes', instance.material_notes) instance.material_updated_by = validated_data.get('material_updated_by', instance.material_updated_by) if 'group' in validated_data: upd_group = [int(x) for x in validated_data.pop('group')] group_student = instance.student_group.all().values_list('id', flat=True) out_group = set(upd_group) ^ set(group_student) in_group = set(upd_group) & set(group_student) if set(upd_group) != set(group_student): for student_id in out_group: if student_id in upd_group: student = User.objects.get(id=student_id) instance.student_group.add(student) send_basic_email.delay(student.id, 'UPD') else: student = User.objects.get(id=student_id) instance.student_group.remove(student) instance.save() return instance