def add(request): """Handels GET/POST adds the given word to the list of exclude list of the user. GET/POST parameters: u --- int token --- hash of user token and n word --- word to add to exclude list language --- language of the word """ track(request, 'add | excludeword | profile | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser word = None if 'language' in params and 'word' in params: language_code = params.pop('language')[-1] try: language = Language.objects.get(code=language_code) except Language.DoesNotExist: return HttpResponseNotFound('Language with "%s" not found.' % language_code) word_name = params.pop('word')[-1] try: word = Word.objects.get(name=word_name, language=language) except Word.DoesNotExist: return HttpResponseNotFound('Word with "%s" not found.' % word_name) else: return HttpResponseBadRequest('Required parameter "language" or "word" is missing.') excludeword, created = ExcludeWord.objects.get_or_create(word=word, user=autheduser.user) data = {'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z'), 'created':created} return HttpResponse(dumps(data), 'application/json')
def revoke(request): """Handels a POST/GET request to auth a user. GET/POST parameters: u --- int token --- hash of user token and n """ track(request, 'revoke | auth | applications | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser else: autheduser.delete() return HttpResponse(dumps({'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z')}), 'application/json')
def association(request): """Handels a POST or GET request to save a association. GET/POST parameters: language --- language of the word and association word --- word association --- association u --- int token --- hash of user token and n """ track(request, 'association | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser language = None if 'language' in params: try: language = Language.objects.get(code=params.pop('language')[-1]) except Language.DoesNotExist: return HttpResponseNotFound('Language with "language" not found.') else: return HttpResponseBadRequest('Required parameter "language" is missing.') word = None if 'word' in params: try: word = Word.objects.get(name=params.pop('word')[-1], language=language) except Word.DoesNotExist: return HttpResponseNotFound('Word with "word" and "language" not found.') else: return HttpResponseBadRequest('Required parameter "word" is missing.') word1 = None points = 0 if 'association' in params: word1, created = Word.objects.get_or_create(name=params.pop('association')[-1], language=language) association, created = Association.objects.update_or_create(word=word, association=word1) points = calculate_points(autheduser.user, association) else: return HttpResponseBadRequest('Required parameter "association" is missing.') data = {'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z'), 'points':points} return HttpResponse(dumps(data), 'application/json')
def lists(request): """Handels GET/POST lists all excluded words of the user. GET/POST parameters: u --- int token --- hash of user token and n """ track(request, 'lists | excludeword | profile | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser excluded_words = ExcludeWord.objects.filter(user=autheduser.user) data = {'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z'), 'excluded_words':[excluded_word.word.to_json(request, limit=0) for excluded_word in excluded_words]} return HttpResponse(dumps(data), 'application/json')
def associationhistory(request): """Handels GET/POST request to export the association history of the authed user. GET/POST parameters: u --- int token --- hash of user token and n """ track(request, 'associationhistory | profile | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser association_histories = AssociationHistory.objects.filter(user=autheduser.user) data = {'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z'), 'association_history':[{'association':association_history.association.to_json(request), 'points':association_history.points} for association_history in association_histories]} return HttpResponse(dumps(data), 'application/json')
def profile(request): """Handels GET/POST request to export a profile of the authed user. GET/POST parameters: u --- int token --- hash of user token and n """ track(request, 'profile | API | TIMA') params = request.POST.copy() if request.method == 'POST' else request.GET.copy() autheduser = check_authed_user(params) if isinstance(autheduser, HttpResponse): return autheduser profile = Profile.objects.get(user=autheduser.user) data = {'response_date':timezone.now().strftime('%Y-%m-%dT%H:%M:%S:%f%z'), 'profile':{'username':profile.user.username, 'points':profile.points, 'cultural_background': profile.cultural_background, 'first_name': profile.user.first_name, 'last_name': profile.user.last_name, 'email': profile.user.email,}} return HttpResponse(dumps(data), 'application/json')