2016-07-19 5 views
0

사용자로부터 많은 입력이 인 데이터 입력을위한 클래스가 있습니다. 나는 그것을 가능한 DB에 넣는 과정을 반자동으로 자동화하는데 사용한다.이 클래스를 장고에 넣을 위치는 어디입니까?

내 본능은 내 모델 클래스에 넣고 테스트를 작성하는 것이지만 매우 미숙 한 작업 일 것입니다. 그리고 나는 많은 방법을 모르고있는 raw_input() 함수와 논리 루프를 가지고 있습니다. 테스트 또는해야 할 일.

이 모듈을 별도로 유지하거나 모델 클래스에 포함시켜야합니까?

def define(self, word=False, word_pk=False): 
    '''Defining a word, there may be language specific elements to edit in here''' 
    try: 
     if word_pk: 
      word = Word.objects.get(id=word_pk) 
     else: 
      word = Word.objects.get(language__name=self.language_ISO, name=word) 
    except: 
     return "Word lookup failed for word=%s word_pk=%s\n" % (word, word_pk) 

    print "\n\tThe Word is: '%s'...\n" % (word) 
    wiktionary_list = word.wiktionary_lookup(self.wiktionary_prefix, self.driver) 
    wn_tuple = word.wn_lookup() 
    while choice("Would you like to add a/another definition for '%s'?: " % word): 
     #Ask the user if they want to use the wn output for definitions, make them select which ones 
     if choice("Would you like to choose a wordnet definition?: "): 
      chosen_defs = ask_input("Which ones? (choose all that apply with a space between numbers): ") 
      chosen_defs = [int(i) for i in (chosen_defs.split())] 
      #Wornet only gives part of speech and definition information so I need to split that here. 
      for i in chosen_defs: 
       #Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere 
       repeat = True 
       while repeat: 
        tup = wn_tuple[i] 
        print "\n(%s) - %s\n" % (tup[0], tup[1]) 
        audio_tup = self.add_audio(word) 
        picture_tup = self.add_picture(word) 

        new_definition = Definition() 
        new_definition.word=word 
        new_definition.part_speech= tup[0] 
        new_definition.definition=tup[1] 
        new_definition.def_source="Wordnet" 
        new_definition.add_pronunciation() 
        new_definition.word_audio=audio_tup[0] 
        new_definition.audio_source=audio_tup[1] 
        new_definition.picture=picture_tup[0] 
        new_definition.pic_source=picture_tup[1] 

        repeat = self.print_n_save(new_definition) 

     elif choice("Would you like to choose a wiktionary definition?: "): 

      choose_defs = ask_input("Which ones would you like to choose? (Numbers separated by spaces): ") 
      chosen_defs = [int(i) for i in choose_defs.split()] 

      for i in chosen_defs: 
       #Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere 
       repeat = True 
       while repeat: 
        print "\n%s\n" % (wiktionary_list[i]) 
        audio_tup = self.add_audio(word) 
        picture_tup = self.add_picture(word) 

        new_definition = Definition() 
        new_definition.word=word 
        new_definition.get_pos() 
        new_definition.definition=wiktionary_list[i] 
        new_definition.def_source="Wiktionary" 
        new_definition.add_pronunciation() 
        new_definition.word_audio=audio_tup[0] 
        new_definition.audio_source=audio_tup[1] 
        new_definition.picture=picture_tup[0] 
        new_definition.pic_source=picture_tup[1] 

        repeat = self.print_n_save(new_definition) 

     else: 
      #Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere 
      repeat = True 
      while repeat: 
       #Asking for definition, inputting raw from some internet source 
       definition = ask_input("What is the definition?: ") 
       definition_source = ask_input("What is the source of the definition?: ") 
       audio_tup = self.add_audio(word) 
       picture_tup = self.add_picture(word) 

       new_definition = Definition() 
       new_definition.word=word 
       new_definition.get_pos() 
       new_definition.definition=definition 
       new_definition.def_source=definition_source 
       new_definition.add_pronunciation() 
       new_definition.word_audio=audio_tup[0] 
       new_definition.audio_source=audio_tup[1] 
       new_definition.picture=picture_tup[0] 
       new_definition.pic_source=picture_tup[1] 

       repeat = self.print_n_save(new_definition) 
+0

코드를 입력하십시오. –

+0

사용자 정의 [shell 명령] (https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/)처럼 보입니다. – Todor

+0

@ SeanFrancisN.Ballais 다음은 단어 개체를 정의하는 한 가지 기능의 예입니다. – deltaskelta

답변

2

원시 파이썬 함수를 단일 상자로 강제 변환하지 마십시오. 당신의 (a 오래 시간 전) 했어야 무엇

, 물건을 테스트하고 파악하기 쉬울 것 때문에 별도의 함수로 그것을 밖으로 분리입니다.

사용자 입력을 요구하기 때문에 웹 사이트는 양식을 통해 양식을 작성하므로 양식 또는 양식 마법사/세트/기타가 필요합니다.

그 양식은 그것을 처리하기 위해 적어도 하나의보기가 필요할 것이므로이를 작성하거나 일반보기를 사용해야 할 수도 있습니다. 알고

는 모델도

+0

이 DB를 더 많이 관리하는 것을 볼 수 있습니다. 사이트 사용자는이 태그를 직접 사용하지 않을 것입니다 ... – deltaskelta

+1

실제로 이것이 폼이있는 뷰에 속한다는 것을 깨닫게되었습니다 – deltaskelta

2

내가 management/commands이 점을 둘 것입니다 (난 정말 코드를 읽어 보지 않았) 뭔가 후 처리를 할 필요가 있습니다. 기능을 BaseCommand 클래스로 바꾸면 잘 할 수 있습니다. 그리고 여기에 how to make testing입니다.

관련 문제