Esempio n. 1
0
    def post(self):
        """
        post information for new organization
        Parameters:
            - name: String, name of the organization
        Response:
            - 415: create_error_response and alert "Unsupported media type. Requests must be JSON"
            - 400: create_error_response and alert "Invalid JSON document" 
            - 409: create_error_response and alert "The organization already exists" 
            - 201: succeed to post
            
        """
        api = Api(current_app)
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, InventoryBuilder.org_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        org = Organization(name=request.json["name"])

        organizations = Organization.query.all()
        try:
            db.session.add(org)
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists",
                                         "The organization already exists")

        return Response(status=201,
                        headers={"Location": api.url_for(OrgItem, id=org.id)})
Esempio n. 2
0
    def put(self, id):
        """
        modify information for particular user
        Parameters:
            - id: Integer, id of user
            - name: String, name of user
            - email: String, email of user
            - password: String, password of user
            - location: String, location of user
            - notifications: Integer, whether the user choose to receive notifications or not
        Response:
            - 415: create_user_error_response and message "Unsupported media type Requests must be JSON"
            - 400: create_user_error_response and message "Invalid JSON document"
            - 404: create_user_error_response and message "User not found" "User ID {} not found."
            - 409: create_user_error_response and message "Already exists","The email address {} is already in use."
            - 204: success to edit
        """
        api = Api(current_app)
        #print(request.json)
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON"
                                         )
        try:
            validate(request.json, InventoryBuilder.user_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        password = request.json["password"]
        user = User(
            name=request.json["name"],
            email=request.json["email"],
            pwdhash=hash_password(password),
            location=request.json["location"],
            notifications=request.json["notifications"]
        )

        user_db = User.query.filter_by(id=id).first()
        if user_db is None:
            return create_error_response(404, "User not found",
                                         "User ID {} was not found".format(id)
                                         )

        try:        
            user_db.name = user.name
            user_db.email = user.email
            user_db.pwdhash = user.pwdhash
            user_db.location = user.location
            user_db.notifications = user.notifications
            db.session.commit()

        except IntegrityError:
            return create_error_response(409, "Already exists",
                                               "The user already exists.")
        

        return Response(status=204)
Esempio n. 3
0
    def put(self, id):
        """
        modify information of a event
        Parameters:
            - id: Integer, id of event
            - name: String, name of event
            - time: DateTime, time of event
            - description: String, description of event
            - location: String, location of event
            - organization: string, organization that the event belongs to
        Response:
            - 415: create_error_response and alert "Unsupported media type Requests should be JSON"
            - 404: create_error_response and alert "Not found No event was found with the id {}"
            - 400: create_error_response and alert "Invalid JSON document"
            - 204: success to edit
        """
        api = Api(current_app)
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON"
                                         )

        body = Event(
            name=request.json["name"],
            time=request.json["time"],
            description=request.json["description"],
            location=request.json["location"],
            organization=request.json["organization"],
        )

        event_db = Event.query.filter_by(id=id).first()
        if event_db is None:
            return create_error_response(404, "Event not found",
                                         "Event ID {} was not found".format(
                                             id)
                                         )

        try:
            validate(request.json, InventoryBuilder.event_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        event_db.name = body.name
        event_db.time = body.time
        event_db.location = body.location
        event_db.description = body.description
        event_db.organization = body.organization
        db.session.commit()

        return Response(status=204)
Esempio n. 4
0
 def get(self, id):
     """
     get information for one user
     Parameters:
         - id: Integer, id of user
     Response:
         - 404: create_error_response and message "User not found" "User ID {} not found."
         - 200: Return the user's information (a Mason document).
     """
     
     #id = int(id)
     api = Api(current_app)
     #User.query.all()
     user_db = User.query.filter_by(id=id).first()
     #print(user_db)
         
     if user_db is None:
         return create_error_response(404, "User not found",
                                      "User ID {} was not found".format(id)
                                      )
                                     
     body = InventoryBuilder(
         name=user_db.name,
         email=user_db.email,
         location=user_db.location,
         notifications=user_db.notifications
     )
     body.add_namespace("eventhub", LINK_RELATIONS_URL)
     body.add_control("self", api.url_for(UserItem, id=id))
     body.add_control("profile", USER_PROFILE)
     body.add_control_delete_user(id)
     body.add_control_edit_user(id)
     body.add_control_all_users()
     return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 5
0
    def delete(self, id):
        """
        delete user's informtation
        Parameters:
            - id: Integer, user id
        Response:
            - 404: create_error_response and send message "User not found" "User ID {} not found."
            - 204: delete successfully
        """
       
        api = Api(current_app)
        user_db = User.query.filter_by(id=id).first()


        if user_db is None:
            return create_error_response(404, "User not found",
                                         "User ID {} was not found".format(id)
                                         )

        #print(user)

        db.session.delete(user_db)
        db.session.commit()
    
        return Response(status=204)
Esempio n. 6
0
    def post(self):
        """
        post information for new event 
        Parameters:
            - name: String, name of the event
            - time: String, time of the event
            - description: String, description of event
            - location: String, location of the event
            - organization: Integer, organization that the event belongs to
            - creator_id: Integer, creator's id of the event
        Response:
            - 415: create_error_response and alert "Unsupported media type. Requests must be JSON"
            - 400: create_error_response and alert "Invalid JSON document" 
            - 201: success to post
        """
        api = Api(current_app)
        if not request.json:
            return create_error_response(415, "Unsupported media type","Requests must be JSON")

        try:
            validate(request.json, InventoryBuilder.event_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        
        # user = User.query.filter_by(id=request.json["creator_id"]).first()
      
        event = Event(
            name = request.json["name"],
            time = request.json["time"],
            description = request.json["description"],
            location = request.json["location"],
            organization = request.json["organization"],
            #creator_id = request.json["creator_id"]
        )

        #event.creator = user
        
        db.session.add(event)
        db.session.commit()

        #print(api.url_for(EventItem, id=event.id))

    
        return Response(status=201, headers={"Location": api.url_for(EventItem, id=event.id)})
Esempio n. 7
0
    def post(self):
        """
        post information for new user
        Parameters:
            - name: String, user's name
            - email: String, user's email
            - password: String, user's password
            - location: String, user's location
            - notifications: String, whether user turn on notification
        Response:
            - 415: "Unsupported media type" "Requests must be JSON"
            - 400: "Invalid JSON document"
            - 409: "Already exists" "The email address is already in use."
            - 201: succeed
        """

        api = Api(current_app)
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, InventoryBuilder.user_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        password = request.json["password"]
        user = User(name=request.json['name'],
                    email=request.json['email'],
                    pwdhash=hash_password(password),
                    location=request.json["location"],
                    notifications=request.json["notifications"])

        try:
            db.session.add(user)
            db.session.commit()

        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "The email address {} is already in use.".format(user.email))

        return Response(
            status=201,
            headers={"Location": api.url_for(UserItem, id=user.id)})
Esempio n. 8
0
    def put(self, id):
        """
        edit information of an organization
        Parameters:
            - id: Integer, id of the organization
            - name: String, name of the organization
        Response:
            - 415: create_error_response and message "Unsupported media type Requests should be JSON"
            - 404: create_error_response and message "No organization was found with the id {}"
            - 400: create_error_response and message "Invalid JSON document"
            - 409: create_error_response and alert "The organization already exists" 
            - 204: success to edit
        """
        api = Api(current_app)
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, InventoryBuilder.org_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        org = Organization(name=request.json["name"])

        org_db = Organization.query.filter_by(id=id).first()
        if org_db is None:
            return create_error_response(
                404, "Not found",
                "No organization was found with the id {}".format(id))

        try:
            org_db.name = org.name
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists",
                                         "The organization already exists")

        return Response(status=204)
