Пример #1
0
def save_post(title, content):
    # Create the new post
    new_post = Post(title=title, content=content, date=datetime.datetime.now())

    # Add and Save the new post
    db_session.add(new_post)
    db_session.commit()
Пример #2
0
    def create_product_from_data(self):
        """
        Creates product and required objects it depends on from data.
        """
        # Normalize field names to system names.
        self._cleanse_data()
        # create independent objects first: tags, group (family, range), customer as they do not need
        # anything to exist.
        self._create_objects(self.product_config[INDEPENDENT])

        # create product dependencies in database (ids will be needed to save the product object in next step)
        db_session.flush()
        base_product = self._create_base_product()
        # now create product dependent objects (those need product_id.
        self._create_objects(self.product_config[PRODUCT_DEPENDENT])

        # create industry specific dependent objects.
        multi_relations = self.product_config[MULTI_RELATIONS]
        if self.product_config.get(INDUSTRY_DEPENDENT):
            industry_dependent_relations = self.product_config[INDUSTRY_DEPENDENT]
            self._create_objects(industry_dependent_relations)
            multi_relations.extend(industry_dependent_relations)
        # append dependent objects to product
        for relation in multi_relations:
            # add objects to appropriate fields
            getattr(base_product, relation).extend(self.objects[relation])
        db_session.commit()
Пример #3
0
def index():
    if request.method == 'GET':
        if 'email' in session:
            return render_template('home.html')
        else:
            return render_template('login.html')

    if request.method == 'POST':
        reqData = request.get_data()
        reqDataJson = json.loads(reqData)

        sSchema = studentschema()
        appSchema = studentapplicationschema()

        studentJson = None
        applicationJson = None

        if 'student' in reqDataJson.keys():
            studentJson = constructStudent(reqDataJson['student'])
        if 'application' in reqDataJson.keys():
            applicationJson = constructApplication(reqDataJson['application'])

        stu = sSchema.load(studentJson, session=db_session).data
        stu.id = session['uid']
        stuapp = appSchema.load(applicationJson, session=db_session).data

        db_session.merge(stu)
        stuapp.s_id = stu.id

        db_session.commit()
        db_session.add(stuapp)
        db_session.commit()
        return json.dumps({'status': 'OK'})
Пример #4
0
def edit_photo(photo_id):
    """ Updates a photo instance """
    photo = Photo.query.get(photo_id)
    if photo is None:
        return jsonify(errors="resource not found"), 404
    data = request.get_json()
    headers = request.headers
    jwt_token = headers['X-Authorization']
    message = {}
    # check for valid token
    try:
        decoded = jwt.decode(jwt_token, constants.SECRET_KEY)
    except jwt.exceptions.InvalidTokenError:
        message['error'] = 'token is invalid'
        return jsonify(message), 400
    # check to see if user owns photo
    if decoded['username'] != photo.user.username:
        return jsonify("You must own a photo to edit it"), 401
    if decoded and decoded['username'] == photo.user.username:
        photo.name = data['name']
        photo.description = data['description']
        photo.picture = data['url']
        photo.category_id = data['categoryId']
        db_session.add(photo)
        db_session.commit()
        db_session.refresh(photo)
        photo = photo.serialize
        return jsonify(photo), 202
Пример #5
0
def delete_item_list(name):
    item = Item.query.filter(Item.name == name).first()

    db_session.delete(item)
    db_session.commit()

    return item
Пример #6
0
def create_post(user_id, lat, lon, body, artist, img_url="null"):
    p = Post(user_id, lat, lon, body, img_url)
    db_session.add(p)
    # make sure we update our database is updated with the id
    db_session.flush()
    db_session.commit()
    return {'success': 1, 'id': p.id, 'msg': 'success'}
Пример #7
0
def create_source(source_name):
    logger.debug("### Create source [{}] ###".format(source_name))

    source = DataSource(source_name=source_name)
    db_session.add(source)
    db_session.commit()
    return source.source_id
Пример #8
0
def add_location():
    content = request.get_json()
    r = Restaurant(content['id'], content['title'], content['coords']['latitude'], content['coords']['longitude'], content['google_ratings'], content['types'])
    db_session.add(r)
    db_session.commit()
    db_session.remove()
    return 'success'
Пример #9
0
def create_chart(chart_name):
    logger.debug("### Create chart [{}] ###".format(chart_name))

    chart = Chart(chart_name=chart_name)
    db_session.add(chart)
    db_session.commit()

    return chart.chart_id
Пример #10
0
def crawl_scrape(src_id):
    source = db_session.query(Source).filter(Source.id == src_id).first()
    xp = {}
    f = {}
    util = {}
    util['src_id'] = src_id
    util['dname'] = source.domain
    xp['title'] = str(source.xp_title)
    xp['lead'] = source.xp_lead
    xp['content'] = source.xp_content
    xp['date'] = source.xp_date
    xp['author'] = source.xp_author
    xp['keywords'] = source.xp_keywords
    f['title'] = source.f_title
    f['lead'] = source.f_lead
    f['content'] = source.f_content
    f['date'] = source.f_date
    f['author'] = source.f_author
    f['keywords'] = source.f_keywords
    source.crawling = True
    db_session.add(source)
    db_session.commit()

    proc = CrawlerProcess({
        'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
        'CLOSESPIDER_PAGECOUNT': 20
    })

    proc.crawl(MainSpider, xp, f, util)
    proc.start(stop_after_crawl=False)
    #d.addBoth(lambda _: reactor.stop())
    #reactor.run()
    current_texts = db_session.query(Text).filter(Text.src_id == src_id).all()

    c = 0
    for t in stored:
        duplicate = False
        for at in current_texts:
            if at.content == t['content']:
                duplicate = True
        if not duplicate:
            c += 1
            db_session.add(
                Text(src_id=t['source'],
                     url=t['url'],
                     title=t['title'],
                     lead=t['lead'],
                     content=t['content'],
                     date=t['date'],
                     author=t['author'],
                     keywords=t['keywords'],
                     lang=t['lang'],
                     num_token=t['num_token']))
    source.num_files_html += c
    source.crawling = False
    db_session.add(source)
    db_session.commit()
    return stored
Пример #11
0
def signup():
    form = request.json
    new_user = User(
        civilite=form["civilite"],
        firstName=form["firstName"],
        lastName=form["lastName"],
        birthDate=form["birthDate"],
        email=form["email"],
        phone=form["phone"],
        plastaId=form["plastaId"],
        pwd=form["password"],
        group=form["group"],
    )
    try:
        db_session.add(new_user)
        db_session.commit()
    except sqlalchemy.exc.IntegrityError as err:
        duplicated_key = err.orig.msg.split("'")[-2]
        return (
            jsonify(
                {
                    "msg": "{} est déja utilisée. Si vous avez déja un compte et oublié votre mot de passe, cliquer sur 'Renvoi du mot de passe' sur la page de login".format(
                        duplicated_key
                    )
                }
            ),
            422,
        )

    # Send a verification mail
    url_conf = generate_confirmation_token(form["email"])
    msg = Message(
        "Validation de votre inscription à J4U",
        sender="*****@*****.**",
        recipients=[form["email"]],
    )
    msg.html = """
                <p>
                Bonjour,
                </p>
                <p>
                Nous vous remercions pour votre participation au projet « Job For You » (J4U).
                </p>
                <p>
                Suite à votre inscription, voici un email de confirmation. Afin de valider votre compte, il vous suffit de cliquer sur le lien suivant (qui n’est actif que quelques jours) :
                </p>
                <p>
                <a href="{}">Cliquez ici pour confirmer votre adresse email</a>
                </p>
                <p>
                L’équipe J4U
                </p>
                """.format(
        url_conf
    )
    mail.send(msg)
    res = jsonify(success=True)
    return res
