Пример #1
0
def ajax_add_user():
    '''
    Called remotely to add a new user.
    '''
    if not current_user.is_authenticated():
        abort(403)

    name = request.form['name']
    email = request.form['email'].lower()
    new_password = request.form['new_password']
    new_password_repeat = request.form['new_password_repeat'] 
        
    if current_user.mongodb_user.email != "*****@*****.**":
        abort(403)
        
    #check passwords
    if new_password != new_password_repeat:
        abort(400)
        
    if new_password == "":
        abort(400)
        
    #hash password
    m = hashlib.sha256()
    m.update(new_password.encode("UTF-8"))
    m.update(SALT.encode("UTF-8"))
        
    #check if user with email address already exists
    users_with_same_email = User.objects(email = email)
    if len(users_with_same_email) > 0:
        abort(400)
        
    try:
        app.logger.debug("Adding new user %s" % name)
            
        #just pick the first article as feedback
        first_article = Article.objects().first()
        first_profile = LearnedProfile(features = first_article.features)
            
        new_user = User(name = name, password = m.hexdigest(),
                        email = email,
                        learned_profile = [first_profile])
        new_user.save(safe=True)
        
        first_feedback = ReadArticleFeedback(user_id = new_user.id,
                                            article = first_article, 
                                            score = 1.0)
        first_feedback.save()
            
        app.logger.debug("...done.")
    except Exception as inst:
        app.logger.error("Could not add new user: %s: %s" % (type(inst), type))
        abort(500)
        
    return ""