Example #1
0
def signup(request):
    """Signup, if request == POST, creates the user"""
    ## if authenticated redirect to user's homepage directly ##
    if request.user.is_authenticated():
        return redirect('home')
    if request.method == "POST":
        user_form = QuestrUserCreationForm(request.POST)
        if user_form.is_valid():
            useraddress = dict(city=user_form.cleaned_data['city'], streetaddress=user_form.cleaned_data['streetaddress'],\
                postalcode=user_form.cleaned_data['postalcode'])
            logging.warn(useraddress)
            userdata = user_form.save(commit=False)
            userdata.address = json.dumps(useraddress)
            userdata.phone = user_form.cleaned_data['phone']
            userdata.save()
            authenticate(username=userdata.email, password=userdata.password)
            userdata.backend = 'django.contrib.auth.backends.ModelBackend'
            auth_login(request, userdata)
            verf_link = user_handler.get_verification_url(userdata)
            logger.debug("verification link is %s", verf_link)
            email_details = user_handler.prepWelcomeNotification(
                userdata, verf_link)
            logger.debug("What goes in the email is \n %s", email_details)
            email_notifier.send_email_notification(userdata, email_details)
            pagetitle = "Please verify your email !"
            return redirect('home')

        if user_form.errors:
            logger.debug("Login Form has errors, %s ", user_form.errors)
        pagetitle = "Signup"
        return render(request, 'signup.html', locals())
    else:
        user_form = QuestrUserCreationForm()
        pagetitle = "Signup"
        return render(request, 'signup.html', locals())
Example #2
0
 def informSuperAdmins(self, quest):
     """Takes in a questobject and informs the superadmins of the same"""
     superadmins = self.getSuperAdmins()
     if len(superadmins) == 0:
         return "fail"
     else:
         for admin in superadmins:  # send notifcations to all the shippers
             email_details = quest_handler.prepNewQuestAdminNotification(
                 admin, quest)
             email_notifier.send_email_notification(admin, email_details)
         return "success"
Example #3
0
def userSettings(request):
    """Change's user's personal settings"""
    pagetype = "loggedin"
    user = request.user
    password = user_handler.passwordExists(user)
    userdetails = user_handler.getQuestrDetails(user.id)
    if request.method == "POST":
        logging.warn(request.POST)
        try:
            user_form = QuestrUserChangeForm(request.POST,
                                             request.FILES,
                                             instance=request.user)
        except QuestrUserProfile.DoesNotExist:
            raise Http404
            return render(request, '404.html', locals())
        if user_form.is_valid():
            useraddress = dict(city=user_form.cleaned_data['city'], streetaddress=user_form.cleaned_data['streetaddress'],\
                streetaddress_2=user_form.cleaned_data['streetaddress_2'], postalcode=user_form.cleaned_data['postalcode'])
            if user_form.cleaned_data['email'] != userdetails.email:
                userdata = user_form.save(commit=False)
                userdata.address = json.dumps(useraddress)
                userdata.phone = user_form.cleaned_data['phone']
                userdata.email_status = False
                userdata.save()
                verf_link = user_handler.get_verification_url(userdata)
                logger.debug("verification link is %s", verf_link)
                email_details = user_handler.prepWelcomeNotification(
                    userdata, verf_link)
                logger.debug("What goes in the email is \n %s", email_details)
                email_notifier.send_email_notification(userdata, email_details)
                pagetitle = "Please verify your email !"
                return redirect('home')
            else:
                userdata = user_form.save(commit=False)
                userdata.address = json.dumps(useraddress)
                userdata.phone = user_form.cleaned_data['phone']

            if user_form.cleaned_data['avatar'] is not None:
                userdata.avatar = user_form.cleaned_data['avatar']
            else:
                userdata.avatar = userdetails.avatar
            logger.warn("file is %s" % userdata.avatar)
            userdata.save()
            request.session['alert_message'] = dict(
                type="Success", message="Your profile has been updated!")
            return redirect('settings')
        if user_form.errors:
            logger.debug("Form has errors, %s ", user_form.errors)
    alert_message = request.session.get('alert_message')
    if request.session.has_key('alert_message'):
        del request.session['alert_message']
    pagetitle = "My Settings"
    return render(request, "generalsettings.html", locals())
