Ejemplo n.º 1
0
def list_users():
    jwttoken = request.headers.get('Authorization').split(' ')[1]
    user = jwt_auth.get_user_from_token(jwttoken)
    data = Member.query.filter_by(user_id=user.id)
    member_list = []
    memberid_list = []
    memberlastname_list = []
    membergender_list = []
    memberage = []
    isownerlist = []
    for i in data:
        member_list.append(i.firstname)
        memberid_list.append(i.id)
        memberlastname_list.append(i.lastname)
        membergender_list.append(i.gender)
        age = dt.datetime.now().year - i.DoB
        memberage.append(age)
        isownerlist.append(i.is_owner)
    return jsonify(
        dict(members=member_list,
             id=memberid_list,
             lastname=memberlastname_list,
             gender=membergender_list,
             age=memberage,
             owner=isownerlist,
             code=200))
Ejemplo n.º 2
0
def edit_planner(planner_id):
    try:
        planner_name = request.json.get('planner_name')
        first_date = request.json.get('first_date')
        last_date = request.json.get('last_date')
        description = request.json.get('description')
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)

        if not planner_name:
            raise Exception('planner_name cannot be empty')
        if not first_date:
            raise Exception('first_date cannot be empty')
        if not last_date:
            raise Exception('last_date cannot be empty')
        if not description:
            raise Exception('description cannot be empty')

        desiredplanner = Planner.query.filter_by(id=planner_id).one()
        if user.id == desiredplanner.user_id:
            Planner.query.filter(Planner.id == planner_id).\
            update({Planner.name: planner_name}, synchronize_session=False)
            db.session.commit()
            return jsonify(dict(success=True, code=201))
        else:
            raise Exception('no such planner in your id')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 3
0
def planner_belonging(planner_id):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=planner_id).one()
        if user.id == desiredplanner.user_id:
            return jsonify(dict(result=True, code=201))
        else:
            return jsonify(dict(result=False, code=201))
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 4
0
def view_location_in_date(plannerid):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=plannerid).one()
        request_date_string = request.json.get('request_date')
        request_date_string_start = request_date_string + " 00:00"
        request_date_string_stop = request_date_string + " 23:59"
        request_date_start = dt.datetime.strptime(request_date_string_start,
                                                  '%d/%m/%Y %H:%M')
        request_date_stop = dt.datetime.strptime(request_date_string_stop,
                                                 '%d/%m/%Y %H:%M')
        if user.id != desiredplanner.user_id:
            raise Exception('access denied')
        listofactivity = Activity.query.filter(
            Activity.planner_ID == plannerid,
            Activity.start >= request_date_start,
            Activity.start <= request_date_stop).order_by(
                Activity.start.asc()).all()
        if not desiredplanner: raise Exception('no such planner')
        if not listofactivity: raise Exception('no activity in planner')
        if user.id == desiredplanner.user_id:
            locationidlist = []
            for x in listofactivity:
                locationidlist.append(x.location_ID)
            returnlocationdetailslist = []
            returnlocationidlist = []
            returnlocationnamelist = []
            returnlocationlatlist = []
            reutnrlocationlnglist = []
            for x in locationidlist:
                locationdetails = Location.query.filter(Location.id == x).one()
                returnlocationdetailslist.append([
                    locationdetails.id, locationdetails.name,
                    locationdetails.latitude, locationdetails.longtitude
                ])
            return jsonify(
                dict(locationdetailslist=returnlocationdetailslist, code=200))
        else:
            raise Exception('no such user')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 5
0
def view_planner(planner_id):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=planner_id).one()
        if user.id == desiredplanner.user_id:
            returnplanner = [
                desiredplanner.id,
                desiredplanner.name,
                desiredplanner.first_date,
                desiredplanner.last_date,
                desiredplanner.description,
            ]
            return jsonify(dict(planner=returnplanner, code=200))
        else:
            raise Exception('no such planner in your id')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 6
0
def delete_member(id):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        user_id = user.id
        member = Member.query.filter_by(id=id)
        user_id_member = Member.query.filter_by(user_id=user_id).first()
        if user_id == user_id_member.user_id:
            if not member:
                raise Exception('member has been deleted')

            member.delete()
            db.session.commit()
            return jsonify(dict(success=True, code=201))
        else:
            raise Exception('no such member in your id')

    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 7
