def save_or_edit_subject(request, grade_id, subject_id): data = JSON_parse(request.POST.get('data')) print data if 'subject_name' in data: name = data['subject_name'] else: return JSON_response({'status': "error"}) if 'times' in data: times = data['times'] try: grade = Grade.objects.get(id=grade_id) except Grade.DoesNotExist: return JSON_response({'status': "error"}) print subject_id if int(subject_id) != -1: try: subject = Subject.objects.get(id=subject_id) subject.name=name except Subject.DoesNotExist: return JSON_response({'messege': 'school does not exists'}) else: subject = Subject( name=name, grade=grade, ) print "so far so good" subject.save() subject.time.clear() for d in times: if int(d['time_id'] == -1): t = TimeStamp( day=d['time_day'], tm=d['time_tm'] ) t.save() else: try: t = TimeStamp.objects.get(id=int(d['time_id'])) except TimeStamp.DoesNotExist: return JSON_response({'messege': 'this should not happen'}) subject.time.add(t) subject.save() print (subject_to_dict(subject, request.user)) return JSON_response(subject_to_dict(subject, request.user))
def subjects(request): if request.POST: item = Subject( name=request.POST['name'], level_3=request.POST['level_3'], level_4=request.POST['level_4'], level_5=request.POST['level_5'], ) item.save() object_list = Subject.objects.all().order_by("name") return render(request, 'subjects/index.html', locals())
def post(self, request, pk: int = None): if 'delete' in request.POST: subject = Subject.objects.get(id=pk) subject.delete() object_list = Subject.objects.all().order_by("name") return render(request, 'subjects/index.html', locals()) else: item = Subject( name=request.POST['name'], level_3=request.POST['level_3'], level_4=request.POST['level_4'], level_5=request.POST['level_5'], attendance_score=request.POST['attendance_score'], ) item.save() object_list = Subject.objects.all().order_by("name") return render(request, 'subjects/index.html', locals())
def search(request, board_slug=None): """ Handles search functionality for all subjects or within some board. """ if 'query' in request.GET: q = request.GET.get('query', None) if not board_slug: subjects_list = Subject.search_subjects(q) bv = False board = False else: board = get_object_or_404(Board, slug=board_slug) subjects_list = Subject.search_subjects(q, board) bv = True paginator = Paginator(subjects_list, 15) page = request.GET.get('page') if paginator.num_pages > 1: p = True else: p = False try: subjects = paginator.page(page) except PageNotAnInteger: subjects = paginator.page(1) except EmptyPage: subjects = paginator.page(paginator.num_pages) p_obj = subjects return render( request, 'search/search_results.html', { 'page': page, 'subjects': subjects, 'p': p, 'bv': bv, 'board': board, 'q': q, 'p_obj': p_obj }) else: return redirect('home')
def get_queryset(self, *args, **kwargs): queryset_list = Subject.get_subjects() user_query = self.request.GET.get('user', '') board_query = self.request.GET.get('board', '') trending_subjects = self.request.GET.get('trending', '') if user_query: queryset_list = queryset_list.filter(author__username__icontains=user_query, ) if board_query: queryset_list = queryset_list.filter(board__slug__icontains=board_query) if trending_subjects == "True": queryset_list = queryset_list.order_by('-rank_score') return queryset_list
def create(request,id): data = {} student = Student.objects.get(pk=id) subject = Subject() subject.subject_name = request.POST.get('subject_name') subject.student = student subject.save() data['name'] = subject.subject_name data['id'] = subject.id return JsonResponse(data)
def add(request): if request.method == "POST": form = request.POST subject = Subject() subject.name = form['name'] subject.score = form['score'] subject.student = Student.objects.get(pk=form['student']) subject.save() return redirect('subjects:subject_detail',subject.pk) students = Student.objects.all() context = { 'students':students, 'subjects':subjects } return render(request, 'subjects/addSubject.html',context)
def test_subject(self): subject = Subject(user=self.user, uni=self.uni, name='foo') serializer = SearchSerializer(subject, context={'auth_user': self.user}) self.assertEqual(serializer.data['label'], 'foo') self.assertEqual(serializer.data['data']['name'], 'foo')
def handle(self, *args, **options): """ The actual logic of the command. Subclasses must implement this method. It may return a Unicode string which will be printed to stdout. More: https://docs.djangoproject.com/en/1.8/howto/custom -management-commands/#django.core.management.BaseCommand.handle """ text_only = False output = [] if text_only: for a in Subject.get_top_level_subjects().order_by('name'): output.append(name(a)) for b in a.get_children(): output.append(" - ".join([name(a), name(b)])) for c in b.get_children(): output.append(" - ".join([name(a), name(b), name(c)])) for d in c.get_children(): output.append(" - ".join([name(a), name(b), name(c), name(d)])) for e in d.get_children(): output.append(" - ".join([name(a), name(b), name(c), name(d), name(e)])) for f in e.get_children(): output.append(" - ".join([name(a), name(b), name(c), name(d), name(e), name(f)])) for g in f.get_children(): raise Exception('Not enough iterative loops!') else: output.append("<html><body><ul>") for a in Subject.get_top_level_subjects().order_by('name'): output.append("<li><a href='/collex/?view=subjects#" + a.name + "'>" + name(a) + "</a>") output.append("<ul>") for b in a.get_children(): output.append("<li><a href='/collex/?view=subjects#" + b.name + "'>" + name(b) + "</a>") output.append("<ul>") for c in b.get_children(): output.append("<li><a href='/collex/?view=subjects#" + c.name + "'>" + name(c) + "</a>") output.append("<ul>") for d in c.get_children(): output.append("<li><a href='/collex/?view=subjects#" + d.name + "'>" + name(d) + "</a>") output.append("<ul>") for e in d.get_children(): output.append("<li><a href='/collex/?view=subjects#" + e.name + "'>" + name(e) + "</a>") output.append("<ul>") for f in e.get_children(): output.append("<li><a href='/collex/?view=subjects#" + f.name + "'>" + name(f) + "</a>") for g in f.get_children(): raise Exception('Not enough iterative loops!') output.append("</li>") output.append("</ul>") output.append("</li>") output.append("</ul>") output.append("</li>") output.append("</ul>") output.append("</li>") output.append("</ul>") output.append("</li>") output.append("</ul>") output.append("</li>") output.append("</ul></body><p>* indicates a subject with more than one parent.</p></html>") return "\n".join(output)
def get_queryset(self, **kwargs): self.user = get_object_or_404(User, username=self.kwargs['username']) return Subject.get_subjects(self.user)
def store_subjects(path): """ Gets subjects data from subjects.json (generated by web scraping) and instantiates Subject objects in the database """ subjects_data = json_to_data(path) subjects = subjects_data["subjects"] for subject in subjects: institute_instance = Institute.objects.get( initials=subject["institute"]) try: semester_instance = Semester.objects.get( year=subject["semester"]["year"], semester=subject["semester"]["semester"]) except: semester_instance = Semester( year=subject["semester"]["year"], semester=subject["semester"]["semester"]) semester_instance.save() subject_instance = Subject(initials=subject["initials"], name=subject["name"], syllabus=subject["syllabus"], workload=string_to_int(subject["workload"]), institute=institute_instance) subject_instance.save() semester_instance.subjects.add(subject_instance) for pre_req_obj in subject["pre_reqs"]: for pre_req in pre_req_obj["pre_reqs"]: if pre_req != "": try: pre_req_instance = PreReq.objects.get( initials=str(pre_req), year_start=pre_req_obj["year_start"], year_end=pre_req_obj["year_end"]) except: pre_req_instance = PreReq( initials=str(pre_req), year_start=pre_req_obj["year_start"], year_end=pre_req_obj["year_end"]) pre_req_instance.save() subject_instance.prereqs.add(pre_req_instance) for cont_elem in subject["continencies"]: if cont_elem != "": try: continence = Continence.objects.get( initials=str(cont_elem)) except: continence = Continence(initials=str(cont_elem)) continence.save() subject_instance.continences.add(continence) for equiv_elem in subject["equivalencies"]: if equiv_elem != "": try: equivalence = Equivalence.objects.get( initials=str(equiv_elem)) except: equivalence = Equivalence(initials=str(equiv_elem)) equivalence.save() subject_instance.equivalences.add(equivalence)
def mutate(self, info, name, short_name, description): subject = Subject(name=name, short_name=short_name, description=description) subject.save() return CreateSubject(subject=subject)