def learn_sentence(request): text = request.POST.get('text', False) structure = request.POST.get('structure', False) translation = request.POST.get('translation', False) pronunciations = request.POST.get('pronunciations', False) if not (text and structure and pronunciations): return HttpResponseBadRequest("Parameters 'text', 'structure' and 'pronunciations' not found or invalid") # convert to the DB storing format prs_dict = simplejson.loads(pronunciations) prs_string = string.join((u"%s:%s" % (k,v) for (k,v) in prs_dict.items()), ",") # if the sentence is already there, we delete it so we can add it again updated Sentence.objects.filter(text = text).delete() s = Sentence(user = request.user, text = text, structure = structure, translation = translation, learned_date = datetime.datetime.now(), kanji_pronunciations = prs_string) s.save() return HttpResponse()
def importdata(request): if len( request.FILES.keys() ) > 0: file = request.FILES[ request.FILES.keys()[0] ] lines = file.readlines() #TODO: validate file format # write known kanji known_kanji = lines[1].decode('utf-8') up = request.user.get_profile() up.known_kanji = known_kanji up.save() i = 4 # erase all existing sentences for the user Sentence.objects.filter(user__username = request.user.username).delete() # read sentences try: while True: date = dateutil.parser.parse( lines[i].decode('utf-8') ) text = lines[i+1].decode('utf-8').strip() structure = lines[i+2].decode('utf-8').strip() pronunciations = lines[i+3].decode('utf-8').strip() translation = lines[i+4].decode('utf-8').strip() s = Sentence(user = request.user, text = text, structure = structure, translation = translation, kanji_pronunciations = pronunciations, learned_date = date) s.save() i += 5 except IndexError: # finished processing the file pass return HttpResponse()