Example #1
0
def addAndFind(fields):
    dayToAdd = Observatoryday(day=fields['day'], comment=fields['comment'], selectedactions=fields['selectedactions'], observers=fields['observers'], observatory_id=fields['observatory_id'])
    addDay(dayToAdd)
    days = getDays()
    found = False
    for day in days:
        if day.day == fields['day'] and day.comment == fields['comment'] and day.selectedactions == fields['selectedactions'] and day.observers == fields['observers'] and day.observatory_id == fields['observatory_id']:
            found = True
    return found
Example #2
0
def test_sameDayIsAddedIfDifferentObservatory(app):
    dayProperties = {
        'day': testDate,
        'comment': 'testataan',
        'observers': 'Tomppa',
        'selectedactions': 'Vakkari',
        'observatory_id': 2
    }
    dayToAdd = Observatoryday(day=testDate,
                              comment='testi',
                              observers='Tom',
                              selectedactions='Vakkari',
                              observatory_id=1)
    addDay(dayToAdd)
    found = addAndFind(dayProperties)
    assert found == True
Example #3
0
def test_ifExistingDayIsAddedCommentAndObserverCanBeModified(app):
    dayProperties = {
        'day': testDate,
        'comment': 'testataan',
        'observers': 'Tomppa',
        'selectedactions': 'Vakkari',
        'observatory_id': 1
    }
    dayToAdd = Observatoryday(day=testDate,
                              comment='testi',
                              observers='Tom',
                              selectedactions='Vakkari',
                              observatory_id=1)
    addDay(dayToAdd)
    found = addAndFind(dayProperties)
    assert found == True
Example #4
0
def add_everything():
  # ARRIVING DATA:
  # {
  #    day, comment, observers, observatory, selectedactions, userID,
  #    catches, [{},{},{}]
  #    observationPeriods, [{},{},{}]
  #    observations [{},{},{}]
  #  };

    req = request.get_json()

    # Save observatoryDay 
    observatory_id = getObservatoryId(req['observatory'])
    day = datetime.strptime(req['day'], '%d.%m.%Y')

    new_obsday = Observatoryday(day=day, comment=req['comment'], observers=req['observers'], selectedactions=req['selectedactions'], observatory_id=observatory_id)
    addDay(new_obsday)

    # get dayID of just created day and and then the actual day as Flask/db object
    dayId = getDayId(new_obsday.day, new_obsday.observatory_id)
    day = getDay(dayId)

    obsId = day.observatory_id

    # Save catches
    if len(req['catches']) > 0:
        catches = req['catches']
        catches_with_dayId = catches
        catches_with_dayId.insert(0, dayId)
        create_catches(catches_with_dayId)


    #Save observation periods
    for i, obsperiod in enumerate(req['observationPeriods']):
        locId = getLocationId(obsperiod['location'], obsId)
        obsp = Observationperiod(
            start_time=datetime.strptime(obsperiod['startTime'], '%H:%M'),
            end_time=datetime.strptime(obsperiod['endTime'], '%H:%M'),
            type_id=getTypeIdByName(obsperiod['observationType']),
            location_id=locId, observatoryday_id=dayId)
        db.session().add(obsp)

        # observation period ID
        obspId = getObsPerId(obsp.start_time, obsp.end_time, obsp.type_id, obsp.location_id, obsp.observatoryday_id)

        # Save original shorthand block of observation period
        shorthand = Shorthand(shorthandblock=obsperiod['shorthandBlock'], observationperiod_id=obspId)
        db.session().add(shorthand)

        shorthand_id = Shorthand.query.filter_by(
            shorthandblock=obsperiod['shorthandBlock'], observationperiod_id=obspId, is_deleted=0).first().id

        #Save observation related to this period
        for observation in req['observations']: #observation = { periodOrderNum: i, subObservations: [] }
            if observation['periodOrderNum'] == str(i):

                for subObservation in observation['subObservations']:
                    birdCount = subObservation['adultUnknownCount'] + subObservation['adultFemaleCount']\
                    + subObservation['adultMaleCount'] + subObservation['juvenileUnknownCount'] + subObservation['juvenileFemaleCount']\
                    + subObservation['juvenileMaleCount'] + subObservation['subadultUnknownCount'] + subObservation['subadultFemaleCount']\
                    + subObservation['subadultMaleCount'] + subObservation['unknownUnknownCount'] + subObservation['unknownFemaleCount']\
                    + subObservation['unknownMaleCount']
                    
                    sub_observation = Observation(species=subObservation['species'],
                        adultUnknownCount=subObservation['adultUnknownCount'],
                        adultFemaleCount=subObservation['adultFemaleCount'],
                        adultMaleCount=subObservation['adultMaleCount'],
                        juvenileUnknownCount=subObservation['juvenileUnknownCount'],
                        juvenileFemaleCount=subObservation['juvenileFemaleCount'],
                        juvenileMaleCount=subObservation['juvenileMaleCount'],
                        subadultUnknownCount=subObservation['subadultUnknownCount'],
                        subadultFemaleCount=subObservation['subadultFemaleCount'],
                        subadultMaleCount=subObservation['subadultMaleCount'],
                        unknownUnknownCount=subObservation['unknownUnknownCount'],
                        unknownFemaleCount=subObservation['unknownFemaleCount'],
                        unknownMaleCount=subObservation['unknownMaleCount'],
                        total_count = birdCount,
                        direction=subObservation['direction'],
                        bypassSide=subObservation['bypassSide'],
                        notes=subObservation['notes'],
                        observationperiod_id=obspId,
                        shorthand_id=shorthand_id,
                        account_id=req['userID'])

                    db.session().add(sub_observation)

    db.session().commit()

    return jsonify(req)
