Пример #1
0
def get_news():
    ses = m.Session()
    filter_interests = []
    news_schema = schemas.NewsScheme()
    try:
        if request.args.get('interests'):
            for n in request.args.get('interests').split(','):
                filter_interests.append(n)
    except:
        pass
    news = ses.query(m.News).order_by(m.News.pub_date.desc()).limit(25)
    filtered_news = []
    if len(filter_interests) != 0:
        for n in news:
            has_interest = False
            for i in n.interests:
                if filter_interests.__contains__(i.interest):
                    has_interest = True
                    break
            if has_interest:
                filtered_news.append(n)
        result = news_schema.dump(filtered_news, many=True)
    else:
        result = news_schema.dump(news, many=True)
    ses.close()
    return {'data': result}
Пример #2
0
def get_answers():
    ses = m.Session()
    survey_records = ses.query(m.SurveyRecord)

    survey_record_schema = schemas.SurveyRecordSchema()
    result = survey_record_schema.dump(survey_records, many=True)
    ses.close()
    return {'data': result}
Пример #3
0
def get_cityparts():
    ses = m.Session()
    parts = ses.query(m.Street)

    street_schema = schemas.StreetSchema()
    result = street_schema.dump(parts, many=True)
    ses.close()
    return {'data': result}
Пример #4
0
def get_surveys_by_id(id):
    ses = m.Session()
    surveys = ses.query(m.Survey).get(id)
    survey_schema = schemas.SurveySchema()

    result = survey_schema.dump(surveys)
    ses.close()
    return {'data': result}
Пример #5
0
def get_Interest():
    ses = m.Session()
    Interest = ses.query(m.Interest)

    Interest_schema = schemas.InterestSchema()
    result = Interest_schema.dump(Interest, many=True)
    ses.close()

    return {'data': result}
Пример #6
0
def post_note():
    user_id = get_jwt_identity()
    ses = m.Session()
    try:
        note_json = request.json
        survey_note_schema = schemas.SurveyNoteSchema()
        note_json.update({'user_id': user_id})
        new_survey_note = survey_note_schema.load(note_json)
        ses.add(new_survey_note)
        ses.commit()
        ses.close()
    except Exception as e:
        return {'error': str(e)}, 400, {'ContentType': 'application/json'}
    return {"data": note_json}, 201, {'ContentType': 'application/json'}
Пример #7
0
def create_or_update_user():
    code = request.args.get("code")
    google_provider_cfg = get_google_provider_cfg()
    token_endpoint = google_provider_cfg["token_endpoint"]

    token_url, headers, body = client.prepare_token_request(
        token_endpoint,
        authorization_response=request.url,
        redirect_url=os.environ.get("GOOGLE_REDIRECT_URI", None) +
        "/glogin/callback",
        code=code)
    token_response = requests.post(
        token_url,
        headers=headers,
        data=body,
        auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
    )

    client.parse_request_body_response(json.dumps(token_response.json()))
    try:
        userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
        uri, headers, body = client.add_token(userinfo_endpoint)
        userinfo_response = requests.get(uri, headers=headers, data=body)
        user_id = userinfo_response.json()['sub']
        ses = m.Session()
        user = ses.query(m.User).filter(m.User.social_id == user_id).first()

        if not user:
            user_schema = schemas.UserSchema()
            new_user = user_schema.load({'social_id': user_id})
            ses.add(new_user)
            ses.commit()
            ses.refresh(new_user)
            ses.close()
            return jwthandler.assign_access_refresh_tokens(
                new_user.id,
                os.environ.get("GOOGLE_REDIRECT_URI", None) + '/registration')
        elif not user.sex:
            return jwthandler.assign_access_refresh_tokens(
                user.id,
                os.environ.get("GOOGLE_REDIRECT_URI", None) + '/registration')
        else:
            return jwthandler.assign_access_refresh_tokens(
                user.id,
                os.environ.get("GOOGLE_REDIRECT_URI", None) + '/')
    except Exception as e:
        return str(e)  #TODO doplnit error handling
Пример #8
0
def update_user():
    user_id = get_jwt_identity()
    ses = m.Session()
    try:
        user_json = request.json
        user_schema = schemas.UserSchema()
        if (user_json['residence_location_id'] == 0):
            user_json.update({'residence_location_id': None})
        if (user_json['work_location_id'] == 0):
            user_json.update({'work_location_id': None})
        user = ses.query(m.User).filter(m.User.id == user_id)
        user.update(user_json)
        ses.commit()
        ses.close()
    except Exception as e:
        return {'error': str(e)}, 400, {'ContentType': 'application/json'}
    return {"data": user_json}, 201, {'ContentType': 'application/json'}
