Beispiel #1
0
def update_item(event, context):
    """stores data related to item

    Parameters
    ----------
    event: dict, required
        item

        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------
    API Gateway Lambda Proxy Output Format: application/json

        Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    """

    with Session() as session:

        # Parse event dict to Item object
        item = Item()
        json_event = event['item']
        for key in json_event:
            setattr(item, key, json_event[key])

        update_object(item, session)
Beispiel #2
0
def store_itemphrases(event, context):
    """stores key phrases of an item

    Parameters
    ----------
    event: dict, required
        item
        KeyPhrases

        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------
    API Gateway Lambda Proxy Output Format: application/json

        Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    """

    with Session() as session:

        # Store all entities of the item
        for str_phrase in event['KeyPhrases']:
            phrase = Keyphrase()
            # search for entity in database
            try:
                phrase = keyphrase_handler.get_phrase_by_content(str_phrase, session)
            except Exception:
                # store phrase in database
                phrase.id = str(uuid4())
                phrase.phrase = str_phrase
                try:
                    update_object(phrase, session)
                except Exception as e:
                    logger.error(
                        "Could not store key phrase. Exception: %s", e, exc_info=True)
                    raise
            # store item keyphrase in database
            itemphrase = ItemKeyphrase()
            # item phrase already exists?
            item_id = event['item']['id']
            try:
                itemphrase = keyphrase_handler.get_itemphrase_by_phrase_and_item_id(
                    phrase.id, item_id, session)
            except Exception:
                itemphrase.id = str(uuid4())
                itemphrase.item_id = item_id
                itemphrase.keyphrase_id = phrase.id
                try:
                    update_object(itemphrase, session)
                except Exception as e:
                    logger.error(
                        "Could not store item key phrase. Exception: %s", e, exc_info=True)
                    raise
Beispiel #3
0
def store_itemsentiment(event, context):
    """stores sentiment of an item

    Parameters
    ----------
    event: dict, required
        item
        Sentiment

        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------
    API Gateway Lambda Proxy Output Format: application/json

        Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    """

    with Session() as session:

        # Store the sentiment of the item
        sentiment = Sentiment()
        # search for sentiment in database
        str_sentiment = event['Sentiment']
        try:
            sentiment = sentiment_handler.get_sentiment_by_content(str_sentiment, session)
        except Exception:
            # store sentiment in database
            sentiment.id = str(uuid4())
            sentiment.sentiment = str_sentiment
            try:
                update_object(sentiment, session)
            except Exception as e:
                logger.error(
                    "Could not store sentiment. Exception: %s", e, exc_info=True)
                raise
        # store item sentiment in database
        itemsentiment = ItemSentiment()
        # item entity already exists?
        item_id = event['item']['id']
        try:
            itemsentiment = sentiment_handler.get_itemsentiment_by_sentiment_and_item_id(
                sentiment.id, item_id, session)
        except Exception:
            itemsentiment.id = str(uuid4())
            itemsentiment.item_id = item_id
            itemsentiment.sentiment_id = sentiment.id
            try:
                update_object(itemsentiment, session)
            except Exception as e:
                logger.error(
                    "Could not store item sentiment. Exception: %s", e, exc_info=True)
                raise
Beispiel #4
0
def give_experience_point(user_id, session):
    user = get_user_by_id(user_id, session)
    user.experience_points = user.experience_points + 1
    new_level = session.query(Level) \
        .filter(Level.required_experience_points <= user.experience_points) \
        .order_by(Level.required_experience_points.desc()) \
        .first()

    if new_level.id != user.level_id:
        user.level_id = new_level.id
    update_object(user, session)
Beispiel #5
0
def store_tag_for_item(item_id, str_tag, session, review_id=None):
    # search for tag in database
    tag = get_tag_by_content(str_tag, session)
    if tag is None:
        # store tag in database
        tag = Tag()
        tag.id = str(uuid4())
        tag.tag = str_tag
        update_object(tag, session)
    # store item tag in database
    itemtag = ItemTag()
    itemtag.id = str(uuid4())
    itemtag.item_id = item_id
    itemtag.tag_id = tag.id
    if review_id is not None:
        itemtag.review_id = review_id
    update_object(itemtag, session)
Beispiel #6
0
def close_review(review: Review, session) -> Review:
    review.status = "closed"
    review.finish_timestamp = helper.get_date_time_now()
    user_handler.give_experience_point(review.user_id, session)

    pair = review_pair_handler.get_review_pair_from_review(review, session)
    partner_review = get_partner_review(review, session)

    if partner_review != None and partner_review.status == 'closed':

        difference = review_pair_handler.compute_difference(pair)
        pair.variance = difference
        pair.is_good = True if difference <= 1 else False

        review.item.in_progress_reviews_level_1 -= 1
        review.item.in_progress_reviews_level_2 -= 1
        if pair.is_good:
            review.item.open_reviews -= 1
            review.item.open_reviews_level_1 -= 1
            review.item.open_reviews_level_2 -= 1

        pairs = review_pair_handler.get_review_pairs_by_item(
            pair.item_id, session)

        if(len(list(filter(lambda p: p.is_good, pairs))) >= 4):
            review.item.status = "closed"
            review.item.close_timestamp = review.finish_timestamp
            review.item.result_score = item_handler.compute_item_result_score(
                review.item_id, session)

    update_object(review, session)
    update_object(pair, session)
    update_object(review.item, session)

    return review