Esempio n. 9
0
    def get(self, user_id):
        """
        Get all the assotiated organization info for a user
        Parameters:
            - id: Integer, user id
        Response:
            - 404: "User not found", "User ID {} was not found"
            - 200: Return the events information
        """
        api = Api(current_app)
        body = InventoryBuilder(items=[])
        user = User.query.filter_by(id=user_id).first()
        if user is None:
            return create_error_response(
                404, "Not found", "User ID {} was not found".format(user_id))
        body["user"] = {"name": user.name}

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(UserItem, id=user_id))
        body.add_control("profile", USER_PROFILE)
        body.add_control_delete_user(user_id)
        body.add_control_edit_user(user_id)
        body.add_control_all_users()

        # find all the organizations
        # orgs= db.session.query(User.related_orgs).filter_by(id=user_id).all()
        # orgs = User.query.with_entities(User.related_orgs).all()
        # orgs = associations.organization.query.filter_by(user=user_id).all()
        """
        users_dt = User.query.filter_by(id=user_id).first()
        orgs = users_dt.related_orgs
        """
        orgs = Organization.query.filter(
            Organization.users2.any(user_id=user_id)).all()
        # orgs = db.session.query(orgs_info.id)
        # for each organization, find specific information
        for i in orgs:
            org = InventoryBuilder()
            #org_dt = Organization.query.filter_by(id=i).first()
            org["name"] = i.name
            #org["name"] = organization.name
            org.add_namespace("eventhub", LINK_RELATIONS_URL)
            org.add_control("self", api.url_for(OrgItem, id=i.id))
            org.add_control("profile", ORG_PROFILE)
            org.add_control_delete_org(i.id)
            org.add_control_edit_org(i.id)
            org.add_control_all_orgs()
            body["items"].append(org)
        return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 10