Пример #12
0
def clearOverride():
    try:
        if 'email' not in session:
            return redirect(url_for('index'))
        odel = db_session.query(overrides).delete()
        db_session.commit()
    except Exception:
        return json.dumps({'status': 'clear failed'})
    return json.dumps({'status': 'OK'})
Пример #13
0
def fblogin():
    """ Handles login requests through facebook login"""
    data = request.get_json()
    print "data from request is"
    print data
    message = {}
    message['message'] = "facebook login in received"

    email = data['email']
    print email
    user = helpers.user_by_email(email)
    print user

    if user:
        # create a JWT token to login

        token_data = {
            'iat': datetime.datetime.utcnow(),
            'exp': datetime.datetime.utcnow() + datetime.timedelta(days=30),
            'username': user.username,
            'userId': user.id,
            'isLoggedIn': True,
        }
        auth_token = jwt.encode(token_data,
                                constants.SECRET_KEY,
                                algorithm='HS256')
        # create a JSON message with JWT and send it to client
        message['auth_token'] = auth_token
        message['success'] = True
        print message
        return jsonify(message), 200

    if not user:
        # create a new User instance from the facebook login credentails, then a JWT login.
        username = data['name']
        new_user = User(username=username, email=email)
        db_session.add(new_user)
        db_session.commit()
        user_with_id = db_session.refresh(new_user)
        message['user'] = user_with_id

        token_data = {
            'iat': datetime.datetime.utcnow(),
            'exp': datetime.datetime.utcnow() + datetime.timedelta(days=30),
            'username': user_with_id.username,
            'userId': user_with_id.id,
            'isLoggedIn': True,
        }
        auth_token = jwt.encode(token_data,
                                constants.SECRET_KEY,
                                algorithm='HS256')

        message['auth_token'] = auth_token
        message['success'] = True
        print message
        return jsonify(message), 200
Пример #14
0
def sign_s3():
    urlSchema = fileurlschema()

    S3_BUCKET = os.environ.get('S3_BUCKET')

    file_name = request.args.get('file-name')
    file_name2 = request.args.get('file-name2')

    # Initialise the S3 client
    s3 = boto3.client('s3',
                      'us-west-2',
                      config=Config(signature_version='s3v4'))

    name = session['name']

    # appending users name before file in order to prevent it from being overwritten by another file with same name.
    file_name = name + '_' + file_name
    file_name2 = name + '_' + file_name2

    # Generate and return the presigned URL
    presigned_post = s3.generate_presigned_url('get_object',
                                               Params={
                                                   'Bucket': S3_BUCKET,
                                                   'Key': file_name
                                               },
                                               ExpiresIn=3600,
                                               HttpMethod='PUT')
    presigned_post2 = s3.generate_presigned_url('get_object',
                                                Params={
                                                    'Bucket': S3_BUCKET,
                                                    'Key': file_name2
                                                },
                                                ExpiresIn=3600,
                                                HttpMethod='PUT')

    result = {}
    email = session['email']
    fr = fileurl.query.filter_by(email_id=email).first()
    result[
        'resume_url'] = "https://" + S3_BUCKET + ".s3.amazonaws.com/" + file_name
    result[
        'coverletter_url'] = "https://" + S3_BUCKET + ".s3.amazonaws.com/" + file_name2
    result['email_id'] = email

    # if user has already inserted a resume once - just use existing id
    if fr:
        result[u'id'] = fr.id
    else:
        result[u'id'] = str(uuid.uuid1())

    furl = urlSchema.load(result, session=db_session).data

    db_session.merge(furl)
    db_session.commit()

    return json.dumps({'url1': presigned_post, 'url2': presigned_post2})
