Ejemplo n.º 1
0
def register(request):
    if request.method == 'POST':
        user_name = request.POST.get('user_name')
        password = request.POST.get('password')
        email = request.POST.get('email')
        user = User().find_one({'email': email})
        if user is not None:
            if user['is_active'] == 1:
                return JsonResponse({
                    'code': 0,
                    'msg': 'Email already exists!'
                })
            if (datetime.datetime.now() - user['create_time']).seconds < 60:
                return JsonResponse({
                    'code':
                    0,
                    'msg':
                    'Please check your email to activate your account!'
                })
        if user is not None and user['is_active'] == 0:
            user_id = str(user['_id'])
        else:
            user_id = str(User().insert_one({
                'username':
                user_name,
                'email':
                email,
                'password':
                get_password(password),
                'salt':
                'example_salt',
                'is_active':
                0,
                'create_time':
                datetime.datetime.now()
            }))
        if EMAIL_ACTIVE_ENABLE is False:
            return JsonResponse({'code': 1, 'msg': 'success'})
        account_activation_token = create_token(
            {
                'user_id': user_id,
                'user_is_active': 0
            }, 60)
        mail_subject = 'Activate your account.'
        current_site = get_current_site(request)
        message = render_to_string(
            'acc_active_email.html', {
                'user_name': user_name,
                'domain': current_site.domain,
                'token': account_activation_token,
                'reset_password': 0
            })
        email = EmailMessage(mail_subject, message, to=[email])
        email.send()

        return JsonResponse({
            'code': 1,
            'msg': 'Activate your account by the email!'
        })
Ejemplo n.º 2
0
def test_user():
    user = User()
    for _id in range(5):
        item = {
            'username': '******' % _id,
            'email': '*****@*****.**',
            'password': '******',
            'salt': 'example_salt',
            'create_time': datetime.datetime.now()
        }
        post_id_1 = user.insert_one(item)
        print(post_id_1)
    item = user.find_one({'username': '******'})
    # user.delete_many({'username': '******'})
    pprint.pprint(item)
Ejemplo n.º 3
0
def login(request):
    if request.method == 'POST':
        if request.POST:
            email = request.POST.get('email')
            password = request.POST.get('password')
            user = User().find_one({'email': email})
            if user is None:
                return JsonResponse({'code': 0, 'msg': 'User not exists!'})
            if user['password'] != get_password(password):
                return JsonResponse({
                    'code': 0,
                    'msg': 'Incorrect email or password.'
                })
            elif user['is_active'] == 0:
                return JsonResponse({'code': 0, 'msg': 'Email not activated!'})
            else:
                request.session["user_email"] = email
                request.session['user_id'] = str(user['_id'])
                return JsonResponse({
                    'code': 1,
                    'msg': '',
                    'user_id': str(user['_id'])
                })
        else:
            return JsonResponse({'code': 0, 'msg': 'Empty Post Data'})
Ejemplo n.º 4
0
def activate(request, token):
    if request.method == "GET":
        acc, payload = authenticate_token(token)
        if acc == 0:
            return HttpResponse(payload)
        else:
            user = User().find_one({'_id': ObjectId(payload['user_id'])})
            if user is None:
                return HttpResponse("User does not exist!")
            if user['is_active'] == 1:
                return HttpResponse("Email is activated!")
            User().collection.update_one({'_id': ObjectId(payload['user_id'])},
                                         {"$set": {
                                             'is_active': 1
                                         }})
        return render(request, 'login.html', {'is_register': 1})