Пример #9
0
def seed_rss():
    ses = m.Session()
    feeds_schema = schemas.RssFeedSchema()

    temp_feeds = ses.query(m.RssFeed).all()

    for feed in temp_feeds:
        for address in feed.rss_address:
            news = parse_rss(address.address, feed.id)
            for n in news:
                try:
                    ses.add(n)
                    ses.commit()
                    #print("new")
                except:
                    print("already in db")
                    ses.rollback()
    ses.close()
Пример #10
0
def post_answer():
    user_id = get_jwt_identity()
    ses = m.Session()
    try:
        answer_json = request.json
        survey_record_schema = schemas.SurveyRecordSchema()
        answer_schema = schemas.AnswerSchema()
        answer_json.update({'user_id': user_id})
        new_survey_record = survey_record_schema.load(answer_json)
        ses.add(new_survey_record)
        if 'answers' in answer_json:
            new_survey_record.answers.extend(
                answer_schema.load(answer_json['answers'], many=True))

        ses.commit()
        ses.close()
    except Exception as e:
        return {'error': str(e)}, 400, {'ContentType': 'application/json'}
    return {"data": answer_json}, 201, {'ContentType': 'application/json'}
Пример #11
0
def get_surveys():
    user_id = get_jwt_identity()
    ses = m.Session()
    surveys = ses.query(m.Survey)
    survey_schema = schemas.SurveySchema(exclude=('questions', 'interests',
                                                  'residence_regions',
                                                  'work_regions'))

    result = survey_schema.dump(surveys, many=True)
    for r in result:
        #return {'err': ses.query(m.SurveyRecord).filter(m.SurveyRecord.survey_id == r['id']).filter(m.SurveyRecord.user_id == user_id).first()}
        r.update({
            'filled':
            True if ses.query(m.SurveyRecord).filter(
                m.SurveyRecord.survey_id == r['id']).filter(
                    m.SurveyRecord.user_id == user_id).first() else False
        })
    ses.close()
    return {'data': result}
Пример #12
0
def get_regInfo():
    ses = m.Session()
    streets = ses.query(m.Street).order_by(m.Street.sub_part).order_by(
        m.Street.street)

    street_schema = schemas.StreetSchema()
    streets_result = street_schema.dump(streets, many=True)

    year = datetime.today().year
    r = range(year - 150, year)
    years = list(reversed([*r]))
    data = {
        'data': {
            "streets": streets_result,
            "years": years,
            "sexes": ["Žena", "Muž"]
        }
    }
    ses.close()
    return data
Пример #13
0
def post_survey():
    ses = m.Session()
    try:
        survey_json = request.json
        survey_schema = schemas.SurveySchema()
        question_schema = schemas.QuestionSchema()
        question_option_schema = schemas.QuestionOptionSchema()

        new_survey = survey_schema.load(survey_json)
        ses.add(new_survey)
        if 'interests' in survey_json:
            for interest in survey_json['interests']:
                new_survey.interests.append(
                    ses.query(m.Interest).get(interest['id']))

        if 'questions' in survey_json:
            for question in survey_json['questions']:
                new_question = question_schema.load(question)
                new_survey.questions.append(new_question)
                if new_question.type != 'text' and 'options' in question:
                    new_question.options.extend(
                        question_option_schema.load(question['options'],
                                                    many=True))

        if 'residence_regions' in survey_json:
            for residence in survey_json['residence_regions']:
                new_survey.residence_regions.append(
                    ses.query(m.Street).get(residence['id']))

        if 'work_regions' in survey_json:
            for work in survey_json['work_regions']:
                new_survey.work_regions.append(
                    ses.query(m.Street).get(work['id']))
        ses.commit()
        ses.close()
    except Exception as e:
        return {'error': str(e)}, 400, {'ContentType': 'application/json'}
    return {"data": survey_json}, 201, {'ContentType': 'application/json'}
from alembic import op
from emuapp import models as m

ses = m.Session()
try:
    ses.query(m.Interests).delete()
    ses.commit()
except:
    ses.rollback()

try:
    ses.query(m.Street).delete()
    ses.commit()
except:
    ses.rollback()

ses.commit()
ses.close()
 def wrapper(*args, **kwargs):
     db = m.Session()
     value = func(*args, **kwargs, ses=db)
     db.close()
     return value