Ejemplo n.º 1
0
def register_view(request):
    password = request.POST.get('newpassword')
    if password != request.POST.get('passcheck'):
        password = None
        return HttpResponse('passwords dont match! auth failed!')
    user = User()
    user.email = request.POST.get('email')
    user.username = user.email
    user.set_password(password)
    user.first_name = request.POST.get('first')
    user.last_name = request.POST.get('last')
    user.save()
    
    userProfile = UserProfile()
    userProfile.user = user
    userProfile.state = request.POST.get('state')
    userProfile.city = request.POST.get('city')
    userProfile.phone = request.POST.get('phone')
    userProfile.save()
    return redirect('/')
Ejemplo n.º 2
0
def register(request):

    if request.method == 'GET':
        return JsonResponse({
            'status':
            3,
            'message':
            'The API where new users can register themselves on the app.'
        })

    if request.method == 'POST':
        try:
            # just to decode JSON properly
            data = json.loads(request.body.decode('utf8').replace("'", '"'))
        except:
            return JsonResponse({
                "message": "Please check syntax of JSON data passed.",
                'status': 4
            })
        try:
            # see whether all fields passed in JSON or not
            data['name']
            data['email']
            data['phone']
            data['emergency_phone']
        except KeyError as missing_data:
            return JsonResponse({
                "message":
                "Missing the following field: {}".format(missing_data),
                'status':
                2
            })

        try:
            int(data['phone'])
        except:
            #phone numbers should be an integer or string only of numbers
            return JsonResponse({
                'status': 0,
                'message': 'Please enter a valid Phone Number.'
            })

        try:
            int(data['emergency_phone'])
        except:
            #phone numbers should be an integer or string only of numbers
            return JsonResponse({
                'status':
                0,
                'message':
                'Please enter a valid Emergency Phone Number.'
            })

        if len(data['phone']) != 10:
            return JsonResponse({
                'status': 0,
                'message': 'Please enter a valid Phone Number.'
            })
        if len(data['emergency_phone']) != 10:
            return JsonResponse({
                'status':
                0,
                'message':
                'Please enter a valid Emergency Phone Number.'
            })

        email = data['email']
        if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
                        email):
            return JsonResponse({
                'status':
                0,
                'message':
                'Please enter a valid Email address.'
            })

        try:
            UserProfile.objects.get(email=email)
            return JsonResponse({
                'status':
                0,
                'message':
                'This Email has already been registered. Try some other email.'
            })
        except:
            pass
        try:
            profile = UserProfile()
            name = ' '.join(str(data['name']).strip().split())
            profile.name = name
            profile.email = str(data['email'])
            profile.phone = int(data['phone'])
            profile.emergency_phone = int(data['emergency_phone'])
            profile.save()

            #verify email
            send_to = profile.email
            body = email_body.register()
            email_token = utils.generate_email_token(profile)
            body = body % (
                name, str(request.build_absolute_uri(reverse("main:nill"))) +
                'email_confirm/' + email_token + '/')

            sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
            from_email = Email('*****@*****.**')
            to_email = Email(send_to)
            subject = "Email Confirmation for your account on Alertify app"
            content = Content('text/html', body)

            try:
                mail = Mail(from_email, subject, to_email, content)
                response = sg.client.mail.send.post(request_body=mail.get())
            except Exception:
                profile.delete()
                return JsonResponse({
                    'message': 'Error sending email. Please try again.',
                    'status': 0
                })

            message = "Registration successful! A confirmation link has been sent to %s. Kindly click on it to verify your email address." % (
                send_to)
            return JsonResponse({'message': message, 'status': 1})
        except Exception:
            return JsonResponse({
                'message': 'Registration failed due to unknown reasons.',
                'status': 0
            })
Ejemplo n.º 3
0
def add_event(request):
    '''
        The view that will be called when DA will add events from the WebPortal.
    '''

    if request.method == 'POST':

        try:
            user_id = str(request.META['HTTP_X_USER_ID'])
        except KeyError:
            return JsonResponse({
                "message": "Header missing: X-USER-ID",
                "status": 2
            })

        try:
            user_profile = UserProfile.objects.get(uuid=user_id)
            if not user_profile:
                raise Exception
        except Exception:
            return JsonResponse(
                {"message": "The given UserId doesnt correspond to any user."})
        ''' Add the below 2 lines on every page where DA functionalities are there. '''

        if not user_profile.is_da:
            return JsonResponse({
                "message": "You must be logged in as a DA to add events.",
                "status": 0
            })

        try:
            # just to decode JSON properly
            data = json.loads(request.body.decode('utf8').replace("'", '"'))
        except:
            return JsonResponse({
                "message": "Please check syntax of JSON data passed.",
                'status': 4
            })

        try:
            event_name = data['name']
            description = data['description']
            fund_goal = data['fund_goal']
            phone = data['phone']
            email = data['email']
        except KeyError as missing_data:
            return JsonResponse({
                "message":
                "Missing the following field: {}".format(missing_data),
                'status':
                2
            })

        try:
            int(data['fund_goal'])
        except:
            #phone numbers should be an integer or string only of numbers
            return JsonResponse({
                'status':
                0,
                'message':
                'Fund Goal has to be a positive integer.'
            })

        try:
            int(data['phone'])
        except:
            #phone numbers should be an integer or string only of numbers
            return JsonResponse({
                'status': 0,
                'message': 'Please enter a valid phone number.'
            })

        if len(phone) != 10:
            return JsonResponse({
                'status': 0,
                'message': 'Please enter a valid Phone Number.'
            })

        if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
                        email):
            return JsonResponse({
                'status':
                0,
                'message':
                'Please enter a valid Email address.'
            })

        try:
            UserProfile.objects.get(email=email)
            return JsonResponse({
                'status':
                0,
                'message':
                'This Email has already been registered. PLease try some other email.'
            })
        except:
            pass

        try:
            profile = UserProfile()
            event_name = ' '.join(str(event_name).strip().split())
            name = 'Admin for Event: ' + event_name
            profile.name = name
            profile.email = str(email)
            profile.phone = int(phone)
            profile.emergency_phone = int(phone)
            profile.save()

            username = profile.name.split(':')[1] + str(profile.id)
            password = ''.join(choice(chars) for i in range(8))
            user = User.objects.create_user(username=username,
                                            password=password)
            profile.user = user
            profile.save()

            event = Event.objects.create(name=event_name,
                                         description=description,
                                         admin=profile,
                                         fund_goal=fund_goal)
            event.save()

            message = 'Event added Successfully!'
            return JsonResponse({'message': message, 'status': 1})

        except Exception as e:
            print(e)
            return JsonResponse({
                'message': 'Event could not be added. Please try again.',
                'status': 0
            })

    if request.method == 'GET':
        return JsonResponse({"message": "API for DA to add events."})