Example #4
0
def createuser(request):
    """Signup, if request == POST, creates the user"""
    ## if authenticated redirect to user's homepage directly ##
    if request.user.is_authenticated():
        user = request.user
        pagetype = "loggedin"

    if request.method == "POST":
        user_form = QuestrUserCreationForm(request.POST)
        if user_form.is_valid():
            useraddress = dict(city=user_form.cleaned_data['city'], streetaddress=user_form.cleaned_data['streetaddress'],\
                streetaddress_2=user_form.cleaned_data['streetaddress_2'], postalcode=user_form.cleaned_data['postalcode'])
            userdata = user_form.save(commit=False)
            userdata.address = json.dumps(useraddress)
            userdata.email_status = True
            userdata.phone = user_form.cleaned_data['phone']
            import hashlib
            import uuid
            hashstring = hashlib.sha256(
                str(timezone.now()) + str(timezone.now()) +
                str(uuid.uuid4())).hexdigest()
            password = hashstring[:4] + hashstring[-2:]
            userdata.set_password(password)
            userdata.save()
            # default_pricing = { 'rate_meter_drop_weekend' : {'car': 8, 'backpack': 6, 'truck': 90, 'minivan': 10} ,
            #                     'rate_per_km_weekend' : {'car': 1.2, 'backpack': 1.0, 'truck': 90, 'minivan': 2.2},
            #                     'rate_meter_drop_weekday' : {'car': 6, 'backpack': 5, 'truck': 90, 'minivan': 10},
            #                     'rate_per_km_weekday' : {'car': 1.0, 'backpack': 0.8, 'truck': 75, 'minivan': 2.0},
            #                     'hourlist' : {'off_peak_hours': {'start_min': 0, 'start_hr': 8, 'end_hr': 21, 'end_min': 59}},
            #                     'chargefreekm' : 2
            #                     }
            # pricingmodel = pricing.WebPricing(userdata)
            # pricingmodel = QuestPricing(pricing=default_pricing, questrs=userdata)
            # pricingmodel.save()

            email_details = user_handler.prepWelcomeCourierNotification(
                userdata, password)
            logger.debug("What goes in the email is \n %s", email_details)
            email_notifier.send_email_notification(userdata, email_details)
            request.session['alert_message'] = dict(
                type="success", message="User has been created!")
            return redirect('home')

        if user_form.errors:
            logger.debug("Login Form has errors, %s ", user_form.errors)
        pagetitle = "Create a User"
        return render(request, 'createuser.html', locals())
    else:
        user_form = QuestrUserCreationForm()
        pagetitle = "Create a User"
        return render(request, 'createuser.html', locals())
Example #5
0
 def informCourier(self, courier, quest):
     """Takes in a questobject and informs the superadmins of the same"""
     accept_url = quest_handler.get_accept_url(quest, courier)
     reject_url = quest_handler.get_reject_url(quest, courier)
     logging.warn(accept_url)
     logging.warn(reject_url)
     from libs.twilio_handler import twclient
     tw = twclient()
     alert_message = tw.load_newquest_notif(quest, accept_url, reject_url)
     logger.warn(alert_message)
     tw.sendmessage(courier.phone, alert_message)
     email_details = quest_handler.prepNewQuestNotification(
         courier, quest, accept_url, reject_url)
     email_notifier.send_email_notification(courier, email_details)
     return "success"
Example #6
0
def resetpassword(request):
    """
    Mail new random password to the user.
    """
    if request.method == "POST":
        user_email = request.POST['email']
        try:
            user = QuestrUserProfile.objects.get(email=user_email)
        except Exception, e:
            message = "Bruh, a user with that email doesnt exist!"
            return render(request, "resetpassword.html", locals())
        if user:
            reset_link = user_handler.get_password_reset_url(user.email)
            # new_random_password = user_handler.get_random_password()
            # user.set_password(new_random_password)
            # user.save()
            email_details = user_handler.prepPasswordResetNotification(
                user, reset_link)
            email_notifier.send_email_notification(user, email_details)
            message = "Please check your inbox for your new password"
            return redirect('signin')
Example #7
0
def createcourier(request):
    """Signup, if request == POST, creates the user"""
    ## if authenticated redirect to user's homepage directly ##
    if request.user.is_authenticated():
        user = request.user
        pagetype = "loggedin"

    if request.method == "POST":
        user_form = QuestrUserCreationForm(request.POST)
        if user_form.is_valid():
            useraddress = dict(city=user_form.cleaned_data['city'], streetaddress=user_form.cleaned_data['streetaddress'],\
                streetaddress_2=user_form.cleaned_data['streetaddress_2'], postalcode=user_form.cleaned_data['postalcode'])
            userdata = user_form.save(commit=False)
            userdata.address = json.dumps(useraddress)
            userdata.email_status = True
            userdata.is_shipper = True
            userdata.phone = user_form.cleaned_data['phone']
            # import hashlib
            # import uuid
            # hashstring = hashlib.sha256(str(timezone.now()) + str(timezone.now()) + str(uuid.uuid4())).hexdigest()
            # password = hashstring[:4]+hashstring[-2:]
            password = user_form.cleaned_data['password1']
            userdata.set_password(password)
            userdata.save()
            email_details = user_handler.prepWelcomeCourierNotification(
                userdata, password)
            logger.debug("What goes in the email is \n %s", email_details)
            email_notifier.send_email_notification(userdata, email_details)
            request.session['alert_message'] = dict(
                type="success", message="Shipper has been created!")
            return redirect('home')

        if user_form.errors:
            logger.debug("Login Form has errors, %s ", user_form.errors)
        pagetitle = "Create a Courier"
        return render(request, 'createcourier.html', locals())
    else:
        user_form = QuestrUserCreationForm()
        pagetitle = "Create a Courier"
        return render(request, 'createcourier.html', locals())