Пример #15
0
def signup():
    """ Creates a new User """
    data = request.get_json()

    # make sure all required data is present
    message = {}
    success = ''

    username = data['username']
    password = data['password']
    email = data['email']

    # check for missing data
    if username == '':
        message['error_username'] = "******"
        success = False
    if password == '':
        message['error_password'] = "******"
        success = False
    if email == '':
        message['error_email'] = "email is required"
        success = False
    if success is False:
        message['success'] = False
        return jsonify(errors=message), 200

    # check for valid data
    if not helpers.valid_username(str(username)):
        message['error_username'] = "******"
        success = False
    if helpers.user_by_name(username) is not None:
        message['error_username'] = "******"
        success = False
    if not helpers.valid_password(password):
        message['error_password'] = "******"
        success = False
    if not helpers.valid_email(email):
        message['error_email'] = "Email is not valid"
        success = False
    if helpers.user_by_email(str(email)) is not None:
        message['error_email'] = "Email already in use"
    if success is False:
        message['success'] = False
        return jsonify(errors=message), 200

    # hash the password for db storage
    pw_hash = helpers.make_pw_hash(username, password)
    # create new instance of user
    new_user = User(username, email, pw_hash)
    db_session.add(new_user)
    db_session.commit()
    db_session.refresh(new_user)
    message['success'] = True
    message['user'] = new_user.serialize
    return jsonify(message), 201
Пример #16
0
def reset_password():
    token = request.json.get("token", None)
    email = confirm_token(token)
    password = request.json.get("password", None)
    if password and token and email:
        user = User.query.filter_by(email=email).first()
        user.set_password(password)
        db_session.commit()
        return jsonify(success=True)

    return jsonify({"msg": "error"}), 400
Пример #17
0
 def insert_task_data(id, temperature, duration, batch_id):
     try:
         data = TaskDataModel(id=id,
                              timestamp=datetime.now(),
                              temperature=temperature,
                              duration=duration,
                              batch_id=batch_id)
         db_session.add(data)
         db_session.commit()
     except Exception as Err:
         raise Err
Пример #18
0
 def insert_log(log_type, action, message=""):
     try:
         data = LogData(id=uuid.uuid1(),
                        timestamp=datetime.now(),
                        log_type=log_type,
                        action=action,
                        message=message)
         db_session.add(data)
         db_session.commit()
     except Exception as Err:
         raise Err
Пример #19
0
def verify():
    token = request.args.get("token")
    email = confirm_token(token, expiration=3600 * 24)
    if email:
        user = User.query.filter_by(email=email).first()
        user.verified = True
        db_session.commit()
        return redirect("{}/#/verified".format(get_config()["app_url"]), code=302)

        return jsonify(success=True)

    return jsonify({"msg": "error"}), 400
Пример #20
0
def update():
    form = request.json
    current_user = get_current_user()
    current_user.civilite = form["civilite"]
    current_user.firstName = form["firstName"]
    current_user.lastName = form["lastName"]
    current_user.birthDate = form["birthDate"]
    current_user.email = form["email"]
    current_user.phone = form["phone"]
    current_user.plastaId = form["plastaId"]
    db_session.add(current_user)
    db_session.commit()
    return jsonify(success=True)
Пример #21
0
    def _refreshDividendsDB(self, ticker, force_refresh=False):

        # 1 Use yahoo reader to scrape
        res = []
        dividends = None
        try:
            dividends = yahoo_reader.yahoo_reader.get_dividend(
                ticker, force_refresh)
        except Exception as e:
            logger.exception(
                "Failed to parse dividends for {ticker}".format(ticker=ticker))
            return res

        # 2 Add to TB and return
        for index, row in dividends.iterrows():

            period = row[0]
            dividend = row[1]
            # Check if it exists
            query = stockdatamodel.Dividend.query.filter(
                stockdatamodel.Dividend.ticker == ticker).filter(
                    stockdatamodel.Dividend.period == period)
            if query.count() < 1:
                logger.debug(
                    "Don't have this dividend yet {ticker} {period} ".format(
                        ticker=ticker, period=period))
                d = stockdatamodel.Dividend(
                    ticker=ticker,
                    period=period,
                    dividend=dividend,
                )
                try:
                    db_session.add(d)
                    res.append(d)
                except Exception as e:
                    logger.exception("Failed to add dividend to DB")
            else:
                d = query.first()
                # logger.debug(d)
                # If we need to force refresh update it
                if force_refresh:
                    logger.debug(
                        "We are force refreshing dividend {ticker} {period}".
                        format(ticker=ticker, period=period))
                    d.dividend = dividend
                res.append(d)
        try:
            db_session.commit()
            return res
        except Exception as e:
            logger.exception("Failed to commit dividend to DB")
