Example #1
0
    def post(self, request,pk):

        form = DoctorScheduleForm(request.POST)
        # print(request.POST)
        if form.is_valid():
            print(request.POST)
            instance=form.save(commit=False)

            instance.doctor_id = pk
            instance.save()
            sender_user = request.user
            receiver_user = instance.doctor.user
            message = f"Your schedule at { instance.date } has been created."
            date = datetime.date.today()
            time = datetime.datetime.now().time()
            notification = Notification(sender_user=sender_user, message=message, status=False,
                                        receiver_user=receiver_user, date=date, time=time)
            notification.save()
            messages.success(request,"Schedule is successfully Created.")
            return redirect('doctor:doctor_schedule_list',pk)

        else:

            context = {
                'form': self.form,


            }
            return render(request, self.template_name, context)
Example #2
0
def handle_subscribe(request,*args):
    user=request.user
    if user.is_authenticated:
        # auth_u = User.objects.get(username=user.email)
        catg=request.session["category"]

        if catg=="teacher":
            return redirect('login_page')

        courseid = request.GET.get('courseid',"")

        curr_course = Course.objects.filter(id=courseid)
        rep={}
        if curr_course:
            
            curr_stu=studentProfile.objects.get(email=user.email)

            curr_course=curr_course[0]
            is_rel = subscription.objects.filter(course=curr_course,student=curr_stu)
            if not is_rel:
                newSUbs = subscription(course=curr_course,student=curr_stu)
                newSUbs.save()
                msg = "Congratulations "+str(curr_stu.firstname)+" "+str(curr_stu.lastname)+" just subscribed to course : "+str(curr_course.name)
                auth_t = User.objects.get(username=curr_course.teacher.email)
                newNotify = Notification(user=auth_t,message=msg)
                newNotify.save()

                print("subadded")

        response=json.dumps(rep)
        return HttpResponse(response,content_type='application/json')


    return redirect('login_page')
Example #3
0
def canceling(request, pk):
    session = Session.objects.filter(pk=pk)[0]
    session.status = 'Canceled'
    session.timeslot.status = 'Available'
    session.timeslot.save()
    price = session.transaction0.amount
    session.student.profile.wallet.addBalance(price)
    medium = User.objects.get(username='******')
    medium.profile.wallet.withdraw(price)
    utcCurrentTime = timezone.now()
    timezonelocal = pytz.timezone('Asia/Hong_Kong')
    currentTime = timezone.localtime(utcCurrentTime, timezonelocal)
    new_transaction = Transaction(from_wallet=medium.profile.wallet,
                                  to_wallet=session.student.profile.wallet,
                                  time=currentTime,
                                  amount=price,
                                  description='Tutorial payment')
    new_transaction.save()
    session.transaction1 = new_transaction
    session.save()
    Notification(
        session.student,
        'Your session on ' + session.timeslot.start.astimezone(
            TIMEZONELOCAL).strftime('%Y-%m-%d %H:%M') + ' ~ ' +
        session.timeslot.end.astimezone(TIMEZONELOCAL).strftime('%H:%M') +
        ' has been canceled, a refund of HK$' + str(price) +
        ' has been added to your wallet.')
    return redirect('session')
Example #4
0
def addvideo(request,course_id):

    user=request.user
    if user.is_authenticated:

        curr_course = Course.objects.filter(id=course_id)
        if len(curr_course)==0:
            return redirect('login_page')

        curr_course=curr_course[0]
        if curr_course.teacher.email!=user.email:
            return redirect('login_page')

        if request.method=='POST':

            vid_title = request.POST.get('videotitle',"")
            desc = request.POST.get('description',"")
            video_thumbnail = request.FILES.get('videothumb',"")
            vid = request.FILES.get('video',"")

            if len(video_thumbnail)==0 or len(vid)==0:
                return redirect('login_page')

            newVideo = Video(course_id=curr_course,title=vid_title,description=desc,vid_thumbnail=video_thumbnail,video=vid)
            Video.save(newVideo)


            # Get followers list and send them new video notification
            curr_user = teacherProfile.objects.get(email=user.email)
            user_followers_obj=Follower.objects.filter(teacher=curr_user)
            user_followers=[]
            if user_followers_obj:
                user_followers=user_followers_obj[0].students.all()
            for i in user_followers:
                msg=curr_user.firstname+" added a new video to the course : "+str(curr_course.name)
                auth_i = User.objects.get(username=i.email)
                newNotify = Notification(user=auth_i,message=msg)
                print(i)
                Notification.save(newNotify)


            return HttpResponseRedirect(reverse('teacher_course',kwargs={'teacher_id':curr_course.teacher.id}) )

        return render(request , 'courses/addvideo.html',{'id':course_id})

    messages.error(request,"Login First")
    return redirect('login_page')