0
def view_all_activity(plannerid):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=plannerid).one()
        if user.id != desiredplanner.user_id:
            raise Exception('access denied')
        listofactivity = Activity.query.filter_by(
            planner_ID=plannerid).order_by(Activity.start.asc()).all()
        if not desiredplanner: raise Exception('no such planner')
        if not listofactivity: raise Exception('no activity in planner')
        if user.id == desiredplanner.user_id:
            activityidlist = []
            activitynamelist = []
            activitystartdatetime = []
            activityenddatetime = []
            activityservicetypelist = []
            activitylocationidlist = []
            activitydescriptionlist = []
            for x in listofactivity:
                activityidlist.append(x.id)
                activitynamelist.append(x.name)
                activitystartdatetime.append(x.start)
                activityenddatetime.append(x.end)
                activityservicetypelist.append(x.serviceType_ID)
                activitylocationidlist.append(x.location_ID)
                activitydescriptionlist.append(x.description)
            return jsonify(
                dict(id=activityidlist,
                     name=activitynamelist,
                     startdatetime=activitystartdatetime,
                     enddatetime=activityenddatetime,
                     description=activitydescriptionlist,
                     servicetypeID=activityservicetypelist,
                     locationID=activitylocationidlist,
                     code=200))
        else:
            raise Exception('no such user')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 8
0
def create_planner():
    try:
        planner_name = request.json.get('planner_name')
        first_date = request.json.get('first_date')
        last_date = request.json.get('last_date')
        description = request.json.get('description')
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        friendlist = request.json.get('friendlist')
        print(friendlist)
        if not planner_name:
            raise Exception('planner_name cannot be empty')
        if not first_date:
            raise Exception('first_date cannot be empty')
        if not last_date:
            raise Exception('last_date cannot be empty')
        if not description:
            raise Exception('description cannot be empty')

        planner_object = Planner(
            name=planner_name,
            first_date=dt.datetime.strptime(first_date, '%Y-%m-%d').date(),
            last_date=dt.datetime.strptime(last_date, '%Y-%m-%d').date(),
            description=description,
            user_id=user.id)
        db.session.add(planner_object)
        db.session.commit()

        for i in friendlist:
            if (i['selected']):
                join = joinMemberPlannerActivity.Jointask(
                    member_ID=i['id'], planner_ID=planner_object.id)
                db.session.add(join)

        db.session.commit()

        return jsonify(dict(success=True, code=201))

    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 9
0
def delete_activity(planner_id, activity_id):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        user_id = user.id
        activity = Activity.query.filter_by(id=activity_id,
                                            planner_ID=planner_id)
        user_id_planner = Planner.query.filter_by(user_id=user_id).first()

        if user_id == user_id_planner.user_id:
            if not activity:
                raise Exception('activity has been deleted')

            activity.delete()
            db.session.commit()
            return jsonify(dict(success=True, code=201))
        else:
            raise Exception('no such activity in your planner')

    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 10
0
def edit_activity(planner_id, activity_id):
    try:
        activity_name = request.json.get('activity_name')
        start_datetime = request.json.get('start_datetime')
        end_datetime = request.json.get('end_datetime')
        description = request.json.get('description')
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)

        if not activity_name:
            raise Exception('activity_name cannot be empty')
        if not start_datetime:
            raise Exception('start_datetime cannot be empty')
        if not end_datetime:
            raise Exception('end_datetime cannot be empty')
        if not description:
            raise Exception('description cannot be empty')

        desiredplanner = Planner.query.filter_by(id=planner_id).one()
        desiredactiity = Activity.query.filter_by(id=activity_id).one()
        if user.id == desiredplanner.user_id:
            if desiredplanner.id == desiredactiity.planner_ID:
                start_datetime = dt.datetime.strptime(start_datetime,
                                                      '%Y-%m-%d').date(),
                end_datetime = dt.datetime.strptime(end_datetime,
                                                    '%Y-%m-%d').date(),
                Activity.query.filter(Activity.id == activity_id).\
                update({Activity.name: activity_name, Activity.start: start_datetime, Activity.end: end_datetime, Activity.description: description}, synchronize_session=False)
                db.session.commit()
                return jsonify(dict(success=True), 201)
            else:
                return jsonify(
                    dict(message='no such activity in your id')), 400
        else:
            raise Exception('no such user in your id')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e))), 400
Ejemplo n.º 11
0
def view_activity(planner_id, activity_id):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=planner_id).one()
        if user.id != desiredplanner.user_id:
            raise Exception('access denied')
        desiredactiity = Activity.query.filter_by(id=activity_id).one()
        if user.id == desiredplanner.user_id:
            if desiredplanner.id == desiredactiity.planner_ID:
                returnactivity = [
                    desiredactiity.id, desiredactiity.name,
                    desiredactiity.start, desiredactiity.end,
                    desiredactiity.description
                ]
                return jsonify(dict(activity=returnactivity, code=201))
            else:
                raise Exception('no such activity')
        else:
            raise Exception('no such user')
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 12
0
def view_all_planner():
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        plannerlist = Planner.query.filter_by(user_id=user.id).all()
        planneridlist = []
        plannernamelist = []
        plannerfirstdatelist = []
        plannerlastdatelist = []
        for x in plannerlist:
            planneridlist.append(x.id)
            plannernamelist.append(x.name)
            plannerfirstdatelist.append(x.first_date)
            plannerlastdatelist.append(x.last_date)
        return jsonify(
            dict(id=planneridlist,
                 name=plannernamelist,
                 startdate=plannerfirstdatelist,
                 enddate=plannerlastdatelist,
                 code=200))
    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 13
