Skip to content

Instantly share code, notes, and snippets.

import copy
from rest_framework_json_api import serializers
from rest_framework_json_api.utils import get_included_serializers
from rest_framework.relations import ManyRelatedField
from django.core.exceptions import FieldDoesNotExist
class ModelSerializer(serializers.ModelSerializer):
extra_prefetches = {}
def get_queryset(self):
return super().get_queryset().filter(question__course__memberships__user=self.request.user)
# Hack to get around the JSONAPI renderer using the pk field for the id
# We do the same thing for PUT requests in the update method on the serializer
if self.request.method == 'GET':
confirmation.pk = confirmation.key
def get_queryset(self):
def filter_question(question_type=None, prefix='user__questions'):
kwargs = {
'{}__course'.format(prefix): models.F('course'),
}
if question_type:
kwargs['{}__{}__isnull'.format(prefix, question_type)] = False
return models.Q(**kwargs)
def filter_answer(question_type=None):
def get_queryset(self):
tables = {
'membership_table': Membership._meta.db_table,
'question_table': Question._meta.db_table,
'slide_question_table': SlideQuestion._meta.db_table,
'test_question_table': TestQuestion._meta.db_table,
'meta_question_table': MetaQuestion._meta.db_table,
'answer_table': Answer._meta.db_table,
'vote_table': Vote._meta.db_table,
'slide_question_link_table': SlideQuestionLink._meta.db_table,
class SerializerQuerysetMixin():
excluded_prefetches = []
@classmethod
def get_queryset(cls, request):
"""
This is the base queryset - will be use for resources that are even just referenced
"""
return cls.Meta.model.objects.all().order_by('id') # .distinct('id')
import re
from pathlib import Path
from lxml import html
import requests
DATA_DIR = Path('data')
BASE_URL = 'https://ocw.mit.edu'
const existsInSet = R.curry((set, value) => set.has(value));
// terms could be a promise
return resolve(this.terms)
// Clean input terms
.then(R.map(R.toLower))
.then(R.map(R.replace(/\s+/g, ' ')))
.then(R.map(R.trim))
// Split terms, but include the full term
.then(R.chain((term) => [term, ...term.split(' ')]))
def head(x):
return x[0]
def tail(x):
return x[1:]
def powerset(x):
if x == []:
return [[]]
else:
const tokeniseInput = function(inputString) {
const tokenise = function(tokens, currentToken, remaining) {
if (remaining.length === 0) {
return tokens;
}
const [currentChar, ...newRemaining] = remaining;
// Only include the current token if it is not empty
const tokensMaybeWithCurrent = function() {