Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MA-1882 adding course api to regex #11526

Merged
merged 3 commits into from
Feb 17, 2016

Conversation

sanfordstudent
Copy link
Contributor

@nasthagiri @jcdyer

Fixes https://openedx.atlassian.net/browse/MA-1882

This may not be the cleanest way to do this regex.

@nasthagiri
Copy link
Contributor

Sure. This works. Alternatively, you can search for and match on the last "courses/" that is found.

Please add a unit test.

@sanfordstudent
Copy link
Contributor Author

I realized this approach doesn't accommodate /blocks url's with mongo-style course id's-

/api/courses/v1/blocks/edx/maths/2020/......

yields a course id of v1/blocks/edx, which we certainly don't want.

I'm going to have to rework this.

@sanfordstudent
Copy link
Contributor Author

Now updated with more comprehensive tests and a regex that excludes /v*/blocks routes explicitly.

@nasthagiri
Copy link
Contributor

@mulby Can you take at a look at this PR? Is this what the analytics team intended for annotating events with course_ids? The URL matching seems a bit fragile to me. But I haven't thought of any better alternatives. What do you think?

@sanfordstudent
Copy link
Contributor Author

I'm going to try to generalize the regex to exclude any route that includes "v*/{}" where {} is not courses.

@mulby
Copy link
Contributor

mulby commented Feb 12, 2016

@nasthagiri - agreed it's very fragile, note that I don't think analytics is the only part of the system that relies on this. Also, I don't think this is the only regex of this kind in the system.

I'm not sure if there is a better way to do this? The only thing I can think of is to somehow run the django URL resolving -> view resolving engine on the URL and looking for an extracted course_id. If we were diligent about always naming the parameter "course_id" or "course_key" we could identify the view and look for parameters that had those names?

I'm not sure if there is a more idiomatic way to handle something like this in Django.

Note that the analytics system does provide a context manager that allows you to override this automagically parsed course_id, you just have to do something like this:

with tracker.context('my_ctx_override', {'course_id': real_course_id}):
    tracker.emit(...)

@sanfordstudent
Copy link
Contributor Author

jenkins run bokchoy

@sanfordstudent
Copy link
Contributor Author

jenkins run js

@@ -7,8 +7,8 @@
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locations import SlashSeparatedCourseKey


COURSE_REGEX = re.compile(r'^.*?/courses/{}'.format(settings.COURSE_ID_PATTERN))
# accommodates course api urls, excluding /blocks routes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update docstring since it's no longer /blocks specific.

@sanfordstudent sanfordstudent force-pushed the sstudent/MA-1882-update-api-regex branch from 12f0847 to a44a059 Compare February 16, 2016 14:01
@nasthagiri
Copy link
Contributor

👍

@sanfordstudent
Copy link
Contributor Author

adding @jcdyer for review.


COURSE_REGEX = re.compile(r'^.*?/courses/{}'.format(settings.COURSE_ID_PATTERN))
# accommodates course api urls, excluding any course api routes that do not fall under v*/courses, such as v1/blocks.
COURSE_REGEX = re.compile(r'^(.*?/courses/)+(?!v[0-9]+/[^/]+){}'.format(settings.COURSE_ID_PATTERN))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the + at the end of (.*?/courses/)+ intended to do? It looks like you're expecting .*?/courses/ to show up multiple times, but that seems weird. Maybe the comment should include some examples of URLs that we intend to match with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can show up multiple times, yes. The tests include URLs such as "/api/courses/v1/courses/course-v1:edX+maths+2020" that exhibit this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you already handle that case by specifying that you want to match /courses/ not followed by v[0-9]+, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

@sanfordstudent sanfordstudent force-pushed the sstudent/MA-1882-update-api-regex branch from a44a059 to c00257e Compare February 16, 2016 19:31
@jcdyer
Copy link
Contributor

jcdyer commented Feb 16, 2016

Other than deduplication of tests, 👍

@sanfordstudent sanfordstudent force-pushed the sstudent/MA-1882-update-api-regex branch from c00257e to a46e4d9 Compare February 16, 2016 22:27
@sanfordstudent sanfordstudent force-pushed the sstudent/MA-1882-update-api-regex branch from a46e4d9 to 45f8e52 Compare February 17, 2016 14:22
sanfordstudent pushed a commit that referenced this pull request Feb 17, 2016
@sanfordstudent sanfordstudent merged commit e4b59c3 into master Feb 17, 2016
@sanfordstudent sanfordstudent deleted the sstudent/MA-1882-update-api-regex branch February 17, 2016 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants