def index(request): results = Leave.objects.filter(author=request.user) paginator = Paginator(results, 8) page = request.GET.get("page") if request.method == "POST": form = LeaveForm(request.POST) if form.is_valid(): leave = Leave( author=request.user, description=form.cleaned_data["description"], start_date=form.cleaned_data["start_date"], end_date=form.cleaned_data["end_date"], ) leave.save() messages.add_message(request, messages.SUCCESS, "Leave added successfully") else: form = LeaveForm() try: leaves = paginator.page(page) except PageNotAnInteger: leaves = paginator.page(1) except EmptyPage: leaves = paginator.page(paginator.num_pages) context = {"leaves": leaves, "form": form} return render(request, "leaves/index.html", context)
def index(request): results = Leave.objects.filter(author=request.user) paginator = Paginator(results, 8) page = request.GET.get('page') if request.method == "POST": form = LeaveForm(request.POST) if form.is_valid(): leave = Leave(author=request.user, description=form.cleaned_data["description"], start_date=form.cleaned_data['start_date'], end_date=form.cleaned_data['end_date']) leave.save() messages.add_message(request, messages.SUCCESS, "Leave added successfully") else: form = LeaveForm() try: leaves = paginator.page(page) except PageNotAnInteger: leaves = paginator.page(1) except EmptyPage: leaves = paginator.page(paginator.num_pages) context = {'leaves': leaves, 'form': form} return render(request, "leaves/index.html", context)
def read_create(request): if request.method == 'GET': leaves = Leave.objects.filter(user=request.user).order_by('-from_date') # Fetching leaves that need to be approved by the logged in user. unapproved = [] for junior in request.user.supervisor.all(): unapproved.extend(Leave.objects.filter(user=junior, approved=False)) return render_to_response('leaves/index.html', {'leaves': leaves, 'unapproved': unapproved}, context_instance=RequestContext(request)) elif request.method == 'POST': leave_text = request.POST.get('leave') if leave_text: leave = Leave() leave.user = request.user dates = re.findall('\d{2}/\d{2}/\d{2}', leave_text) if len(dates) > 0: leave.from_date = datetime.datetime.strptime(dates[0], '%d/%m/%y') leave_text = leave_text.replace(dates[0], '') try: leave.to_date = datetime.datetime.strptime(dates[1], '%d/%m/%y') leave_text = leave_text.replace(dates[1], '') except IndexError: pass leave.reason = leave_text leave.save() supervisor = getattr(request.user.userprofile, 'supervisor') user_name = request.user.first_name + ' ' + request.user.last_name if supervisor: if supervisor.email: send_mail( 'Approve leave for {0}'.format(user_name), '''{0} has requested for a leave from {1} to {2}. Reason: {3} To approve the leave please click on the link http://localhost:8000/leaves/{4}'''.format( user_name, dates[0], dates.pop(), leave.reason, leave.id ), EMAIL_HOST_USER, [supervisor.email] ) return HttpResponse('success') return HttpResponse('error')