Example #8
0
def resend_verification_email(request):
    """
        Sends a email verification link to the corresponding email address
    """
    if request.user.is_authenticated():
        user_email = request.user
        try:
            user = QuestrUserProfile.objects.get(email=user_email)
            if user and not user.email_status:
                verf_link = user_handler.get_verification_url(user)
                logger.debug("verification link is %s", verf_link)
                email_details = user_handler.prepWelcomeNotification(
                    user, verf_link)
                logger.debug("What goes in the email is \n %s", email_details)
                email_notifier.send_email_notification(user, email_details)
                # user_handler.send_verfication_mail(user)
        except QuestrUserProfile.DoesNotExist:
            raise Http404
            return render(request, '404.html')
    request.session['alert_message'] = dict(
        type="success",
        message="The verification link has been sent to your email")
    return redirect('home')
Example #9
0
    def post(self, request, format=None):
        """
        Allows couriers to signup
        ---
        request_serializer: serializers.CourierSignupValidationSerializer
        response_serializer: serializers.CourierSignupSerializer
        """
        user = request.user
        ## Error if user is already authenticated
        if user.is_authenticated():
            responsedata = dict(success=False)
            return Response(responsedata, status=status.HTTP_400_BAD_REQUEST)

        data = request.DATA.copy()
        serialized_user = serializers.CourierSignupValidationSerializer(
            data=data)
        if serialized_user.is_valid():
            serialized_user = serializers.CourierSignupSerializer(data=data)
            if serialized_user.is_valid():
                user = serialized_user.save()
                user.is_shipper = True
                user.save()
                ## Using User hander send out verification code to the user on the phone
                logging.warn("user {0} is created".format(user.displayname))
                ## Creating a auth token for the user and return the same as response
                # token = Token.objects.get(user=user)
                # tokendata = dict(token=token.key)
                verf_link = user_handler.get_verification_url(user)
                logger.debug("verification link is %s", verf_link)
                email_details = user_handler.prepWelcomeNotification(
                    user, verf_link)
                logger.debug("What goes in the email is \n %s", email_details)
                email_notifier.send_email_notification(user, email_details)
                responsedata = dict(success=True)
                return Response(responsedata, status=status.HTTP_201_CREATED)
        responsedata = dict(errors=serialized_user.errors, success=False)
        return Response(responsedata, status=status.HTTP_400_BAD_REQUEST)
Example #10
0
def signup_by_invitation(request):
    """Signup, if request == POST, creates the user"""
    ## if authenticated redirect to user's homepage directly ##
    logging.warn(request.POST)
    user = request.user
    if request.user.is_authenticated():
        return redirect('home')

    if request.GET['questr_token']:
        questr_token = request.GET['questr_token']
        try:
            prev_token = UserToken.objects.get(token=questr_token)
            logger.debug("prev_token")
            logger.debug(prev_token)
            if prev_token:
                logger.debug("transctional status")
                logger.debug(prev_token.status)
                if not prev_token.status:
                    if request.method == "POST":
                        user_form = QuestrUserCreationForm(request.POST)
                        if user_form.is_valid():
                            useraddress = dict(city=user_form.cleaned_data['city'], streetaddress=user_form.cleaned_data['streetaddress'],\
                                postalcode=user_form.cleaned_data['postalcode'])
                            logging.warn(useraddress)
                            userdata = user_form.save(commit=False)
                            userdata.address = json.dumps(useraddress)
                            userdata.phone = user_form.cleaned_data['phone']
                            if userdata.email != prev_token.email:
                                request.session['alert_message'] = dict(
                                    type="warning",
                                    message=
                                    "The email provided was not the email the invitation was sent to!"
                                )
                                return redirect('index')
                            if prev_token.token_type == 1:
                                userdata.is_shipper = True
                            else:
                                userdata.is_shipper = False
                            userdata.save()
                            authenticate(username=userdata.email,
                                         password=userdata.password)
                            userdata.backend = 'django.contrib.auth.backends.ModelBackend'
                            auth_login(request, userdata)
                            prev_token.status = True
                            prev_token.save()
                            verf_link = user_handler.get_verification_url(
                                userdata)
                            logger.debug("verification link is %s", verf_link)
                            email_details = user_handler.prepWelcomeNotification(
                                userdata, verf_link)
                            logger.debug("What goes in the email is \n %s",
                                         email_details)
                            email_notifier.send_email_notification(
                                userdata, email_details)
                            pagetitle = "Please verify your email !"
                            return redirect('home')
                        if user_form.errors:
                            logger.debug("Login Form has errors, %s ",
                                         user_form.errors)
                        pagetitle = "Signup By invitation"
                        return render(request, 'signupbyinvitation.html',
                                      locals())
                    else:
                        user_form = QuestrUserCreationForm()
                        questr_token = request.GET['questr_token']
                        pagetitle = "Signup"
                        return render(request, 'signupbyinvitation.html',
                                      locals())
        except prev_token.DoesNotExist:
            return redirect('home')
    return redirect('home')
Example #11
0
 def informQuestrAfterAcceptance(self, courier, questr, quest):
     """Takes in a questobject, a questr object and a courier object and informs the questr of the accepted quest"""
     email_details = quest_handler.prepQuestAppliedNotification(
         courier, questr, quest)
     email_notifier.send_email_notification(questr, email_details)
     return "success"