0
def create_member():
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        user_id = user.id
        firstname = request.json.get('firstname')
        lastname = request.json.get('lastname')
        dob = request.json.get('dob')
        gender = request.json.get('gender')

        if not firstname:
            raise Exception('firstname is empty')
        if not lastname:
            raise Exception('lastname is empty')
        if not dob:
            raise Exception('dob is empty ')
        if not gender:
            raise Exception('gender is empty')

        DoB = (dt.datetime.now()).year - int(dob)
        print(DoB)
        if (DoB < 0):
            raise Exception('please fill correct age')

        tocreateuserobject = Member(firstname=firstname,
                                    lastname=lastname,
                                    DoB=DoB,
                                    gender=gender,
                                    user_id=int(user_id))
        db.session.add(tocreateuserobject)
        db.session.commit()
        return jsonify(dict(success=True, code=201))

    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))
Ejemplo n.º 14
0
def create_activity(plannerid):
    try:
        jwttoken = request.headers.get('Authorization').split(' ')[1]
        user = jwt_auth.get_user_from_token(jwttoken)
        desiredplanner = Planner.query.filter_by(id=plannerid).one()
        if user.id != desiredplanner.user_id:
            raise Exception('access denied')
        activity_name = request.json.get('activity_name')
        start_date = request.json.get('start_date')
        start_time = request.json.get('start_time')
        end_date = request.json.get('end_date')
        end_time = request.json.get('end_time')
        description = request.json.get('description')
        atype = request.json.get('type')
        if not activity_name:
            raise Exception('activity_name cannot be empty')
        if not start_date:
            raise Exception('start_date cannot be empty')
        if not start_time:
            raise Exception('start_time cannot be empty')
        if not end_date:
            raise Exception('end_date cannot be empty')
        if not end_time:
            raise Exception('end_time cannot be empty')
        if not description:
            raise Exception('description cannot be empty')

        start_datetime = start_date + ' ' + start_time
        end_datetime = end_date + ' ' + end_time

        if atype == 1:
            start = request.json.get('start')
            stop = request.json.get('stop')
            if (start['name'] == "" or start['lat'] == ""
                    or start['lng'] == ""):
                raise Exception(
                    'Start location name, latitude or longtitude is missing')
            if (stop['name'] == "" or stop['lat'] == "" or stop['lng'] == ""):
                raise Exception(
                    'Stop location name, latitude or longtitude is missing')
            start_object = Location(name=start['name'],
                                    latitude=str(start['lat']),
                                    longtitude=str(start['lng']))
            stop_object = Location(name=stop['name'],
                                   latitude=str(stop['lat']),
                                   longtitude=str(stop['lng']))
            db.session.add(start_object)
            db.session.add(stop_object)
            db.session.commit()
            activity_object_stop = Activity(
                name=activity_name + " (end)",
                start=dt.datetime.strptime(end_datetime, '%Y-%m-%d %H:%M'),
                end=dt.datetime.strptime(end_datetime, '%Y-%m-%d %H:%M'),
                description=description,
                planner_ID=int(plannerid),
                serviceType_ID=int(atype),
                location_ID=stop_object.id)
            db.session.add(activity_object_stop)
            db.session.commit()
            activity_object_start = Activity(name=activity_name+" (start)",
            start=dt.datetime.strptime(start_datetime, '%Y-%m-%d %H:%M'),
            end=dt.datetime.strptime(start_datetime, '%Y-%m-%d %H:%M'),
            description=description, planner_ID=int(plannerid), serviceType_ID=int(atype),location_ID=start_object.id\
               , ref=activity_object_stop.id )
            db.session.add(activity_object_start)
            db.session.commit()
            return jsonify(dict(success=True, code=201))
        else:
            inl = request.json.get('in')
            if (inl['name'] == "" or inl['lat'] == "" or inl['lng'] == ""):
                raise Exception(
                    'Location name, latitude or longtitude is missing')
            in_object = Location(name=inl['name'],
                                 latitude=str(inl['lat']),
                                 longtitude=str(inl['lng']))
            db.session.add(in_object)
            db.session.commit()
            activity_object_in = Activity(
                name=activity_name,
                start=dt.datetime.strptime(start_datetime, '%Y-%m-%d %H:%M'),
                end=dt.datetime.strptime(end_datetime, '%Y-%m-%d %H:%M'),
                description=description,
                planner_ID=int(plannerid),
                serviceType_ID=int(atype),
                location_ID=in_object.id)
            db.session.add(activity_object_in)
            db.session.commit()
            return jsonify(dict(success=True, code=201))

    except Exception as e:
        db.session.rollback()
        return jsonify(dict(success=False, message=str(e), code=400))