GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

get_or_create_trail()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
1
from sugarloaf.models import Trail, Area
2
3
def get_or_create(session, model, **kwargs):
4
    """Get first instance of given model by kwargs or create a new one
5
    
6
    from http://stackoverflow.com/questions/2546207/does-sqlalchemy-have-an-equivalent-of-djangos-get-or-create"""
7
    instance = session.query(model).filter_by(**kwargs).first()
8
    if instance:
9
        return instance
10
    else:
11
        instance = model(**kwargs)
12
        session.add(instance)
13
        session.commit()
14
        return instance
15
16
17
def get_or_create_trail(session, trail_name, area_name, trail_difficulty):
18
    trail = Trail.query.filter_by(name=trail_name).first()
19
    if trail:
20
        if trail.difficulty != trail_difficulty:
21
            trail.difficulty = trail_difficulty
22
            session.add(trail)
23
            session.commit()
24
        return trail
25
    else:
26
        area = get_or_create(session, Area, name=area_name)
27
        trail = Trail(name=trail_name, area=area)
28
        session.add(trail)
29
        session.commit()
30
        return trail