Example #5
0
def reject_appointment(request, pk):

    appointment = Appointment.objects.get(pk=pk)
    appointment.status = "reject"
    sender_user = request.user
    receiver_user = appointment.patient.user
    message = f"Your appointment for { appointment.doctor } at { appointment.appointment_schedule } has been rejected."
    date = datetime.date.today()
    time = datetime.datetime.now().time()
    notification = Notification(sender_user=sender_user,
                                message=message,
                                status=False,
                                receiver_user=receiver_user,
                                date=date,
                                time=time)
    notification.save()

    appointment.save()
    messages.success(request, "This appointment is successfully rejected.")
    return redirect("appointment:new_appointment")
Example #6
0
    def post(self, request, pk):

        form = AppointmentForm(request.POST)
        # print(request.POST)
        if form.is_valid():
            print(request.POST)
            instance = form.save(commit=False)

            instance.patient_id = pk
            instance.status = 'confirm'

            instance.save()
            sender_user = request.user
            receiver_user = instance.doctor.user
            message = f"Your appointment for { instance.patient} at { instance.appointment_schedule } has been created."
            date = datetime.date.today()
            time = datetime.datetime.now().time()
            notification = Notification(sender_user=sender_user,
                                        message=message,
                                        status=False,
                                        receiver_user=receiver_user,
                                        date=date,
                                        time=time)
            notification.save()
            print(notification)

            # return JsonResponse({'success': True})

            return redirect('appointment:appointment_list', pk)

        else:

            context = {
                'form': self.form,
            }
            return render(request, self.template_name, context)
Example #7
0
    def handle(self, *args, **options):

        # for i in range(10000):
        #     new_code = 'COMP' + str(i).zfill(4)
        #     if (len(Course_code.objects.filter(code = new_code)) == 0):
        #         code = Course_code(code = 'COMP' + str(i).zfill(4))
        #         code.save()

        utcCurrentTime = timezone.now()
        timezonelocal = pytz.timezone('Asia/Hong_Kong')
        currentTime = timezone.localtime(utcCurrentTime, timezonelocal)
        tomorrowTime = timezone.localtime(
            utcCurrentTime + datetime.timedelta(hours=24), timezonelocal)

        # Begin all sessioins

        allExpiredTimeslots = Timeslot.objects.filter(
            Q(start__lte=tomorrowTime)
            & (Q(status='Available') | Q(status='Blocked'))).order_by('start')
        for timeslot in allExpiredTimeslots:
            timeslot.status = 'Unavailable'
            timeslot.save()

        allBookedSessions = Session.objects.filter(
            Q(timeslot__start__lte=tomorrowTime,
              status='Booked')).order_by('timeslot__start')
        for session in allBookedSessions:
            session.status = 'Committed'
            session.save()

        # End all sessions
        allFinishedSessions = Session.objects.filter(
            Q(timeslot__end__lte=currentTime,
              status='Committed')).order_by('timeslot__end')
        for session in allFinishedSessions:
            session.status = 'Ended'
            price = session.transaction0.amount
            commission = session.commission
            session.timeslot.tutor.profile.wallet.addBalance(price -
                                                             commission)
            utcCurrentTime = timezone.now()
            timezonelocal = pytz.timezone('Asia/Hong_Kong')
            currentTime = timezone.localtime(utcCurrentTime, timezonelocal)
            medium = User.objects.get(username='******')
            new_transaction = Transaction(
                from_wallet=medium.profile.wallet,
                to_wallet=session.timeslot.tutor.profile.wallet,
                time=currentTime,
                amount=price - commission,
                description='Tutorial payment')
            new_transaction.save()
            session.transaction1 = new_transaction

            if commission != 0:
                MyTutors = User.objects.get(username='******')
                MyTutors.profile.wallet.addBalance(commission)
                new_transaction = Transaction(
                    from_wallet=medium.profile.wallet,
                    to_wallet=MyTutors.profile.wallet,
                    time=currentTime,
                    amount=commission,
                    description='Commission fee')
                new_transaction.save()
            medium.profile.wallet.withdraw(price)
            Notification(
                session.timeslot.tutor, 'A tutorial session fee of HK$' +
                str(price - commission) + ' had been added to your wallet.')
            Notification(
                session.student,
                'You are invited to write a review for this tutorial session.')
            session.save()

        print('Cron All Sessions Success')
