def create_department(request): if not request.user.profile.is_superuser: raise PermissionDenied form = DepartmentCreationForm() if request.method == 'POST': form = DepartmentCreationForm(request.POST) if form.is_valid(): hospital = request.user.profile.department.hospital max_order_dept = hospital.department_set.all().aggregate( Max('order')) print(max_order_dept) new_order = 1 if not max_order_dept else max_order_dept[ 'order__max'] + 1 department = Department( name=form.cleaned_data.get('name'), description=form.cleaned_data.get('description'), order=new_order, hospital=hospital, created_by=request.user) department.save() messages.success(request, "Department Created Successfully!") return redirect('departments:view_departments') else: messages.error(request, "Form Details Invalid") return render(request, "departments/department_creation.html", {"form": form})
def _create_user(self, email, password, department=None, **extra_fields): if not email: raise ValueError('The given email must be set') if not department: department = Department.objects.filter(d_code="-").first() if not department: department = Department(d_code="-", name="Bolumsuz") department.save() email = self.normalize_email(email) user = self.model(email=email, department=department, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create(self, validated_data): user = self.context['request'].user department = Department(**validated_data) department.created_by = user department.updated_by = user department.slug = slugify(uuid.uuid4()) department.save() return department
def handle_department(department_name, department_name_ar, department_location): try: obj = Department.objects.get(department_name=department_name) # print('Department found with id = ' + str(obj.department_id)) except Department.DoesNotExist: obj = Department() # print('College not found, creating new object') obj.department_name = department_name obj.department_name_ar = department_name_ar obj.department_location = department_location try: obj.save() # print("Department " + department_name + ' saved in database') return obj except Exception as e: print("Error in storing the Department object") print(str(e)) return None
def test_Department_name_duplicate_invalid(self): department = Department.objects.create(name="Office") with self.assertRaises(ValidationError): dup_department = Department(name="Office") dup_department.validate_unique()
def test_department_name_length_invalid(self): with self.assertRaises(ValidationError): invalid_len = Department(name="t"*151) invalid_len.full_clean()
def class_process(request): # Get request's context context = RequestContext(request) # Get HTTP POST total number of classes to add total = int(request.POST.get('total')) # ... and the User we are working with user_to_add_id = int(request.POST.get('user_to_add_id')) # Now, for each class to add, check to see if it is already # stored. If not, create a new class/department object. If it # is, create a tutor class object. for i in range(0,int(total)): # Get the HTTP POST objects for this particular class class_to_add = request.POST.get('class_' + str(i)) department_to_add = request.POST.get('department_' + str(i)) number_to_add = request.POST.get('number_' + str(i)) # First check to see if the department exists departments = Department.objects.all() dept_id = None for dept in departments: if dept.name == department_to_add: dept_id = dept.id tutor_to_associate = None for tutor in TutorProfile.objects.all(): if tutor.user.id == user_to_add_id: tutor_to_associate = tutor # If no department id was returned in the above, add it... if dept_id == None: new_department = Department(name=str(department_to_add), short_ident = str(department_to_add)[0:4].upper(), school=tutor_to_associate.school) new_department.save() dept_id = new_department.id # Now check to see if the class exists classes = Class.objects.all() class_id = None for cl in classes: if cl.number == int(number_to_add): class_id = cl.id # If no class id was returned in the above, add it... if class_id == None: new_class = Class(department=Department.objects.get(id=dept_id), title = str(class_to_add), number = int(number_to_add)) new_class.save() class_id = new_class.id if tutor_to_associate == None: return HttpResponse("This tutor does not exist!!!!") # Get the right class to associate class_to_associate = Class.objects.get(id=class_id) # Construct new tutor class object new_tutor_class = TutorClass( tutor=tutor_to_associate, teach=class_to_associate) # Save this tutor class to the database new_tutor_class.save() return HttpResponse("All things added successfully!")
def class_process(request): # Get request's context context = RequestContext(request) # Get HTTP POST total number of classes to add total = int(request.POST.get('total')) # ... and the User we are working with user_to_add_id = int(request.POST.get('user_to_add_id')) # Now, for each class to add, check to see if it is already # stored. If not, create a new class/department object. If it # is, create a tutor class object. for i in range(0, int(total)): # Get the HTTP POST objects for this particular class class_to_add = request.POST.get('class_' + str(i)) department_to_add = request.POST.get('department_' + str(i)) number_to_add = request.POST.get('number_' + str(i)) # First check to see if the department exists departments = Department.objects.all() dept_id = None for dept in departments: if dept.name == department_to_add: dept_id = dept.id tutor_to_associate = None for tutor in TutorProfile.objects.all(): if tutor.user.id == user_to_add_id: tutor_to_associate = tutor # If no department id was returned in the above, add it... if dept_id == None: new_department = Department( name=str(department_to_add), short_ident=str(department_to_add)[0:4].upper(), school=tutor_to_associate.school) new_department.save() dept_id = new_department.id # Now check to see if the class exists classes = Class.objects.all() class_id = None for cl in classes: if cl.number == int(number_to_add): class_id = cl.id # If no class id was returned in the above, add it... if class_id == None: new_class = Class(department=Department.objects.get(id=dept_id), title=str(class_to_add), number=int(number_to_add)) new_class.save() class_id = new_class.id if tutor_to_associate == None: return HttpResponse("This tutor does not exist!!!!") # Get the right class to associate class_to_associate = Class.objects.get(id=class_id) # Construct new tutor class object new_tutor_class = TutorClass(tutor=tutor_to_associate, teach=class_to_associate) # Save this tutor class to the database new_tutor_class.save() return HttpResponse("All things added successfully!")