def subject(request, sid, gid, template_name="eplace/teacher/subject.html"):
    try:
        teacher = request.user.teacher
        subject = teacher.subjects.get(pk=int(sid))
        group = teacher.groups.get(pk=int(gid))
        lessons = LessonDay.objects.filter(teacher=teacher, group=group, subject=subject)
    except Subject.DoesNotExist:
        raise Http404
    except Group.DoesNotExist:
        raise Http404

    today = datetime.datetime.today().date()
    lf = LessonDayForm(initial={'date': today })
    glf = GenerateLessonDayForm(initial={'start_date': today, 'end_date': today})
    
    c = RequestContext(request, {'glf_form': glf, 'lesson_form':lf, 'subject': subject, 'teacher': teacher, 'group': group, 'lessons': lessons})
#    return render_to_response(template_name, c)

    if request.method == "POST":
        lf = LessonDayForm(request.POST)
        if lf.is_valid():
            try:
                o = lf.save(teacher, subject, group)
                return json_response("OK", render_to_string(template_name, c))
            except:
                return json_response("ERROR")

    return json_response("OK", render_to_string(template_name, c))
def ld_delete(request, lid):
    if request.method == "POST":
        teacher = request.user.teacher
        try:
            teacher.lessonday_set.get(pk=int(lid)).delete()
            return json_response("OK",get_page(request))
        except LessonDay.DoesNotExist:
            return json_response("NO SUCH LESSON DAY")
    return json_response("ERROR")
def tick_save(request):
    teacher = request.user.teacher
    if request.method == "POST":
        form = TickForm(teacher, request.POST)
        if form.is_valid():
            form.cleaned_data
            list = request.POST.getlist('tick')
            for x in list:
                save_tick(x, form.cleaned_data.get('group'), form.cleaned_data.get('subject'), teacher)
            return json_response("OK",get_page(request))
    return json_response("ERROR")
def save(request, template_name="eplace/teacher/settings_form.html"):
    if request.method == "POST":
        f = SettingsForm(request.POST)
        if f.is_valid():
            try:
                f.save(request.user)
                c = RequestContext(request, {'form': SettingsForm(initial=get_default_settings(request))})
                return json_response("OK", render_to_string(template_name, c))
            except:
                import traceback
                traceback.print_exc()
                return json_response("ERROR SAVING")
        else:
            return json_response("ERROR FORM")
    return json_response("ERROR")
def get(request, template_name="eplace/student/index.html", limit=50):
    list = Student.objects.all()    
    page = int(request.GET.get('page',request.POST.get('page',1)))
    if request.method == "POST":
        q = request.POST.get('q',None)
        print q
        if q is not None:
            list = Student.objects.filter(name__icontains=q) | Student.objects.filter(group__title__icontains=q)
        list = list[(page-1)*limit:page*limit]
        c = RequestContext(request, {'object_list' : list})
        return json_response("OK", render_to_string("eplace/student/student-list.html", c))
    list = list[(page-1)*limit:page*limit]
    c = RequestContext(request, {'object_list' : list})
    return render_to_response(template_name, c)
def generate(request, sid, gid):
    try:
        teacher = request.user.teacher
        subject = teacher.subjects.get(pk=int(sid))
        group = teacher.groups.get(pk=int(gid))
        lessons = LessonDay.objects.filter(teacher=teacher, group=group, subject=subject)
    except Subject.DoesNotExist:
        raise Http404
    except Group.DoesNotExist:
        raise Http404

    if request.method == "POST":
        glf = GenerateLessonDayForm(request.POST)
        if glf.is_valid():
            try:
                glf.save(teacher, subject, group)
                return json_response("OK",get_page(request))
            except:
                import traceback
                traceback.print_exc()
                return json_response("CANT SAVE")
        else:
            return json_response("INVALID FORM")
    return json_response("ERROR")
def get(request, template_name="eplace/teacher/settings_form.html"):
    c = RequestContext(request, {'form': SettingsForm(initial=get_default_settings(request))})
    if request.method == "POST":
        return json_response("OK", render_to_string(template_name, c))
    return json_response("ERROR")
 def nf(request, *a, **kw):
     if (is_teacher(request.user)):
         return f(request, *a, **kw)
     if request.is_ajax():
         return json_response("ERROR")
     return HttpResponseRedirect(reverse('eplace_index'))