Пример #1
0
    def post(self):
        """
        # post information for new event 
        # Parameters:
        #     - name: String, name of event
        #     - description: String, description of event
        #     - place: String, place of event
        #     - creator_id: Integer, creator's id of event
        # Response:
        #     - 415: create_event_error_response and alert "unsupported media type and Requests must be JSON"
        #     - 400: create_event_error_response and alert "Invalid JSON document" 
        #     - 409: create_event_error_response and alert "Already exists Event with id '{}' already exists."
        #     - 201: success to post
        """
        api = Api(current_app)
        if not request.json:
            return create_event_error_response(415, "Unsupported media type",
                                               "Requests must be JSON")

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

        user = User.query.filter_by(id=request.json["creatorId"]).first()

        event = Event(name=request.json["name"],
                      description=request.json["description"],
                      place=request.json["place"],
                      creator_id=request.json["creatorId"])

        event.creator = user

        events = Event.query.all()

        event.id = len(events) + 1

        try:

            body = InventoryBuilder()
            db.session.add(event)
            db.session.commit()
            print(api.url_for(EventItem, id=event.id))

            events = Event.query.all()

            event.id = len(events)

        except IntegrityError:
            return create_event_error_response(
                409, "Already exists",
                "Event with id '{}' already exists.".format(event.id))

        return Response(
            status=201,
            headers={"Location": api.url_for(EventItem, id=event.id)})
Пример #2
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)
Пример #3
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)
Пример #4
0
 def add_control_delete_event(self, id):
     from .resources.EventItem import EventItem
     api = Api(current_app)
     self.add_control("delete",
                      href=api.url_for(EventItem, id=id),
                      method="DELETE",
                      title="Delete this event")
Пример #5
0
 def add_control_delete_org(self, id):
     from .resources.OrgItem import OrgItem
     api = Api(current_app)
     self.add_control("delete",
                      href=api.url_for(OrgItem, id=id),
                      method="DELETE",
                      title="Delete this organization")
Пример #6
0
    def get(self):
        """
        Return information of all users (returns a Mason document) if found otherwise returns 404
        get all users information
        Response:
            - 200: Return information of all users (returns a Mason document)
        """
        api = Api(current_app)

        users = User.query.all()
        body = InventoryBuilder(items=[])

        for i in users:
            item = MasonBuilder(name=i.name,
                                email=i.email,
                                pwdhash=i.pwdhash,
                                location=i.location,
                                notifications=i.notifications)
            item.add_control("self", api.url_for(UserItem, id=i.id))
            item.add_control("profile", "/profiles/user/")
            body["items"].append(item)

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control_all_users()
        body.add_control_add_user()
        #print(body)

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #7
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)})
Пример #8
0
 def add_control_delete_user(self, id):
     from .resources.UserItem import UserItem
     api = Api(current_app)
     self.add_control("delete",
                      href=api.url_for(UserItem, id=id),
                      method="DELETE",
                      title="Delete this user")
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #12
0
 def add_control_edit_event(self, id):
     from .resources.EventItem import EventItem
     api = Api(current_app)
     self.add_control("edit",
                      href=api.url_for(EventItem, id=id),
                      method="Put",
                      encoding="json",
                      title="Edit a event",
                      schema=self.event_schema())
Пример #13
0
    def get(self):
        """
        # get information as follows
        # Information:
        #     - id: Integer, id of event
        #     - name: String, name of event
        #     - description: String, description of event
        #     - place: String, place of event
        #     - time: DataTime, time of event
        #     - creator_id: Integer, creator's id of event
        #     - join_users: users_id, Integer, id of user
        #                   users_name, String, name of user
        # Response:
        #     - 400: Found something else and get KeyError and ValueError
        #     - 200: Return information of all events (returns a Mason document)
        """
        api = Api(current_app)

        try:
            events = Event.query.all()
            body = InventoryBuilder(items=[])

            for j in events:
                if (j.creator != None):

                    creator_user = {}

                    creator_user["id"] = j.creator.id

                    creator_user["name"] = j.creator.name
                    joined_users = []
                    for i in j.joined_users:
                        user = {}
                        user["id"] = i.id
                        user["name"] = i.name
                        joined_users.append(user)

                    item = MasonBuilder(id=j.id,
                                        name=j.name,
                                        description=j.description,
                                        place=j.place,
                                        time=j.time,
                                        creator=creator_user,
                                        joined_users=joined_users)
                    item.add_control("self", api.url_for(EventItem, id=j.id))
                    item.add_control("profile", "/profiles/event/")
                    body["items"].append(item)

            body.add_namespace("eventhub", LINK_RELATIONS_URL)
            body.add_control_all_events()
            body.add_control_add_event()

            return Response(json.dumps(body), 200, mimetype=MASON)
        except (KeyError, ValueError):
            abort(400)