0
    def get(self, org_id):
        """
        Get all the users' email of an organization
        Parameters:
            - id: Integer, organization id
        Response:
            - 404: "Organization not found", "Organization ID {} was not found"
            - 200: Return the users' email addresses
        """
        api = Api(current_app)
        body = InventoryBuilder(items=[])
        org = Organization.query.filter_by(id=org_id).first()
        if org is None:
            return create_error_response(
                404, "Organization not found",
                "Organization ID {} was not found".format(org_id))
        body["organization"] = {"org_id": org.id, "name": org.name}

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(OrgItem, id=org_id))
        body.add_control("profile", ORG_PROFILE)
        body.add_control_delete_org(org_id)
        body.add_control_edit_org(org_id)
        body.add_control_all_orgs()

        # find all the users
        #users= db.session.query(Organization.users2).filter_by(id=org_id).all()
        related_users = User.query.filter(User.orgs.any(org_id=org.id)).all()
        # for each user, find the id and email
        for i in related_users:
            user = InventoryBuilder()
            # user details
            user["name"] = i.name
            user["email"] = i.email
            user["pwdhash"] = i.pwdhash
            user["location"] = i.location
            user["notifications"] = i.notifications

            user.add_namespace("eventhub", LINK_RELATIONS_URL)
            user.add_control("self", api.url_for(UserItem, id=i.id))
            user.add_control("profile", USER_PROFILE)
            user.add_control_delete_user(i)
            user.add_control_edit_user(i)
            user.add_control_all_users()
            body["items"].append(user)

        return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 11
0
 def delete(self, id):
     """
     # delete information of an organization
     # Parameters:
     #     -id: Integer, id of the event
     # Response:
     #     -404: create_error_response and alert "Organization not found"
     #     -204: success to delete
     """
     api = Api(current_app)
     org_db = Organization.query.filter_by(id=id).first()
     if org_db is None:
         return create_error_response(404, "Not found",
                                      "Organization not found")
     db.session.delete(org_db)
     db.session.commit()
     return Response(status=204)
Esempio n. 12
0
    def get(self, event_id):
        """
        Get all the users' details for a event
        Parameters:
            - id: Integer, event id
        Response:
            - 404: "Event not found", "Event ID {} was not found"
            - 200: Return the users' email addresses
        """
        api = Api(current_app)
        body = InventoryBuilder(items=[])
        event = Event.query.filter_by(id=event_id).first()
        if event is None:
            return create_error_response(
                404, "Event not found",
                "Event ID {} was not found".format(event_id))
        body["event"] = {"event_id": event.id, "name": event.name}

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(EventItem, id=event_id))
        body.add_control("profile", EVENT_PROFILE)
        body.add_control_delete_event(event_id)
        body.add_control_edit_event(event_id)
        body.add_control_all_events()

        # find all the users
        followers = User.query.filter(User.events.any(event_id=event.id)).all()
        # for each user, find the id and email
        for i in followers:
            user = InventoryBuilder()
            user["name"] = i.name
            user["email"] = i.email
            user["pwdhash"] = i.pwdhash
            user["location"] = i.location
            user["notifications"] = i.notifications

            user.add_namespace("eventhub", LINK_RELATIONS_URL)
            user.add_control("self", api.url_for(UserItem, id=i.id))
            user.add_control("profile", USER_PROFILE)
            user.add_control_delete_user(i.id)
            user.add_control_edit_user(i.id)
            user.add_control_all_users()
            body["items"].append(user)

        return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 13