Пример #22
0
def new_photo():
    """ Creates a new photo """
    headers = request.headers
    data = request.get_json()
    # initialze return message
    message = {}
    success = ''
    # check for valid token
    jwt_token = headers['X-Authorization']
    try:
        jwt.decode(jwt_token, constants.SECRET_KEY)
    except jwt.exceptions.InvalidTokenError:
        message['error'] = 'token is invalid'
        return jsonify(message), 400
    name = data['name']
    description = data['description']
    category_id = data['categoryId']
    url = data['url']
    user_id = data['userId']

    # check for missing data
    if name == '':
        message['error_name'] = "you must provide a name"
        success = False
    if description == '':
        message['error_description'] = "you must provide a description"
        success = False
    if category_id == '':
        message['error_category'] = "please choose a cateogry"
        success = False
    if url == '':
        message['error_picture'] = "provide a url for your picture"
        success = False
    if success is False:
        return jsonify(errors=message), 200

    # validate data
    if not helpers.valid_url(url):
        message['error_picture'] = "you must provide a valid url"
        success = False

    if success is False:
        return jsonify(errors=message), 200

    # create a new photo instance
    newPhoto = Photo(name, description, category_id, url, user_id)
    db_session.add(newPhoto)
    db_session.commit()
    db_session.refresh(newPhoto)
    photo = newPhoto.serialize
    return jsonify(photo), 201
Пример #23
0
def positions():
    data = request.json
    job = request.args.get("avam")
    oldJobLabel = data["oldJobLabel"]

    # This parameter is only received by control-group searches
    # Under the J4U condition, oldJobValue and oldJobLabel are persisted on recom
    # Since control bypasses recom, this parameter is persisted here
    if oldJobLabel:
        oldjob = data["codes"]
        current_user = get_current_user()
        current_user.oldJobValue = oldjob[0]["value"]
        current_user.oldJobLabel = data["oldJobLabel"]
        db_session.commit()

    headers = {
        "Accept": "application/json, text/plain, */*",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Postman-Token": "a22088d4-4d7f-4d72-b14d-bceb48ef23db",
        "X-Requested-With": "XMLHttpRequest",
        "cache-control": "no-cache",
    }
    # SECO's JobRoom starts pagination from 0, but our pagination component matches page number and navigation items, so we need to decrease its number by one
    params = (("page", int(data["currentPage"]) - 1), ("size", "10"), ("sort", "score"))

    if data["cantonCodes"] == [""]:
        data["cantonCodes"] = []

    data = {
        "permanent": None,
        "workloadPercentageMin": 0,
        "workloadPercentageMax": 100,
        "onlineSince": 30,
        "displayRestricted": False,
        "keywords": [],
        "professionCodes": data["codes"],
        "communalCodes": [],
        "cantonCodes": data["cantonCodes"],
    }
    response = requests.post(
        "https://www.job-room.ch/jobadservice/api/jobAdvertisements/_search",
        headers=headers,
        params=params,
        data=json.dumps(data),
    )
    res = response.json()
    res = {"totalCount": response.headers.get("X-Total-Count", "0"), "positions": res}

    return jsonify(res)
Пример #24
0
def overrideMatch():
    if 'email' not in session:
        return redirect(url_for('index'))
    try:
        oSchema = overrideschema()
        oDataS = request.get_data()
        oDataJ = json.loads(oDataS)
        oObj = oSchema.load(oDataJ, session=db_session).data
        oObj.s_id = str(oDataJ['s_id'])
        oObj.p_id = str(oDataJ['p_id'])
        db_session.merge(oObj)
        db_session.commit()
    except Exception:
        return json.dumps({'status': 'override failed'})
    return json.dumps({'status': 'OK'})
