Tuesday, January 24, 2017

Doing query param request in Django

Typically, I want my API endpoint url to be flat like this:

mydomain.com/shit_api/v1/person/123

That url is pretty straight forward, it's trying to get a person with an ID of 123 from shit_api. But that's not always clear cut.

I've been working on a particular API that needed a pair of optional filters that we're a pain to work out in a regular expression. And this is where I found about Django's QueryDict object. With it I can write out API urls with params:

mydomain.com/shit_api/v1/something/?filter1=value1&filter2=value2

And on the urls and views, I can handle it as such:

#urls.py
urlpatterns = [
   url(r'^parts/$', SomethingView.as_view()), name='Something'),
]
...

#views.py
class SomethingView(View):
   def get(self, request, *args, *kwargs):
      fiter1_value = request.GET.get('filter1', 'default')
      fiter2_value = request.GET.get('filter2', 'default')

      #do more here

      return Response('some response')

The thing here is that request.GET.get() method having a default value. This make it that having it on the URL it isn't a problem. So call like:

mydomain.com/shit_api/v1/something/

Will still work.

Bonus since I don't have to deal with a convoluted regular expression. Yehey!

Friday, January 13, 2017

Tricks with Django URLs and knowing you suck doing RE

RE or regular expressions and I don't really see eye to eye. But as a programmer, I have to work with them. So it's awkward like your ex works in the same office as you. But lucky for us, we just stick to a couple of tricks to make it a bit palatable.

Numbers in URL

You'll need to use a \d+ in the pattern.

ex: (r’^status/(\d+)$’, ‘myapp.backbone_view’)

This would work on /api/status/1

Dealing with spaces

I found two ways to do this but then found out that they are the same: [\w|\W]+ AND .+

Yes that's a dot.

ex: (r’^status/([\w|\W]+)$’, ‘myapp.backbone_view’) OR
(r’^status/(.+)$’, ‘myapp.backbone_view’)

This would work on /api/status/dead on arrival

The browser will just replace the spaces with %20 and it should work.


Thursday, December 15, 2016

Back to teaching but part time mind you

So I'm just back to teaching part time at USTP which is formerly MUST. I've been asked to teach Object Oriented Programming. And this is what I've noticed.

Starting with the OOP syllabus, I noticed:
  • It's outdated as fuck. For example, it still mentions "Java applets". Yes, that's 1998 for you.
  • The syllabus reads like it's a Java 101 class instead of purely OOP class. 
Aside from the syllabus, the lab setup is wrong.
  • "Standalone Lab" which translates to "Computer Lab WITH NO Internet". Yes, we're back to 1998 again. All Java programming to date requires Internet access. Heck, ALL programming activity requires Internet access.
  • This assumption of using IDEs (Netbeans, in this case) at the start. I disagree with this. IDEs hide or automates the actual process of programming. Not understanding the actual process doesn't make for good devs or programmers.
This complaints are besides the point, what I really found was a wide, gaping disconnect on how OOP is seen in the industry and in academia. This could also be true for other IT related subjects.

We need to find a way to close the gap because if not we are condeming a generation of students, workers to "Hello sir, may I take your order?"

And that is just fucked up.