0
    def get(self, user_id):
        """
        Get all the events information for a user
        Parameters:
            - id: Integer, user id
        Response:
            - 404: "User not found", "User ID {} was not found"
            - 200: Return the events information
        """
        api = Api(current_app)
        body = InventoryBuilder(items=[])
        user = User.query.filter_by(id=user_id).first()
        if user is None:
            return create_error_response(
                404, "Not found", "User ID {} was not found".format(user_id))
        body["user"] = {"user_id": user.id, "name": user.name}

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(UserItem, id=user_id))
        body.add_control("profile", USER_PROFILE)
        body.add_control_delete_user(user_id)
        body.add_control_edit_user(user_id)
        body.add_control_all_users()

        # find all the events
        followed_events = Event.query.filter(
            Event.users1.any(user_id=user_id)).all()
        # for each event, find specific information
        for i in followed_events:
            event = InventoryBuilder()
            event["name"] = i.name
            event["time"] = i.time
            event["description"] = i.description
            event["location"] = i.location
            event["organization"] = i.organization

            event.add_namespace("eventhub", LINK_RELATIONS_URL)
            event.add_control("self", api.url_for(EventItem, id=i.id))
            event.add_control("profile", EVENT_PROFILE)
            event.add_control_delete_event(i.id)
            event.add_control_edit_event(i.id)
            event.add_control_all_events()
            body["items"].append(event)

        return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 14
0
 def delete(self,id):
     """
     delete information of an event
     Parameters:
         -id: Integer, id of the event
     Response:
         -404: create_error_response and alert "Event not found"
         -204: success to delete
     """
     api = Api(current_app)
     event_db = Event.query.filter_by(id=id).first()
     if event_db is None:
             return create_error_response(404, "Event not found",
                                      "Event ID {} was not found".format(
                                          id)
                                      )
     Event.query.filter(Event.id==id).delete()
     db.session.commit()
     return Response(status = 204)
Esempio n. 15
0
 def get(self, id):
     """
     get details for particular event 
     Parameters:
         - id: Integer, event ID
     Response:(tbc)
         - 404: create_error_response and alert "No event was found with the id {}"
         - 200: Return information of the event (returns a Mason document)
     """
     api = Api(current_app)
     event_db = Event.query.filter_by(id=id).first()
     if event_db is None:
         return create_error_response(404, "Event not found",
                                      "Event ID {} was not found".format(
                                          id)
                                      )
     """
     if event_db.creator is None:
         return create_error_response(404, "Event not found",
                                      "Event ID {} was not found".format(
                                          id)
                                      )
     """
     body = InventoryBuilder(
         name=event_db.name,
         time=event_db.time,
         description=event_db.description,
         location=event_db.location,
         organization = event_db.organization
     )
     
     body.add_namespace("eventhub", LINK_RELATIONS_URL)
     body.add_control("self", api.url_for(EventItem, id=id))
     body.add_control("profile", EVENT_PROFILE)
     body.add_control_delete_event(id)
     body.add_control_edit_event(id)
     body.add_control_all_events()
     return Response(json.dumps(body), 200, mimetype=MASON)
Esempio n. 16
0
    def get(self, id):
        """
        get details for particular organization
        Parameters:
            - id: Integer, event ID
        Response:(tbc)
            - 404: create_error_response and message "No organization was found with the id {}"
            - 200: Return information of the organization (returns a Mason document)
        """
        api = Api(current_app)
        org_db = Organization.query.filter_by(id=id).first()
        if org_db is None:
            return create_error_response(
                404, "Not found",
                "No organization was found with the id {}".format(id))

        body = InventoryBuilder(name=org_db.name)
        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(OrgItem, id=id))
        body.add_control("profile", ORG_PROFILE)
        body.add_control_delete_org(id)
        body.add_control_edit_org(id)
        body.add_control_all_orgs()
        return Response(json.dumps(body), 200, mimetype=MASON)