Пример #14
0
    def add_control_edit_org(self, id):
        from .resources.OrgItem import OrgItem

        api = Api(current_app)

        self.add_control("edit",
                         href=api.url_for(OrgItem, id=id),
                         method="Put",
                         encoding="json",
                         title="Edit an organization",
                         schema=self.org_schema())
Пример #15
0
    def get(self, id):
        """
        # get specific information for particular user
        # Parameters:
        #     - id: Integer, id of user
        # Information:
        #     - name: String, name of user
        #     - location: String, location of user
        #     - username: String, username of user
        #     - Created_events: - id: Integer, id of event
        #                       - name: String, name of event
        #                       - description: String, description of event
        #                       - place: String, place of event
        #                       - time: DataTime, time of event
        # Response:
        #     - 404: create_user_error_response and alert "Not found No user was found with the id {}"
        #     - 200: Return information of this user (returns a Mason document)
        """

        id = int(id)
        api = Api(current_app)
        User.query.all()
        db_user = User.query.filter_by(id=id).first()
        print(db_user)

        db_loginuser = LoginUser.query.filter_by(id=id).first()
        events = Event.query.filter_by(creator_id=id).all()
        events_list = []
        for j in events:
            item = {}
            item["id"] = j.id
            item["name"] = j.name
            item["description"] = j.description
            item["place"] = j.place
            item["time"] = j.time
            events_list.append(item)

        if db_user is None:
            return create_user_error_response(
                404, "Not found",
                "No user was found with the id {}".format(id))

        body = InventoryBuilder(id=db_user.id,
                                name=db_user.name,
                                location=db_user.location,
                                username=db_loginuser.username,
                                Created_events=events_list)
        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)
Пример #16
0
    def add_control_edit_user(self, id):
        from .resources.UserItem import UserItem

        api = Api(current_app)

        self.add_control("edit",
                         href=api.url_for(UserItem, id=id),
                         method="Put",
                         encoding="json",
                         title="Edit a user",
                         schema=self.user_schema())
Пример #17
0
    def post(self):
        """
        # post information for new user
        # Parameters:
        #     - username: String, username of user
        #     - name: String, name of user
        #     - password: String, password of user
        # Response:
        #     - 415: create_user_error_response and alert "Unsupported media type Requests must be JSON"
        #     - 400: create_user_error_response and alert "Invalid JSON document"
        #     - 409: create_user_error_response and alert "Already exists Product with handle '{}' already exists."
        #     - 201: success to post
        """
    
        api = Api(current_app)
        if not request.json:
            return create_user_error_response(415, "Unsupported media type",
                                               "Requests must be JSON"
                                               )
        try:
            validate(request.json, InventoryBuilder.user_schema())
        except ValidationError as e:
            return create_user_error_response(400, "Invalid JSON document", str(e))
        
        loginuser = LoginUser(username=request.json['username'])
        loginuser.password_hash = loginuser.generate_hash(request.json['password'])
        user = User(name=request.json['name'], location=request.json["location"])

        loginuser.user = user


        try:

            body = InventoryBuilder()
                    
            db.session.add(user)
            db.session.add(loginuser)
            db.session.commit()
        
        
            users = User.query.all()

            user.id = len(users)


        except IntegrityError:
            return create_user_error_response(409, "Already exists",
                                               "Product with handle '{}' already exists.".format(user.id)
                                               )
    
        return Response(status=201, headers={
            "Location": api.url_for(UserItem, id=user.id)
        })
Пример #18
0
    def get(self, id):
        """
        # get specific information for particular event 
        # Parameters:
        #     - id: Integer, id of event
        # Response:
        #     - 404: create_event_error_response and alert "Not found No event was found with the id {}"
        #     - 200: Return information of the event (returns a Mason document)
        # Information:
        #     - name: String, name of event
        #     - description: String, description of event
        #     - place: String, place of event
        #     - time: DataTime, time of event
        #     - creator_id: Integer, creator's id of event
        #     - join_users: users_id, Integer, id of user
        #                   users_name, String, name of user
        """
        api = Api(current_app)
        db_event = Event.query.filter_by(id=id).first()
        if db_event is None:
            return create_event_error_response(
                404, "Not found",
                "No event was found with the id {}".format(id))

        if db_event.creator is None:
            return create_event_error_response(
                404, "Not found",
                "No event was found with the id {}".format(id))

        joined_users = db_event.joined_users
        users = []
        for i in joined_users:
            user = {}
            user["name"] = i.name
            user["id"] = i.id
            item = MasonBuilder(id=i.id, name=i.name)
            users.append(item)
        body = InventoryBuilder(id=db_event.id,
                                name=db_event.name,
                                description=db_event.description,
                                place=db_event.place,
                                time=db_event.time,
                                joined_users=users)
        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)