Example #8
0
def confirmBooking(request, pk):
    session = Session.objects.get(pk=pk)
    session_day_start = datetime.datetime(session.timeslot.start.year,
                                          session.timeslot.start.month,
                                          session.timeslot.start.day, 0, 0, 0,
                                          0)
    session_day_end = datetime.datetime(session.timeslot.start.year,
                                        session.timeslot.start.month,
                                        session.timeslot.start.day, 23, 0, 0,
                                        0)
    sameTutorHistory = Session.objects.filter(
        student=request.user,
        timeslot__tutor=session.timeslot.tutor,
        timeslot__start__gte=session_day_start,
        timeslot__start__lte=session_day_end,
        status='Booked')
    check1 = False
    if len(sameTutorHistory) == 0:
        check1 = True
    if check1:
        timeClashBookedSession = Session.objects.filter(
            Q(student=request.user)
            & ~Q(timeslot__tutor=session.timeslot.tutor)
            & (Q(timeslot__start=session.timeslot.start)
               | Q(timeslot__end=session.timeslot.end)) & Q(status='Booked'))
        check2 = False
        return_msg = {'success': False, 'msg': ''}
        if len(timeClashBookedSession) == 0:
            check2 = True
        if check2:
            timeClashSelf = Timeslot.objects.filter(
                Q(tutor=request.user) & (Q(start=session.timeslot.start)
                                         | Q(end=session.timeslot.end))
                & (Q(status='Booked') | Q(status='Available')))
            check_self = False
            if len(timeClashSelf) == 0:
                check_self = True
            if check_self:
                session.commission = round(
                    session.timeslot.tutor.tutorprofile.price *
                    decimal.Decimal(0.05), 2)
                price = session.timeslot.tutor.tutorprofile.price + session.commission

                if request.method == 'POST':
                    coupon_code = request.POST.get('coupon', None)
                    check2_5 = Coupon.isValid(coupon_code)
                    if check2_5:
                        session.commission = 0
                        price = session.timeslot.tutor.tutorprofile.price

                check3 = session.student.profile.wallet.checkBalance(price)

                if check3:
                    session.student.profile.wallet.withdraw(price)
                    medium = User.objects.get(username='******')
                    medium.profile.wallet.addBalance(price)
                    utcCurrentTime = timezone.now()
                    timezonelocal = pytz.timezone('Asia/Hong_Kong')
                    currentTime = timezone.localtime(utcCurrentTime,
                                                     timezonelocal)
                    new_transaction = Transaction(
                        from_wallet=session.student.profile.wallet,
                        to_wallet=medium.profile.wallet,
                        time=currentTime,
                        amount=price,
                        description='Tutorial payment')
                    new_transaction.save()
                    session.transaction0 = new_transaction
                    session.status = 'Booked'
                    session.save()
                    Notification(
                        session.student, 'Your session booking on ' +
                        session.timeslot.start.astimezone(
                            TIMEZONELOCAL).strftime('%Y-%m-%d %H:%M') + ' ~ ' +
                        session.timeslot.end.astimezone(
                            TIMEZONELOCAL).strftime('%H:%M') +
                        ' is successful, your have paid HK$' + str(price) +
                        '.')
                    Notification(
                        session.timeslot.tutor,
                        'Your time slot is booked, a new session has been scheduled on '
                        + session.timeslot.start.astimezone(
                            TIMEZONELOCAL).strftime('%Y-%m-%d %H:%M') + ' ~ ' +
                        session.timeslot.end.astimezone(
                            TIMEZONELOCAL).strftime('%H:%M') + '.')
                    return_msg = {
                        'success': True,
                        'msg': 'Booking Successfully Placed'
                    }
                else:
                    session.timeslot.status = 'Available'
                    session.timeslot.save()
                    Notification(
                        session.student,
                        'Your session booking is unsuccessful due to insufficient balance.'
                    )
                    session.delete()
                    button = {'label': 'Go to my wallet', 'link': '/wallet/'}
                    return_msg = {
                        'success': False,
                        'msg': 'Booking Unsuccessful',
                        'reason': 'Insufficient balance in your wallet',
                        'button': button
                    }
            else:
                session.timeslot.status = 'Available'
                session.timeslot.save()
                tutor_id = session.timeslot.tutor.id
                session.delete()
                button = {
                    'label': 'Go back to browse other timeslots',
                    'link': '/viewTutor/' + str(tutor_id)
                }
                return_msg = {
                    'success': False,
                    'msg': 'Booking Unsuccessful',
                    'reason': "A time clash with your teaching time.",
                    'button': button
                }
        else:
            session.timeslot.status = 'Available'
            session.timeslot.save()
            tutor_id = session.timeslot.tutor.id
            session.delete()
            button = {
                'label': 'Go back to browse other timeslots',
                'link': '/viewTutor/' + str(tutor_id)
            }
            return_msg = {
                'success': False,
                'msg': 'Booking Unsuccessful',
                'reason':
                "A time clash with your previously booked sessions occurs.",
                'button': button
            }
    else:
        session.timeslot.status = 'Available'
        session.timeslot.save()
        tutor_id = session.timeslot.tutor.id
        session.delete()
        button = {
            'label': 'Go back to browse other timeslots',
            'link': '/viewTutor/' + str(tutor_id)
        }
        return_msg = {
            'success': False,
            'msg': 'Booking Unsuccessful',
            'reason':
            "You can't book more than one sessions of the same tutor on the same day.",
            'button': button
        }

    return render(request, 'nav-result.html', {'return_msg': return_msg})