Ejemplo n.º 5
0
def reset_password(request):
    if request.method == 'POST':
        is_send_mail = request.POST.get('is_send_mail')
        if is_send_mail == '1':
            email = request.POST.get('email')
            user = User().find_one({'email': email})

            if user is None:
                return JsonResponse({'code': 0, 'msg': 'Email not exists!'})

            account_activation_token = create_token(
                {'user_id': str(user['_id'])}, 60)
            mail_subject = 'Please reset your password.'
            current_site = get_current_site(request)
            message = render_to_string(
                'acc_active_email.html', {
                    'user_name': user['username'],
                    'domain': current_site.domain,
                    'token': account_activation_token,
                    'reset_password': 1
                })
            email = EmailMessage(mail_subject, message, to=[email])
            email.send()
            return JsonResponse({
                'code': 1,
                'msg': 'The email send successfully!'
            })

        else:
            password = request.POST.get('password')
            token = request.POST.get('token')
            if not all([password, token]):
                return JsonResponse({
                    'code': 0,
                    'msg': 'password or token is None!'
                })
            acc, payload = authenticate_token(token)
            if acc == 0:
                return HttpResponse(payload)
            else:
                user = User().find_one({
                    '_id': ObjectId(payload['user_id']),
                    'is_active': 1
                })
                if user is None:
                    return JsonResponse({
                        'code':
                        0,
                        'msg':
                        'User not exist or Email not activated!'
                    })
                User().collection.update_one(
                    {'_id': ObjectId(payload['user_id'])},
                    {"$set": {
                        'password': get_password(password)
                    }})
            return JsonResponse({
                'code':
                1,
                "msg":
                "Reset password Successfully, Please Login!"
            })
Ejemplo n.º 6
0
def task_register(request):
    """
    Receive a task's (task_id, config_space) and dump it into a pickle file.

    Parameters
    ----------
    request: a django.core.handlers.wsgi.WSGIRequest object with following keys:
        'id' : a unique task id
        'config_space_json' : json string of a config_space created by ConfigSpace.read_and_write.json.write()

    Returns
    -------
    (readable information) string in HttpResponse form

    """
    if request.method == 'POST':
        if request.POST:
            # Parse request
            # task_id = request.POST.get('task_id')
            email = request.POST.get('email')
            password = request.POST.get('password')
            user = User().find_one({'email': email})
            if user is None:
                return JsonResponse({
                    'code':
                    0,
                    'msg':
                    '[bo_advice/views.py] User does not exist'
                })
            else:
                if user['password'] != hashlib.md5(
                        password.encode(encoding='utf-8')).hexdigest():
                    return JsonResponse({
                        'code':
                        0,
                        'msg':
                        '[bo_advice/views.py] Incorrect Password'
                    })

            # task_id = request.POST.get('task_id')
            config_space_json = request.POST.get('config_space_json')
            config_space = config_json.read(config_space_json)

            num_constraints = int(request.POST.get('num_constraints', 0))
            num_objs = int(request.POST.get('num_objs', 1))
            task_info = {
                'num_constraints': num_constraints,
                'num_objs': num_objs
            }
            options = json.loads(request.POST.get('options', '{}'))

            time_limit_per_trial = int(
                request.POST.get('time_limit_per_trial', 300))
            active_worker_num = int(request.POST.get('time_limit_per_trial',
                                                     1))
            parallel_type = request.POST.get('parallel_type', 'async')
            task_name = request.POST.get('task_name', 'task')
            if task_name == 'task':
                n = Task().find_all().count()
                task_name += '_' + str(n)
            # Create advisor
            advisor_type = request.POST.get('advisor_type', 'default')
            max_runs = request.POST.get('max_runs', 200)

            task_id = str(Task().insert_one({
                'task_name':
                task_name,
                'owner':
                str(user['_id']),
                'create_time':
                datetime.datetime.now(),
                'config_space':
                json.loads(config_space_json),
                'status':
                'running',
                'advisor_type':
                advisor_type,
                'max_run':
                max_runs,
                'surrogate_type':
                options['surrogate_type'],
                'time_limit_per_trial':
                time_limit_per_trial,
                'active_worker_num':
                active_worker_num,
                'parallel_type':
                parallel_type
            }))

            if advisor_type == 'default':
                from openbox.core.generic_advisor import Advisor
                config_advisor = Advisor(config_space,
                                         task_info,
                                         task_id=task_id,
                                         **options)
            elif advisor_type == 'tpe':
                from openbox.core.tpe_advisor import TPE_Advisor
                config_advisor = TPE_Advisor(config_space)
            else:
                raise ValueError('Invalid advisor type!')

            # Save advisor in a global dict
            advisor_dict[task_id] = config_advisor

            return JsonResponse({
                'code': 1,
                'msg': 'SUCCESS',
                'task_id': task_id
            })
        else:
            return JsonResponse({'code': 0, 'msg': 'Empty post data'})
    else:
        return JsonResponse({'code': 0, 'msg': 'Should be a POST request'})