Пример #19
0
    def put(self, id):
        """
        # modify specific information for particular event 
        # Parameters:
        #     - id: Integer, id of event
        #     - name: String, name of event
        #     - description: String, description of event
        #     - place: String, place of event
        #     - time: DataTime, time of event
        # Response:
        #     - 415: create_event_error_response and alert "Unsupported media type Requests must be JSON"
        #     - 404: create_event_error_response and alert "Not found No event was found with the id {}"
        #     - 400: create_event_error_response and alert "Invalid JSON document"
        #     - 204: success to edit
        """
        api = Api(current_app)
        if not request.json:
            return create_event_error_response(415, "Unsupported media type",
                                               "Requests must be JSON")

        event = Event(
            id=id,
            name=request.json["name"],
            place=request.json["place"],
            time=request.json["time"],
            description=request.json["description"],
        )

        db_event = Event.query.filter_by(id=id).first()
        if db_event is None:
            return create_event_error_response(
                404, "Not found",
                "No event was found with the id {}".format(id))

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

        db_event.id = event.id
        db_event.name = event.name
        db_event.place = event.place

        db_event.description = event.description
        db.session.commit()

        return Response(status=204,
                        headers={"Location": api.url_for(EventItem, id=id)})
Пример #20
0
    def get(self):
        """
        get information as follows
        Information:
            - 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:
            - 200: Return information of all events (as a Mason document)
        """
        api = Api(current_app)
        
        
        events = Event.query.all()
        body = InventoryBuilder(event_list=[])
        
        #add creator id
        for item in events:
            """
            if (item.creator_id != None):
                #create a dictionary
                creator = {}
                creator["id"] = item.creator_id

                creator_user = User.query.filter_by(id=item.creator_id).first
                creator_name = creator_user.name
                creator["name"] = creator_name
            """
            event = MasonBuilder(
                    name=item.name, 
                    time = item.time,
                    description=item.description, 
                    location = item.location, 
                    organization = item.organization,
                    #creator = item.creator
            )
            event.add_control("self", api.url_for(EventItem, id=item.id))
            event.add_control("profile", "/profiles/event/")
            body["event_list"].append(event)

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control_all_events()
        body.add_control_add_event()

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #21
0
    def delete(self, user_id, event_id):
        """
        # Delete the information of joined event 
        # Parameters:
        #     - user_id: Integer, id of user
        #     - event_id: Integer, id of event
        # Response:
        #     - 404: create_event_error_response and alert "Doesn't exists event with id '{}' doesn't exist."
        #     - 404: create_user_error_response and alert "Doesn't exists user with id '{}' doesn't exist."
        #     - 404: create_error_response and alert "Doesn't exists user with id '{}' doesn't exist in event with id '{}' 
        #     - 204: success to delete
        """
        api = Api(current_app)

        event = Event.query.filter_by(id=event_id).first()
        user = db.session.query(User).get(user_id)

        if user is None:
            return create_user_error_response(
                404, "Doesn't exists",
                "user with id '{}' doesn't exist.".format(user_id))

        if event is None:
            return create_event_error_response(
                404, "Doesn't exists",
                "event with id '{}' doesn't exist.".format(event_id))

        user_not_event = False

        for j in event.joined_users:
            if j.id == user_id:
                event.joined_users.remove(j)
                user_not_event = True

        if user_not_event:
            return create_error_response(
                404, "Doesn't exists",
                "user with id '{}' doesn't exist in event with id '{}'.".
                format(user_id, event_id))

        db.session.commit()

        return Response(
            status=204,
            headers={"Location": api.url_for(EventItem, id=event_id)})
Пример #22
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)})
Пример #23
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)})
Пример #24
0
    def put(self, user_id, event_id):
        """
        # modify the information of joining event 
        # Parameters:
        #     - user_id: Integer, id of user
        #     - event_id: Integer, id of event
        # Response:
        #     - 404: create_event_error_response and alert "Not found No event was found with the id {}"
        #     - 404: create_user_error_response and alert "Not found No user was found with the id {}"
        #     - 409: create_event_error_response and alert "Already exists event with id '{}' already exists."
        #     - 409: create_uesr_error_response and alert "Already exists user with id '{}' already exists."
        #     - 204: success to edit
        """
        api = Api(current_app)
        event = Event.query.filter_by(id=event_id).first()
        if event is None:
            return create_event_error_response(
                404, "Not found",
                "No event was found with the id {}".format(event_id))

        event = Event.query.filter_by(id=event_id).first()
        for i in event.joined_users:
            if i.id == int(user_id):
                return create_user_error_response(
                    409, "Already exists",
                    "user with id '{}' already exists.".format(i.id))
        user = User.query.filter_by(id=user_id).first()
        if user is None:
            return create_user_error_response(
                404, "Not found",
                "No user was found with the id {}".format(id))

        try:
            event.joined_users.append(user)
            db.session.commit()
        except IntegrityError:
            return create_user_error_response(
                409, "Already exists",
                "user with name '{}' already exists.".format(user.id))

        return Response(
            status=204,
            headers={"Location": api.url_for(EventsByUser, user_id=user_id)})
