def delete(self, idx, idx1):
        leader_id = idx
        activity_id = idx1
        activity = Activity.objects(activity_id=activity_id).first()

        if activity.leader_id == leader_id:
            Activity.objects(activity_id=activity_id).delete()
            return jsonify("The activity is deleted")
        return jsonify("You do not have authority")
    def post(self, idx, idx1):  # Apply a new activity
        applicant_id = idx
        activity_id = idx1

        # Search if the applicanttion has already existed in
        #  MongoDB collection 'Application'
        applications_exist = Application.objects(applicant_id=applicant_id)
        for application_exist in applications_exist:
            if application_exist.activity_id == activity_id:
                return jsonify("You already applied", 400)

        # Obtain applicant's information from collection 'User'
        user = User.objects(use_id=applicant_id).first()
        applicant_name = user.username

        # Obtain activity information from collection 'Activity'
        activity = Activity.objects(activity_id=activity_id).first()
        activity_name = activity.activity_name

        apply_id = Application.objects.count()
        apply_id += 1
        application = Application(apply_id=apply_id,
                                  applicant_id=applicant_id,
                                  applicant_name=applicant_name,
                                  activity_id=activity_id,
                                  activity_name=activity_name,
                                  status=0)
        application.save()
        return jsonify("Successfully apply", 200)
    def get(self):
        serachByLeader = request.form.get("leader")
        serachByActivity = request.form.get("activity")
        searchString = request.form.get("search")

        if serachByLeader:
            activities = Activity.objects(leader_name__startswith=searchString)
            #return jsonify(activities)
            return jsonify(activities)

        if serachByActivity:
            activities = Activity.objects(
                activity_name__startswith=searchString)

            return jsonify(activities)

        return jsonify(Activity.objects.all())
    def put(self, idx, idx1):
        use_id = idx
        activity_id = int(idx1)
        activity = Activity.objects(activity_id=activity_id).first()
        if activity.leader_name == use_id:
            activity_id = idx1
            activity_name = request.form.get("activity")
            start_date_str = request.form.get("start")
            skills = request.form.get("skills")
            genre = request.form.get("genre")
            description = request.form.get("description")

            if not start_date_str:
                Activity.objects(activity_id=activity_id).update(
                    activity_name=activity_name,
                    genre=genre,
                    skills=skills,
                    description=description)

                return jsonify(Activity.objects(activity_id=activity_id))

            start_date_datetime = datetime.strptime(start_date_str,
                                                    '%Y-%m-%d %H:%M:%S')
            Activity.objects(activity_id=activity_id).update(
                activity_name=activity_name,
                start_date=start_date_datetime,
                genre=genre,
                skills=skills,
                description=description)

            return add_cors(jsonify(Activity.objects(activity_id=activity_id)))
        return jsonify("You do not have the authority")
    def post(self, idx):
        # activity_id is useful when all the activiteis to be shown ==>To be confirm
        use_id = idx
        user = User.objects(use_id=use_id).first()

        leader_id = user.user_id

        activities = Activity.objects.all()
        n = len(activities)
        activity_id = 0
        if n != 0:
            activity_id = activities[n - 1].activity_id
            activity_id += 1

        activity_name = request.form.get("activity")
        start_date_str = request.form.get("start")
        skills = request.form.get("skills")
        genre = request.form.get("genre")
        description = request.form.get("description")

        start_date_datetime = datetime.strptime(start_date_str,
                                                '%Y-%m-%d %H:%M:%S')

        #check if there is the same activity name
        isPresent = Activity.objects(activity_name=activity_name).first()
        if isPresent:
            return jsonify("Please select another name")

        activity = Activity(leader_id=leader_id,
                            activity_id=activity_id,
                            activity_name=activity_name,
                            leader_name=username,
                            open_date=datetime.utcnow(),
                            start_date=start_date_datetime,
                            genre=genre,
                            skills=skills,
                            description=description)
        activity.save()
        return (jsonify("Successfully build a new activity"))
    def post(self, idx):
        # The activities the user attended
        user_id = idx  # idx = user_id
        applications = Application.objects(applicant_id=user_id)
        apply_activities = []

        if applications:
            for application in applications:
                activity_id = application.activity_id
                activity_id = activity_id
                status = application.status

                activity = Activity.objects(activity_id=activity_id).first()

                activity_name = activity.activity_name
                open_date = activity.open_date
                genre = activity.genre
                leader_name = activity.leader_name

                open_date_local = time.local(open_date)
                format_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                            open_date_local)

                apply_activity_dict = {
                    "Project_Name": activity_name,
                    "Deadline": open_date,
                    "Deadline": format_time,
                    "Major": genre,
                    "Leader": leader_name,
                    "Application Status": status
                }

                apply_activities.append(apply_activity_dict)

            return jsonify(apply_activities, 200)

        return jsonify("You don't have any applied acticities", 400)
    def get(self, idx):  # idx = user_id
        user_id = idx
        activities = Activity.objects(leader_id=user_id).all()
        create_activities = []

        if activities:
            for activity in activities:

                activity_name = activity.activity_name
                applications = Application.objects(
                    activity_name=activity_name).all()
                application_list_zero = []
                application_list_one = []
                for application in applications:
                    if application.status == 0:
                        application_list_zero.append(application.applicant_id)
                    elif application.status == 1:
                        application_list_one.append(application.applicant_id)
                activity_id = activity.activity_id
                activity_name = activity.activity_name
                open_date = activity.open_date
                genre = activity.genre

                # '0': the application has not been decided by the leader
                # '1': the application is successful
                create_activity_dict = {
                    "Project_Name": activity_name,
                    "Deadline": open_date,
                    "Major": genre,
                    "0": application_list_zero,
                    "1": application_list_one
                }

                create_activities.append(create_activity_dict)
            return jsonify(create_activities, 200)

        return add_cors(jsonify("You do not have any created acticities", 400))
 def get(self, idx, idx1):
     return jsonify(Activity.objects(activity_id=idx1))
示例#9
0
 def delete(self, idx):
     Activity.objects(act_id=idx).delete()
     return jsonify( message='Activity was successfully deleted!')
示例#10
0
 def get(self, idx: int):
     return jsonify(Activity.objects(act_id=idx))