Beispiel #7
0
def store_factchecks(event, context):
    """stores data related to factchecks

    Parameters
    ----------
    event: dict, required
        FactChecks
        item

        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------

    """

    with Session() as session:

        # Parse event dict to Item object
        for json_event in event['FactChecks']:
            if 'claimReview' not in json_event:
                # raise Exception('No claimReview found in factchecks!')
                return
            organization = FactChecking_Organization()
            factcheck = ExternalFactCheck()
            # What is the publishing organization?
            if 'publisher' not in json_event['claimReview'][0]:
                org_name = "Unknown"
            elif 'site' in json_event['claimReview'][0]['publisher']:
                org_name = json_event['claimReview'][0]['publisher']['site']
            elif 'name' in json_event['claimReview'][0]['publisher']:
                org_name = json_event['claimReview'][0]['publisher']['name']
            else:
                org_name = "Unknown"
            # Does the publishing organization already exist?
            try:
                organization = factchecking_organization_handler.get_organization_by_name(org_name, session)
            except Exception:
                # store organization in database
                organization.id = str(uuid4())
                organization.name = org_name
                organization.counter_trustworthy = 0
                organization.counter_not_trustworthy = 0
                try:
                    update_object(organization, session)
                except Exception as e:
                    logger.error(
                        "Could not store Organization. Exception: %s", e, exc_info=True)

            factcheck_url = json_event['claimReview'][0]['url']
            factcheck_title = json_event['claimReview'][0]['title']
            item_id = event['item']['id']
            try:
                # Does the factcheck already exist?
                factcheck = external_factcheck_handler.get_factcheck_by_url_and_item_id(
                    factcheck_url, item_id, session)
            except Exception as e:
                # create new factcheck in database
                factcheck.id = str(uuid4())
            # store factcheck in database
            factcheck.url = factcheck_url
            factcheck.title = factcheck_title
            factcheck.item_id = item_id
            factcheck.factchecking_organization_id = organization.id
            try:
                update_object(factcheck, session)
            except Exception as e:
                logger.error(
                    "Could not store factchecks. Exception: %s", e, exc_info=True)
                raise
def update_item(event, context):
    """Updates an item.

    Parameters
    ----------
    event: dict, required
        API Gateway Lambda Proxy Input Format

        # api-gateway-simple-proxy-for-lambda-input-format
        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------
    API Gateway Lambda Proxy Output Format: application/json

        Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    """

    log_method_initiated("Update item", event, logger)

    item_id = event['pathParameters']['item_id']

    with Session() as session:

        item = item_handler.get_item_by_id(item_id, session)

        if item is None:
            response = {
                "statusCode": 404,
                "body": "No item found with the specified id."
            }
            response_cors = set_cors(response, event)
            return response_cors

        body = event['body']
        body = json.loads(body) if isinstance(body, str) else body
        for key in body:
            if hasattr(item, key):
                if not isinstance(body[key], dict) and not isinstance(body[key], list):
                    setattr(item, key, body[key])
            else:
                response = {
                    "statusCode": 400,
                    "body": "Could not update item. Provided input does not match item model."
                }
                response_cors = set_cors(response, event)
                return response_cors

        item = update_object(item, session)

    if(item.status == 'rejected'):
        EventPublisher().publish_event(
            'codetekt.admin_service', 'item_rejected', {'item_id': item.id})

    if item is None:
        response = {
            "statusCode": 500,
            "body": "Could not write changes to db. Event id: {}".format(event['requestContext']['requestId'])
        }
        response_cors = helper.set_cors(response, event)
        return response_cors

    response = {
        "statusCode": 200,
        "body": json.dumps(item.to_dict())
    }
    response_cors = set_cors(response, event)
    return response_cors
def prepare_and_store_urls(item: Item, urls: [], session):
    """ prepares the urls (extract the claimant and perform threat check) and stores urls in the item

    Parameters
    ----------
    item:
        Item, required

    urls:
        string [], required

    session:
        Session, required

    Returns
    ------
        item:
            Item

    """
    unsafe_urls = False
    # Store all urls referenced in the item
    for str_url in urls:
        # do not accept urls referencing localhost
        try:
            if str_url == "" or re.search('127\.', str_url) or re.search(
                    'localhost', str_url, re.IGNORECASE):
                continue
        except (AttributeError, TypeError):
            continue

        # store claimant derived from url
        domain = urlparse(str_url).hostname
        claimant = Claimant()
        # claimant already exists?
        try:
            claimant = claimant_handler.get_claimant_by_name(domain, session)
        except Exception:
            # store claimant in database
            claimant.id = str(uuid4())
            claimant.claimant = domain
            try:
                update_object(claimant, session)
            except Exception as e:
                logger.error("Could not store claimant. Exception: %s",
                             e,
                             exc_info=True)
                raise

        url = URL()
        # get or create URL item
        try:
            url = get_url_by_content(str_url, session)
        except Exception:
            # create and store url in database
            url.id = str(uuid4())
            url.url = str_url
            url.claimant_id = claimant.id
            ## check the URL for malware etc.
            url.unsafe = url_threatcheck.threatcheck(str_url)
            if (url.unsafe != None):
                unsafe_urls = True

            try:
                update_object(url, session)
            except Exception as e:
                logger.error("Could not store urls. Exception: %s",
                             e,
                             exc_info=True)
                raise
        itemurl = ItemURL()
        # get or create itemUrl
        try:
            itemurl = get_itemurl_by_url_and_item_id(url.id, item.id, session)
        except Exception:
            # store itemurl in database
            itemurl.id = str(uuid4())
            itemurl.item_id = item.id
            itemurl.url_id = url.id
            try:
                update_object(itemurl, session)
            except Exception as e:
                logger.error("Could not store itemurls. Exception: %s",
                             e,
                             exc_info=True)
                raise

    if unsafe_urls is True:
        item.status = 'Unsafe'
        update_object(item, session)

    return item