Пример #25
0
    def put(self, id):
        """
        # modify specific information for particular user
        # Parameters:
        #     - id: Integer, id of user
        #     - name: String, name of user
        #     - location: String, location of user
        # Response:
        #     - 415: create_user_error_response and alert "Unsupported media type Requests must be JSON"
        #     - 400: create_user_error_response and alert "Invalid JSON document"
        #     - 404: create_user_error_response and alert "Not found No user was found with the id {}"
        #     - 204: success to edit
        """
        api = Api(current_app)
        print(request.json)
        if not request.json:
            return create_user_error_response(415, "Unsupported media type",
                                              "Requests must be JSON")
        try:
            validate(request.json, InventoryBuilder.user_schema())
        except ValidationError as e:
            return create_user_error_response(400, "Invalid JSON document",
                                              str(e))

        user = User(name=request.json["name"],
                    location=request.json["location"])

        db_user = User.query.filter_by(id=id).first()
        if db_user is None:
            return create_user_error_response(
                404, "Not found",
                "No user was found with the id {}".format(id))

        db_user.id = id
        db_user.name = user.name
        db_user.location = user.location
        db.session.commit()

        return Response(status=204,
                        headers={"Location": api.url_for(UserItem, id=id)})
Пример #26
0
    def delete(self, id):
        """
        # delete specific information for particular user
        # Parameters:
        #     - id: Integer, id of user
        # Response:
        #     - 404: create_user_error_response and alert "Not found No user was found with the id {}"
        #     - 204: success to delete
        """
        #     '''
        #     user = User(
        #         id=request.json["id"]
        #     )

        #     loginUser = LoginUser(
        #         id=request.json["id"]
        #     )

        #     db_user = User.query.filter_by(id=id).first()
        #     loginUser = LoginUser.query.filter_by(id=id).first()
        #     '''
        api = Api(current_app)
        user = db.session.query(LoginUser).get(id)

        if user is None:
            return create_user_error_response(
                404, "Doesn't exists",
                "user with id '{}' doesn't exists.".format(id))

        #db.session.delete(db_user)
        #db.session.commit()

        print(user)

        db.session.delete(user)
        db.session.commit()

        return Response(
            status=204,
            headers={"Location": api.url_for(UserItem, id=user.id)})
Пример #27
0
    def get(self):
        """
        # Return information of all users (returns a Mason document) if found otherwise returns 404
        # get all users information
        # Information:
        #     - id: Interger, id of user
        #     - name: String, name of user
        #     - place: String, location of user
        #     - join_events: event_id, Integer, id of event
        #                    event_name, String, name of event
        # Response:
        #     - 400: Found something else and get KeyError and ValueError
        #     - 200: Return information of all users (returns a Mason document)
        """
        api = Api(current_app)

        try:
            users = User.query.all()
            body = InventoryBuilder(items=[])
            for j in users:
                events= []
                for i in j.joined_events:
                    event = {}
                    event["id"]=i.id
                    event["name"] = i.name
                    events.append(event)
                item = MasonBuilder(id=j.id, name=j.name, place=j.location, joined_events=events)
                item.add_control("self", api.url_for(
                    UserItem, id=j.id))
                item.add_control("profile", "/profiles/user/")
                body["items"].append(item)
            body.add_namespace("eventhub", LINK_RELATIONS_URL)
            body.add_control_all_users()
            body.add_control_add_user()
            print(body)

            return Response(json.dumps(body), 200, mimetype=MASON)
        except (KeyError, ValueError):
            abort(400)
Пример #28
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)
Пример #29
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)
Пример #30
0
    def get(self):
        """
        get information as follows
        Information:
            - name: name of the organization
        Response:
            - 200: Return information of all organizations as a Mason document
        """
        api = Api(current_app)

        orgs = Organization.query.all()
        body = InventoryBuilder(orgs_list=[])

        for item in orgs:
            org = MasonBuilder(name=item.name, )
            org.add_control("self", api.url_for(OrgItem, id=item.id))
            org.add_control("profile", "/profiles/org/")
            body["orgs_list"].append(org)

        body.add_namespace("eventhub", LINK_RELATIONS_URL)
        body.add_control_all_orgs()
        body.add_control_add_org()

        return Response(json.dumps(body), 200, mimetype=MASON)