Esempio n. 1
0
def get_course(workspace):
    if workspace not in WORKSPACE_CACHE:
        for course, _ in query("/api/list_courses", course=None):
            try:
                WORKSPACE_CACHE[query("/slack/workspace_name", course=course)] = course
            except KeyError:
                continue
    return WORKSPACE_CACHE[workspace]
Esempio n. 2
0
def get_course(workspace):
    if workspace not in WORKSPACE_CACHE:
        for course in CONFIG:
            try:
                WORKSPACE_CACHE[query("/slack/workspace_name",
                                      course=course)] = course
            except KeyError:
                continue
    return WORKSPACE_CACHE[workspace]
Esempio n. 3
0
    def _process(self):
        course_id = query("piazza/course_id", course=self._course, staff=True)

        self._posts = OrderedSet()
        for match in itertools.chain(
                re.finditer(SHORT_REGEX, self._message),
                re.finditer(LONG_REGEX.format(course_id), self._message),
        ):
            cid = int(match.group("cid"))
            fid_str = match.group("fid")
            full_cid = match.group("cid") + ("_f{}".format(fid_str)
                                             if fid_str else "")
            post = query("piazza/get_post",
                         course=self._course,
                         staff=True,
                         cid=cid)
            subject = post["history"][0]["subject"]
            content = post["history"][0]["content"]

            if fid_str:
                fid = int(fid_str)  # 1 indexed
                curr_id = 0
                for child in post["children"]:
                    if child["type"] != "followup":
                        continue
                    curr_id += 1
                    if fid == curr_id:
                        break
                else:
                    return
                content = child["subject"]

            subject = unescape(subject)
            content = unescape(re.sub("<[^<]+?>", "", content))
            url = "https://piazza.com/class/{}?cid={}".format(
                course_id, full_cid)

            self._posts.add(Post(subject, content, url, full_cid))
Esempio n. 4
0
    def register_course(course):
        if get_endpoint(course) not in get_staff_endpoints(app.remote):
            abort(403)

        with connect_db() as db:
            ret = db("SELECT * FROM bot_data WHERE course = (%s)",
                     [course]).fetchone()

        if ret:
            # course already setup
            return redirect(
                get_add_to_slack_link(
                    query("/slack/workspace_name", course=course)))
        else:
            return redirect(url_for("course_config", course=course))
Esempio n. 5
0
    def index():
        staff_endpoints = set(get_staff_endpoints(app.remote))
        active_courses = []
        for course, endpoint in query("/api/list_courses", course=None):
            if endpoint in staff_endpoints:
                active_courses.append(course)

        if len(active_courses) == 0:
            return (
                "You are not a member of staff in any course that uses this tool",
                401,
            )
        if len(active_courses) == 1:
            return redirect(
                url_for("register_course", course=active_courses[0]))

        options = "<p>".join(
            '<button formaction="register/{}">{}</button>'.format(
                course, course) for course in active_courses)

        return f"""
Esempio n. 6
0
 def responses(self):
     users = None
     for match in re.finditer(TAG_REGEX, self._message):
         course = match.group("course")
         try:
             if course == "heads" or course == "admins":
                 course = self._course
             admins = query("admins/list_admins", course=course)
         except:
             continue
         if not admins:
             continue
         users = users or requests.get("https://slack.com/api/users.list",
                                       params={
                                           "token": self._bot_token
                                       }).json()
         tags = []
         for member in users["members"]:
             for email, name in admins:
                 if member["profile"].get("email") == email or member[
                         "profile"].get("real_name_normalized") == name:
                     tags.append("<@{}>".format(member["id"]))
         yield "Admins for {}: {}".format(course, ", ".join(tags))
Esempio n. 7
0
def get_endpoint(course):
    return query("/api/{}/get_endpoint".format(course), course=None)