Пример #25
0
def create_follow(from_id, to_id):
    # assume my_id and friend_id are valid
    u = Follow(from_id, to_id)
    db_session.add(u)
    db_session.flush()
    db_session.commit()
    return {
        'success': 1,
        'from_id': u.from_id,
        'to_id': u.u_id,
        'msg': 'success'
    }


#print(create_post(1, 1000, 100, "dsf"))
Пример #26
0
def retrieve_all(surveyId):
    df = get_surveys()
    uois = User.query.filter_by(formDone=False).all()
    for uoi in uois:
        if uoi.surveyId in df["ID"].unique():
            v = df[df["ID"] == uoi.surveyId][df.columns[1:]].values[0]
            f = Features(*v)
            uoi.features = f
            uoi.formDone = True
            db_session.add(uoi)

    db_session.commit()

    if surveyId in df["ID"].unique():
        return True
    return False
Пример #27
0
def create_user(username, password, first, last, img_url="null"):
    if len(password) > 16:
        return {
            'success': 0,
            'id': 'null',
            'msg': 'password is too long, make less than 16'
        }
    u = User(username, password, first, last, img_url)

    # make sure it is not duplicate
    if db_session.query(User).filter(User.username == username).first():
        return {'success': 0, 'id': 'null', 'msg': 'user already exist'}
    db_session.add(u)
    # make sure we update our database is updated with the id
    db_session.flush()
    db_session.commit()
    return {'success': 1, 'id': u.id, 'msg': 'success'}
Пример #28
0
def worldseatemp_temp():
    logging.debug("worldseatemp_temp() started")
    source_name = "worldseatemp"

    source_id = DataSource.query.filter(DataSource.source_name == source_name)
    source_id = source_id.all()

    if not source_id:
        source_id = create_source(source_name)
    else:
        source_id = source_id[0].source_id

    temp = get_temp_worldseatemp()

    data = Data(indicator=temp, chart_id=chart_id, source_id=source_id)

    db_session.add(data)
    db_session.commit()
Пример #29
0
def worldseatemp_temp():
    logging.debug("worldseatemp_temp() started")
    source_name = "worldseatemp"

    source_id = DataSource.query.filter(DataSource.source_name == source_name)
    source_id = source_id.all()

    if not source_id:
        source_id = create_source(source_name)
    else:
        source_id = source_id[0].source_id

    temp = get_temp_worldseatemp()

    data = Data(indicator=temp, chart_id=chart_id, source_id=source_id)

    db_session.add(data)
    db_session.commit()
Пример #30
0
def delete_user(user_id):
    """ Deletes a User """
    user = User.query.get(user_id)
    headers = request.headers
    jwt_token = headers['X-Authorization']
    # check for valid token
    try:
        decoded = jwt.decode(jwt_token, constants.SECRET_KEY)
    except jwt.exceptions.InvalidTokenError:
        return jsonify("token is not valid"), 400
    if user is None:
        return jsonify(Message="Not Found"), 404
    # check to make sure user names match
    if decoded['username'] != user.username:
        return jsonify("you can only delete your own profile"), 401
    db_session.delete(user)
    db_session.commit()
    return jsonify(success=True)
Пример #31
0
def before_first_request():
    init_db()
    user_datastore.find_or_create_role(name='admin',
                                       description='Administrator')
    user_datastore.find_or_create_role(name='user', description='User')
    user_datastore.find_or_create_role(name='operator', description='Operator')
    encrypted_password = encrypt_password('password')
    if not user_datastore.get_user('*****@*****.**'):
        user_datastore.create_user(email='*****@*****.**',
                                   username='******',
                                   password='******')
    if not user_datastore.get_user('*****@*****.**'):
        user_datastore.create_user(email='*****@*****.**',
                                   username='******',
                                   password='******')
    db_session.commit()
    user_datastore.add_role_to_user('*****@*****.**', 'admin')
    user_datastore.add_role_to_user('*****@*****.**', 'operator')
    db_session.commit()