Example #5
0
def add_everything():
    # SAAPUVA DATA:
    # {
    #    day, comment, observers, observatory, selectedactions, userID,
    #    catches, [{},{},{}]
    #    observationPeriods, [{},{},{}]
    #    observations [{},{},{}]
    #  };

    # Ensimmäinen rivi kaikki stringejä
    # catches: Lista pyydysobjekteja, TARVITSEE dayIDn ennen tallentamista
    # Observationperiods: Lista havaintojakso-objekteja, TARVITSEE dayID:n ennen tallentamista
    # observationPeriod = {
    #   location,
    #   startTime,
    #   endTime,
    #   observationType,
    #   shorthandBlock,
    # };
    # observations: lista objekteja, joista jokainen sisältää orig. pikakirjoitusrivin
    # ja listan osahavainto-objekteja:
    # [{ periodOrderNum: i, subObservations: [] },  ]

    # shorthand TARVITSEE observationperiod-ID:n
    # subObservation TARVITSEE obsperiod-IDn ja shorthandID:n
    # Observations objektin tietueen 'periodOrderNum, pitäisi vastata observationsPeriods-listan indekseihin,
    # eli jos periodOrderNum on 1, liittyy se observationPeriods-listan indeksissä 1 olevaan havaintojaksoon.
    # Tätä tietoa tarvitaan oikean jakson id:n liittämisessä havaintoon

    #print('***\nData arriving in add-everything')
    req = request.get_json()

    # Save observatoryDay
    observatory_id = getObservatoryId(req['observatory'])
    print('observatory_id', observatory_id)
    day = datetime.strptime(req['day'], '%d.%m.%Y')

    new_obsday = Observatoryday(day=day,
                                comment=req['comment'],
                                observers=req['observers'],
                                selectedactions=req['selectedactions'],
                                observatory_id=observatory_id)
    addDay(new_obsday)

    # get dayID of just created day and and then the actual day as Flask/db object
    dayId = getDayId(new_obsday.day, new_obsday.observatory_id)
    day = getDay(dayId)

    obsId = day.observatory_id

    # Save catches
    if len(req['catches']) > 0:
        catches = req['catches']
        #print('catches type', type(catches))

        print('dayId', type(dayId))
        catches_with_dayId = catches
        catches_with_dayId.insert(0, dayId)
        #print('catches', catches_with_dayId)
        create_catches(catches_with_dayId)

    #Save observation periods
    for i, obsperiod in enumerate(req['observationPeriods']):
        locId = getLocationId(obsperiod['location'], obsId)
        obsp = Observationperiod(
            start_time=datetime.strptime(obsperiod['startTime'], '%H:%M'),
            end_time=datetime.strptime(obsperiod['endTime'], '%H:%M'),
            type_id=getTypeIdByName(obsperiod['observationType']),
            location_id=locId,
            observatoryday_id=dayId)
        db.session().add(obsp)

        # observation period ID
        # tämän voisi suorittaa tyylikkäämmin parametrina pelkkä obsp
        obspId = getObsPerId(obsp.start_time, obsp.end_time, obsp.type_id,
                             obsp.location_id, obsp.observatoryday_id)

        # Save original shorthand block of observation period
        shorthand = Shorthand(shorthandblock=obsperiod['shorthandBlock'],
                              observationperiod_id=obspId)
        db.session().add(shorthand)

        # Toimisi shorthand_id = shorthand.id tms, jolloin ei tarvittaisi seuraava erillistä queryä MUTTA vaatii db.committin
        shorthand_id = Shorthand.query.filter_by(
            shorthandblock=obsperiod['shorthandBlock'],
            observationperiod_id=obspId,
            is_deleted=0).first().id

        #Save observation related to this period

        for observation in req[
                'observations']:  #observation = { periodOrderNum: i, subObservations: [] }
            print(observation['periodOrderNum'])
            if observation['periodOrderNum'] == str(i):

                for subObservation in observation['subObservations']:
                    # subObservation = {
                    # species: "",
                    # adultUnknownCount: 0,
                    # adultFemaleCount: 0,
                    # adultMaleCount: 0,
                    # juvenileUnknownCount: 0,
                    # juvenileFemaleCount: 0,
                    # juvenileMaleCount: 0,
                    # subadultUnknownCount: 0,
                    # subadultFemaleCount: 0,
                    # subadultMaleCount: 0,
                    # unknownUnknownCount: 0,
                    # unknownFemaleCount: 0,
                    # unknownMaleCount: 0,
                    # direction: "",
                    # bypassSide: "",
                    # notes: "",
                    # observationperiod_id: ""
                    # };

                    birdCount = subObservation['adultUnknownCount'] + subObservation['adultFemaleCount']\
                    + subObservation['adultMaleCount'] + subObservation['juvenileUnknownCount'] + subObservation['juvenileFemaleCount']\
                    + subObservation['juvenileMaleCount'] + subObservation['subadultUnknownCount'] + subObservation['subadultFemaleCount']\
                    + subObservation['subadultMaleCount'] + subObservation['unknownUnknownCount'] + subObservation['unknownFemaleCount']\
                    + subObservation['unknownMaleCount']

                    sub_observation = Observation(
                        species=subObservation['species'],
                        adultUnknownCount=subObservation['adultUnknownCount'],
                        adultFemaleCount=subObservation['adultFemaleCount'],
                        adultMaleCount=subObservation['adultMaleCount'],
                        juvenileUnknownCount=subObservation[
                            'juvenileUnknownCount'],
                        juvenileFemaleCount=subObservation[
                            'juvenileFemaleCount'],
                        juvenileMaleCount=subObservation['juvenileMaleCount'],
                        subadultUnknownCount=subObservation[
                            'subadultUnknownCount'],
                        subadultFemaleCount=subObservation[
                            'subadultFemaleCount'],
                        subadultMaleCount=subObservation['subadultMaleCount'],
                        unknownUnknownCount=subObservation[
                            'unknownUnknownCount'],
                        unknownFemaleCount=subObservation[
                            'unknownFemaleCount'],
                        unknownMaleCount=subObservation['unknownMaleCount'],
                        total_count=birdCount,
                        direction=subObservation['direction'],
                        bypassSide=subObservation['bypassSide'],
                        notes=subObservation['notes'],
                        observationperiod_id=obspId,
                        shorthand_id=shorthand_id,
                        account_id=req['userID'])

                    db.session().add(sub_observation)

    db.session().commit()

    print('End of add_everything***\n')

    return jsonify(req)