Exemple #1
0
def blog_create(**new_blog_data):

    new_blog = Blog()

    new_blog.site = new_blog_data['site'].id
    new_blog.name = new_blog_data['name']
    new_blog.description = new_blog_data['description']
    new_blog.url = new_blog_data['url']
    new_blog.path = new_blog_data['path']
    new_blog.local_path = new_blog.path
    new_blog.theme = new_blog_data['theme']

    new_blog.save()

    new_blog_default_category = Category(blog=new_blog,
                                         title="Uncategorized",
                                         default=True)

    new_blog_default_category.save()

    # template installation should be its own function
    # install whatever the currently set system default templates are

    user = user_from_ka(**new_blog_data)

    logger.info("Blog {} created on site {} by user {}.".format(
        new_blog.for_log, new_blog.site.for_log, user.for_log))

    return new_blog
Exemple #2
0
def migrate_categories():
    f = open("innovonet-migration-2016-05-11/category.json", "r")
    d = f.read()
    f.close()
    data_cats = json.loads(d)

    for row in data_cats['RECORDS']:
        data_obj = Category(name=row['name'])
        data_obj.save()
Exemple #3
0
 def create_categories_recursive(self, categories, parent=None):
     for name in categories:
         category = Category(name=name, parent=parent, channel=self.channel)
         category.save()
         self.categories.append(category)
         if isinstance(categories, dict):
             subcategories = categories[name]
             if subcategories:
                 self.create_categories_recursive(subcategories, category)
Exemple #4
0
def migrate_categories():
    f = open("innovonet-migration-2016-05-11/category.json", "r")
    d = f.read()
    f.close()
    data_cats = json.loads(d)

    for row in data_cats['RECORDS']:
        data_obj = Category(name=row['name'])
        data_obj.save()   
Exemple #5
0
def _create_category(wealth, name, type, parent=None):
  category = Category(
      wealth=wealth,
      name=name,
      parent=parent,
      total_balance=0,
      balance=0,
      type=type)
  category.save()
  return category
Exemple #6
0
    def test_models_catagory(self):
        slug = get_unique_slug(Category, "Test title")
        self.assertEqual(slug, "test-title")

        category_obj = Category(title="Test title")
        category_obj.save()
        self.assertEqual(category_obj.slug, "test-title")
        self.assertEqual(str(category_obj), "Test title")

        slug = get_unique_slug(Category, "Test title")
        self.assertEqual(slug, "test-title-1")
Exemple #7
0
def new_category(blog_id):

    from core.models import db
    with db.atomic() as txn:

        user = auth.is_logged_in(request)
        blog = Blog.load(blog_id)
        permission = auth.is_blog_editor(user, blog)

        category_list = [n for n in blog.categories]

        category = Category(id=0,
            title='',
            blog=blog)

        top_level_category = Category(
            id=None,
            title='[Top-level category]',
            parent=None
            )

        category_list.insert(0, top_level_category)

        tags = template_tags(
            blog=blog,
            user=user)

    if request.method == "POST":
        with db.atomic() as txn:
            category_title = request.forms.getunicode('category_title')
            try:
                parent_category = int(request.forms.getunicode('category_parent'))
            except ValueError:
                parent_category = None

            with db.atomic() as txn:

                category = Category(blog=blog,
                    title=category_title,
                    parent_category=parent_category
                    )
                category.save()

        redirect('{}/blog/{}/category/{}'.format(
            BASE_URL, blog.id, category.id))

    tpl = template('edit/category',
        category=category,
        category_list=category_list,
        menu=generate_menu('blog_new_category', category),
        search_context=(search_contexts['sites'], None),
        **tags.__dict__)

    return tpl
    def create(self, validated_data):

        # {'name': 'new category 4'}

        # name='new category 4'

        category = Category(**validated_data)

        category.save()

        return category
Exemple #9
0
 def setUp(self):
     self.profile = Profile(username='******', spent_time=133)
     self.profile.save()
     category1 = Category(name="Категория1",
                          profile=self.profile,
                          spent_time=63)
     category1.save()
     category2 = Category(name="Категория2",
                          profile=self.profile,
                          spent_time=70)
     category2.save()
Exemple #10
0
 def add_category(cls, request):
     if request.method == 'POST':
         form = CategoryForm(request.POST)
         if form.is_valid():
             name = request.POST['name']
             new_category = Category(name=name)
             new_category.save()
             return HttpResponseRedirect("/manager/kategorie/")
     else:
         form = CategoryForm()
     return render_to_response('backpanel_new_category.html', 
                               {'form': form}, 
                               context_instance=RequestContext(request))
Exemple #11
0
def make_category(request):
    if request.method == 'POST':
        form = CategoryCreateForm(request.POST)
        if form.is_valid():
            new_category = Category()
            new_category.name = form.cleaned_data['category']
            new_category.save()
        return redirect(to='staff')
    else:
        form = CategoryCreateForm()
    return render(request, 'core/make_category.html', {
        'form' : form,
    })
Exemple #12
0
 def add_category(cls, request):
     if request.method == 'POST':
         form = CategoryForm(request.POST)
         if form.is_valid():
             name = request.POST['name']
             new_category = Category(name=name)
             new_category.save()
             return HttpResponseRedirect("/manager/kategorie/")
     else:
         form = CategoryForm()
     return render_to_response('backpanel_new_category.html',
                               {'form': form},
                               context_instance=RequestContext(request))
Exemple #13
0
class ModelTestCase(TestCase):
    """Test suite for the channel and category models."""
    def setUp(self):
        """Define the test variables."""
        self.channel_name = "test"
        self.category_name = 'Games'
        self.subcategories_names = ['XBOX One', 'XBOX 360', 'Playstation 4']
        self.channel = Channel(name=self.channel_name)
        self.category = Category(name=self.category_name)
        self.subcategories = [
            Category(name=category_name)
            for category_name in self.subcategories_names
        ]

    def test_model_can_create_a_channel(self):
        """Test the channel model can create a channel."""
        old_count = Channel.objects.count()
        self.channel.save()
        new_count = Channel.objects.count()
        self.assertNotEqual(old_count, new_count)

    def test_model_can_create_categories(self):
        """Test the category model can create a category."""
        self.channel.save()
        self.category.channel = self.channel
        self.category.save()
        old_count = Category.objects.count()
        for category in self.subcategories:
            category.parent = self.category
            category.channel = self.channel
            category.save()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)

    def test_model_can_overwrite_a_channel_categories(self):
        """Test the category model can create a category."""
        self.channel.save()
        self.category.channel = self.channel
        self.category.save()
        old_count = Category.objects.count()
        for category in self.subcategories:
            category.parent = self.category
            category.channel = self.channel
            category.save()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)
        old_count = new_count
        Category.objects.filter(channel=self.channel).delete()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)
Exemple #14
0
    def _create_categories(self):
        with open('categories.json', 'r') as categories_file:
            categories_json = json.load(categories_file)

            for category in categories_json:
                category_name = list(category.keys())[0]
                some_category = Category(name=category_name)
                print('Adding category %s' % category_name)
                some_category.save()

                for subcategory_name in category[category_name]:
                    some_subcategory = Category(name=subcategory_name,
                                                parent_category=some_category)
                    print('\tAdding subcategory %s' % subcategory_name)
                    some_subcategory.save()
Exemple #15
0
  def save(self):
    parent = self.cleaned_data['parent']
    if parent == 'inc' or parent == 'exp':
      parent = None

    category = Category(
        wealth=self.wealth,
        type=self.category_type,
        parent=parent,
        name=self.cleaned_data['text'],
        description=self.cleaned_data['description'],
        balance=0,
        total_balance=0)
    category.save()
    return category
Exemple #16
0
def new_category(blog_id):

    from core.models import db
    with db.atomic() as txn:

        user = auth.is_logged_in(request)
        blog = Blog.load(blog_id)
        permission = auth.is_blog_editor(user, blog)

        category_list = [n for n in blog.categories]

        category = Category(id=0, title='', blog=blog)

        top_level_category = Category(id=None,
                                      title='[Top-level category]',
                                      parent=None)

        category_list.insert(0, top_level_category)

        tags = template_tags(blog=blog, user=user)

    if request.method == "POST":
        with db.atomic() as txn:
            category_title = request.forms.getunicode('category_title')
            try:
                parent_category = int(
                    request.forms.getunicode('category_parent'))
            except ValueError:
                parent_category = None

            with db.atomic() as txn:

                category = Category(blog=blog,
                                    title=category_title,
                                    parent_category=parent_category)
                category.save()

        redirect('{}/blog/{}/category/{}'.format(BASE_URL, blog.id,
                                                 category.id))

    tpl = template('edit/category',
                   category=category,
                   category_list=category_list,
                   menu=generate_menu('blog_new_category', category),
                   search_context=(search_contexts['sites'], None),
                   **tags.__dict__)

    return tpl
Exemple #17
0
 def handle(self, channel_name, categories_filename, *args, **options):
     if not isfile(categories_filename):
         raise CommandError('The specified filename does not exist.')
     try:
         channel = Channel.objects.get(name=channel_name)
         self.stdout.write('Overwriting channel %s...' % channel_name)
         Category.objects.filter(channel=channel).delete()
     except Channel.DoesNotExist:
         self.stdout.write('Creating channel %s...' % channel_name)
         channel = Channel(name=channel_name)
         channel.save()
     categories_paths = []
     created_categories = []
     with open(categories_filename, 'rt', encoding='utf8') as inp:
         # TODO Check the parent category is in the db if its not found in created_categories
         # (may happen if the file is not sorted)
         for line in inp:
             category_path = line.strip().replace('\n', '')
             if category_path != '':
                 categories_paths.append(category_path)
                 last_slash_pos = category_path.rfind('/')
                 parent = None
                 parent_path = ''
                 parent_name = ''
                 if last_slash_pos != -1:
                     parent_path = category_path[:last_slash_pos].strip()
                     parent_last_slash_pos = parent_path.rfind('/')
                     parent_name = parent_path[parent_last_slash_pos +
                                               1:].strip()
                     for created_category in reversed(created_categories):
                         if created_category.name == parent_name:
                             parent = created_category
                             break
                     if not parent:
                         self.stderr.write(
                             'Could not find the parent of category %s (check the file is sorted).'
                             % category_path)
                 category_name = category_path[last_slash_pos + 1:].strip()
                 self.stdout.write('Creating category %s...' %
                                   category_path)
                 category = Category(name=category_name,
                                     channel=channel,
                                     parent=parent)
                 category.save()
                 created_categories.append(category)
     n_categories = len(created_categories)
     self.stdout.write('Channel %s was imported with %i categories.' %
                       (channel_name, n_categories))
Exemple #18
0
    def test_command_delete(self, mock_get_products):
        """
        Test for the command : python manage.py database --delete
        """

        mock_get_products.return_value = Mock()
        mock_get_products.return_value.json.return_value = mock_api_return

        # Register a category for the test.
        cat = Category(name='test_category')
        cat.save()

        # Register a product for the test.
        Product(name='prod_name',
                code='1',
                nutrition_grade='a',
                image_nutrition_url='image_nutrition_url',
                image_small_product_url='image_small_product_url',
                image_full_product_url='image_full_product_url',
                url='url',
                category=cat).save()

        category_count_before_del = Category.objects.all().count()
        product_count_before_del = Product.objects.all().count()

        out = StringIO()
        call_command('database', '--delete', stdout=out)

        category_count_after_del = Category.objects.all().count()
        product_count_after_del = Product.objects.all().count()

        # Before delete
        self.assertEqual(category_count_before_del, 1)
        self.assertEqual(product_count_before_del, 1)

        # After delete
        self.assertEqual(category_count_after_del, 0)
        self.assertEqual(product_count_after_del, 0)

        self.assertIn('1 products and 1 categories deleted', out.getvalue())
Exemple #19
0
    def test_expense_model(self):

        c1 = Category(name='food')
        c2 = Category(name='doctor')
        c3 = Category(name='entertainment')
        c4 = Category(name='ride')
        c1.save()
        c2.save()
        c3.save()
        c4.save()

        u1 = User(username='******', email='*****@*****.**', first_name='soonmo', last_name='kwon')
        u1.save()
        u2 = User(username='******', email='*****@*****.**', first_name='soonuck', last_name='kwon')
        u2.save()

        item1 = Item(dollar_amount=100.00, team_member=u1, category=c2, activity_date=(date(year=2013, month=7, day=23)))
        item1.save()
        item2 = Item(dollar_amount=52.25, team_member=u2, category=c3, activity_date=(date(year=2013, month=7, day=22)))
        item2.save()
Exemple #20
0
def get_or_create_category(each_deal_data_dict,
                           categories_dict,
                           shortcut=False):
    '''
    * 'shortcut' option is for simply retreiving the category object.
    '''
    category_slug = each_deal_data_dict['category_slug']
    category_name = each_deal_data_dict['category_name']
    if category_slug == 'retail-services':
        category_slug = 'shopping-services'
        category_name = 'Shopping & Services'
    if not category_slug:
        return None  # In case where Sqoot doesn't show any category for a given deal

    try:
        category_model = Category.objects.get(code=category_slug,
                                              ref_id_source='sqoot')
    except Category.DoesNotExist:
        category_model = Category(ref_id_source='sqoot',
                                  code=category_slug,
                                  name=category_name)
        category_model.save()

    if shortcut:
        return category_model

    parent_slug = categories_dict[category_slug]
    if parent_slug:
        try:
            parent_category = Category.objects.get(code=parent_slug,
                                                   ref_id_source='sqoot')
            category_model.parent = parent_category
        except Category.DoesNotExist:
            parent_category = Category(ref_id_source='sqoot', code=parent_slug)
            parent_category.save()
            category_model.parent = parent_category
    else:
        category_model.parent = None

    # In case it was already created as another category's parent (hence save outside try-except)
    category_model.name = category_name
    category_model.save()
    return category_model
Exemple #21
0
def get_or_create_category(each_deal_data_dict, categories_dict, shortcut=False):
    '''
    * 'shortcut' option is for simply retreiving the category object.
    '''
    category_slug = each_deal_data_dict['category_slug']
    category_name = each_deal_data_dict['category_name']
    if category_slug == 'retail-services':
        category_slug = 'shopping-services'
        category_name = 'Shopping & Services'
    if not category_slug:
        return None # In case where Sqoot doesn't show any category for a given deal

    try:
        category_model = Category.objects.get(code=category_slug, ref_id_source='sqoot')
    except Category.DoesNotExist:
        category_model = Category(ref_id_source='sqoot', code=category_slug, name=category_name)
        category_model.save()

    if shortcut:
        return category_model

    parent_slug = categories_dict[category_slug]
    if parent_slug:
        try:
            parent_category               = Category.objects.get(code=parent_slug, ref_id_source='sqoot')
            category_model.parent         = parent_category
        except Category.DoesNotExist:
            parent_category = Category(ref_id_source='sqoot', code=parent_slug)
            parent_category.save()
            category_model.parent         = parent_category
    else:
        category_model.parent             = None

    # In case it was already created as another category's parent (hence save outside try-except)
    category_model.name = category_name
    category_model.save()
    return category_model
Exemple #22
0
 def create(self, validated_data):
     category = Category(**validated_data)
     category.save()
     return category
    def populate_db(self):
        """
        This method populates the database with Open Food Facts data when the Product and Category tables are empty.
        If these tables are not empty, the user will be notified.
        The Open Food Facts API is used.
        """

        if self.db_is_empty():
            try:
                self.stdout.write(
                    "Saving Open Food Facts data in the local database ...")
                self.stdout.write("Please wait ...")

                # Get API configuration
                api_url = API_CONFIG["url"]
                api_criteria = API_CONFIG["criteria"]
                nutrition_grades = API_CONFIG["nutrition_grades"]
                openfoodfacts_categories = OFF_CATS["categories"]

                for category in openfoodfacts_categories:
                    api_criteria.update({"tag_0": category})

                    # Save the category in the database
                    cat = Category(name=category)
                    cat.save()

                    for nutrition_grade in nutrition_grades:
                        api_criteria.update({"tag_1": nutrition_grade})
                        # Call API
                        response = requests.get(api_url, params=api_criteria)
                        data = response.json()
                        products = data['products']

                        # Save the product in the database
                        for product in products:
                            if product.get('image_nutrition_url') and product.get('image_small_url') \
                                    and product.get('code') != "1":
                                Product(name=product.get(
                                    'product_name').lower().capitalize(),
                                        code=product.get('code'),
                                        nutrition_grade=product.get(
                                            'nutrition_grades'),
                                        image_nutrition_url=product.get(
                                            'image_nutrition_url'),
                                        image_small_product_url=product.get(
                                            'image_small_url'),
                                        image_full_product_url=product.get(
                                            'image_small_url').replace(
                                                '200.jpg', 'full.jpg'),
                                        url=product.get('url'),
                                        category=cat).save()

                self.stdout.write(
                    "-> Database populated with Open Food Facts data.")
                self.stdout.write(
                    f"-> {self.product_count} products and {self.category_count} "
                    f"categories were registered.")

            except requests.exceptions.ConnectionError as err:
                # If there is an API connection problem, the Category table is flushed.
                Category.objects.all().delete()
                print(f'Error : {err}')
        else:
            self.stdout.write("Database is not empty !")
            self.stdout.write(
                "Please use --delete before repopulating the database.")
Exemple #24
0
def scrap_from_lms(year_semester, limit=True):
    ''' Scrap(Parse) subjects data from lms.
    This function makes a baseline of database.
    Using parsed information of each subject, 
    it searches database and update a matched subject or create a new one.
    If limit is true, this function reads one subject per 0.5 seconds,
    otherwise it reads the lms data as fast as it can.
    '''

    ## input example: '2018s', '2019f'

    ## parsing input
    year = year_semester[0:4]
    semester_input = year_semester[4:5]

    ## we change semester 's' to '0' and 'f' to '2' for lms query
    ## semester_code is used for model 'Semester'
    semester_dic = {'s': '0', 'f': '2', 'S': '0', 'F': '2'}
    semester_code = {'0': 'S', '2': 'F'}
    semester_code_in_Korean = {'0': '년 1학기', '2': '년 2학기'}
    semester = semester_dic[semester_input]

    ## Semester check and if there is no semester, then make it
    try:
        Semester_object = Semester.objects.get(code=year +
                                               semester_code[semester])
    except ObjectDoesNotExist:
        Semester_object = Semester(name=year +
                                   semester_code_in_Korean[semester],
                                   code=year + semester_code[semester])
        Semester_object.save()
        try:
            print("* * * * New semester created! WOW! " + year +
                  semester_code_in_Korean[semester] + " * * * *")
        except:
            print("* * * * New semester created! WOW! " + year +
                  semester_code[semester] + " * * * *")

    ## variables
    lms_url = 'http://lms.postech.ac.kr/'

    ## Change department values to use whole departments' information
    department_page = requests.get(lms_url + 'Course.do?cmd=viewCourseAllList')
    department_tree = html.fromstring(department_page.content)
    department_list = department_tree.xpath('//span[@class="selectForm"]')[1]
    department_name = department_list.xpath('.//a[@href]/text()')
    department_code = department_list.xpath('.//a/attribute::href')

    ## save departments variables
    departments = []
    departments_cleared_name = []
    Department_objects = []

    if limit:
        limit_str = '0.5 SEC'
    else:
        limit_str = 'UNLIMITTED!!!'
    print(
        "********************************** PARSING MESSAGE ***************************************"
    )
    print("******* SEMESTER: " + year + " " + semester_code[semester] +
          "     *************************   SPEED LIMIT: " + limit_str +
          " *******")
    print(
        "*********************************** PARSING START ****************************************"
    )

    ## for each parsed department information, get 'Department' model object or create new one
    for i in range(len(department_code)):
        departments.append(department_code[i].split(',')[1].split("'")[1])
        departments_cleared_name.append(clear_string(department_name[i]))

        try:
            Department_object = Department.objects.get(
                name=departments_cleared_name[i])
        except ObjectDoesNotExist:
            Department_object = Department(name=departments_cleared_name[i])
            Department_object.save()
            try:
                print(
                    "****** New department " + departments_cleared_name[i] +
                    " created ****** This should be done ONCE at the first time"
                )
            except:
                print(
                    "****** New department created ****** This should be done ONCE at the first time"
                )

        Department_objects.append(Department_object)

    ## current page starts from 1
    cur_page = 1

    ## search. open subject lists by department
    for cur_dept in range(len(departments)):
        # To let you know where you are
        try:
            print("Parsing department: " + departments_cleared_name[cur_dept])
        except:
            print("Parsing department #" + str(cur_dept))

        while True:
            ## get a subject list of one department
            page = requests.get(
                lms_url +
                'Course.do?cmd=viewCourseAllList&courseTermDTO.courseTermId=' +
                year + '09' + semester + '&searchCode1=' +
                departments[cur_dept] + '&curPage=' + str(cur_page))
            if page.status_code != 200:
                cur_page += 1
                continue
            tree = html.fromstring(page.content)

            ## find the href list
            href_list = tree.xpath('//td/a/attribute::href')

            ## if there is no subject it breaks
            if len(href_list) == 0:
                print("No more subjects in this list!")
                break

            ## start parsing!
            print("Parsing page # " + str(cur_page))

            ## get one subject info and plan data from one subject data
            for cur_subject in href_list:
                info_page = requests.get(lms_url + cur_subject)
                if info_page.status_code != 200:
                    continue
                info_tree = html.fromstring(info_page.content)

                ## get info table
                info_tr = info_tree.xpath('//table/tr')
                ## plan src
                plan_a = info_tree.xpath('//a/attribute::href')[0]

                ## EXAMPLE: 1-0-1
                try:
                    credit = clear_string(info_tr[8].xpath('td')[3].text)
                except:
                    continue
                ## EXAMPLE: CSED101
                code = clear_string(info_tr[4].xpath('td')[1].text)

                ## EXAMPLE: 햅틱스 입문
                subject_name = clear_string(info_tr[2].xpath('td')[1].text)
                ## EXAMPLE: 전공선택
                category = clear_string(info_tr[6].xpath('td')[3].text)

                ## EXAMPLE: 1
                try:
                    class_number = int(
                        clear_string(info_tr[4].xpath('td')[3].text))
                except ValueError:
                    class_number = 1
                ## EXAMPLE: '200'
                capacity_raw = clear_string(info_tr[10].xpath('td')[1].text)
                ## EXAMPLE: 200
                capacity = int(re.search(r'\d+', capacity_raw).group())

                ## load plan data
                plan_page = requests.get(lms_url + plan_a)
                if plan_page.status_code != 200:
                    continue
                plan_tree = html.fromstring(plan_page.content)

                ## get plan table. sometimes it does not have any plan table (?)
                if len(plan_tree.xpath('//table')) > 0:
                    plan_tr = plan_tree.xpath('//table')

                    ## EXAMPLE: 윤은영
                    prof_name = clear_string(plan_tr[2].xpath('.//td')[0].text)
                    ## EXAMPLE..... is the line below.
                    time_place_raw = plan_tr[1].xpath('.//td')[5].xpath(
                        './/text()')
                    time_place_num = len(time_place_raw)

                    ## time_place lines can be multiple, so we prepare arrays
                    time_place = []
                    place = []
                    start_time = []
                    end_time = []
                    days = []

                    ## This time_place can be several line. So it goes with for loop.
                    for line_number in range(time_place_num):
                        time_place_text = clear_string(
                            time_place_raw[line_number])

                        ## EXAMPLE: 1. 월 수 (09:30~10:45) | 무은재기념관 강의실 [101호]
                        if '.' not in time_place_text:
                            time_place.append(time_place_text)
                        else:
                            time_place.append(time_place_text.split('.')[1])

                        ## EXAMPLE: 월 수 (09:30~10:45) | 무은재기념관 강의실 [101호]

                        ## Actually... I don't know why I wrote like this.
                        ## Maybe there is a historical reason. (e.g. there is no time_place text yet.)
                        if len(time_place[line_number]) < 8:
                            time_place_num = 0
                            break

                        ## EXAMPLE:  무은재기념관 강의실 [101호]
                        time_place_or_splitted = time_place[line_number].split(
                            '|')
                        place_temp = time_place_or_splitted[1]
                        ## EXAMPLE: ' 무은재기념관 강의실 [101호]' -> '무은재기념관 강의실 [101호]'
                        place.append(place_temp[1:len(place_temp) + 1])

                        ## EXAMPLE: 월 수 (09:30~10:45)
                        days_time = time_place_or_splitted[0]
                        ## EXAMPLE: 09:30~10:45
                        time = days_time.split('(')[1].split(')')[0]
                        ## EXAMPLE: 09:30
                        start_time.append(time.split('~')[0])
                        ## EXAMPLE: 10:45
                        end_time_raw = time.split('~')[1]
                        if end_time_raw[0:2] == '24':
                            end_time_raw = '23:59'
                        end_time.append(end_time_raw)

                        ## EXAMPLE: ['월', '수']
                        days.append(days_time.split('(')[0].split())

                ## if there is no table (I don't know why I wrote this code in RoR lemons)
                ## just pass it. come back later. Don't save anything.
                else:
                    continue

                ## Try to find category of subject. if except occurs, make one.
                try:
                    if category == u"0035":
                        category = u"기초선택"
                    Category_object = Category.objects.get(category=category)
                except ObjectDoesNotExist:
                    Category_object = Category(category=category)
                    Category_object.save()
                    try:
                        print("***** New category: " + category +
                              " added *****")
                    except:
                        print("***** New category added *****")

                ## Try to find Subject.
                try:
                    ## Find subject by code, class_number, semester and name
                    ## This should be just one
                    Subject_object = Subject.objects.get(
                        code=code,
                        class_number=class_number,
                        semester=Semester_object,
                        name=subject_name)
                    ## Update it for which is not specified (code, class_number, semester and name)
                    Subject_object.category = Category_object
                    Subject_object.department = Department_objects[cur_dept]
                    Subject_object.plan = plan_a
                    if prof_name != "":
                        Subject_object.professor = prof_name
                    if capacity != 0:
                        Subject_object.capacity = capacity
                    if credit != "":
                        Subject_object.credit = credit
                    Subject_object.save()
                    try:
                        print("Subject " + subject_name + "(" + code +
                              ") is updated")
                    except:
                        print("Updated!")

                except ObjectDoesNotExist:
                    ## There is no matching subject. do once more
                    Subject_object = Subject(
                        name=subject_name,
                        code=code,
                        category=Category_object,
                        department=Department_objects[cur_dept],
                        plan=plan_a,
                        professor=prof_name,
                        class_number=class_number,
                        capacity=capacity,
                        credit=credit,
                        semester=Semester_object)
                    Subject_object.save()
                    try:
                        print("Subject " + subject_name + "(" + code +
                              ") is created")
                    except:
                        print("Created!")

                ## Try to find out Period or make it
                ## Update period just in case
                Period_objects = Period.objects.filter(subject=Subject_object)

                ## update for existing time
                if time_place_num - len(Period_objects) >= 0:
                    for period_object_number in range(len(Period_objects)):
                        Period_object = Period_objects[period_object_number]
                        Period_object.place = place[period_object_number]
                        Period_object.start = start_time[period_object_number]
                        Period_object.end = end_time[period_object_number]

                        Period_object.mon = '월' in days[period_object_number]
                        Period_object.tue = '화' in days[period_object_number]
                        Period_object.wed = '수' in days[period_object_number]
                        Period_object.thu = '목' in days[period_object_number]
                        Period_object.fri = '금' in days[period_object_number]
                        Period_object.save()

                    ## Create Period objects for not yet created time
                    for number in range(time_place_num - len(Period_objects)):
                        new_object_number = number + len(Period_objects)
                        #print("Cur # " + str(new_object_number) + " with " + start_hour[new_object_number] + ":" + start_min[new_object_number])

                        Period_object = Period(
                            subject=Subject_object,
                            place=place[new_object_number],
                            start=start_time[new_object_number],
                            end=end_time[new_object_number],
                            mon='월' in days[new_object_number],
                            tue='화' in days[new_object_number],
                            wed='수' in days[new_object_number],
                            thu='목' in days[new_object_number],
                            fri='금' in days[new_object_number])
                        Period_object.save()

                if limit:
                    sleep(0.5)

            cur_page += 1
        cur_page = 1
Exemple #25
0
    def handle(self, *args, **options):
        for current in options['tsv_file']:
            with open(current) as input_file:
                results = {}
                parser_state = ""
                current_question_id = 0
                for row in input_file.readlines():
                    row = row.strip()
                    tokens = row.split("\t")
                    if tokens[0] == "##" or tokens[0] == "":
                        continue
                    elif parser_state == "" and tokens[0] == "Quiz":
                        results["quiz"] = tokens[1]
                        parser_state = "parsed_quiz"
                    elif parser_state == "parsed_quiz" and tokens[0] == "Timed":
                        results["timed"] = tokens[1]
                        parser_state = "parsed_timed"
                    elif parser_state == "parsed_timed" and tokens[
                            0] == "Max Time In Mins":
                        results["max_time_in_mins"] = tokens[1]
                        parser_state = "parsed_max_time"
                    elif parser_state == "parsed_max_time" and tokens[
                            0] == "Quiz Structure":
                        parser_state = "parse_quiz_structure"
                    elif parser_state == "parse_quiz_structure":
                        if "quiz_structure" not in results:
                            results["quiz_structure"] = []
                        # change state to parsing question bank
                        if tokens[0] == "Category":
                            parser_state = "parse_qb"
                            continue

                        # update quiz structure
                        current_qs = {}
                        k, v = tokens
                        current_qs[k] = int(v)
                        results["quiz_structure"].append(current_qs)
                    elif parser_state == "parse_qb":
                        # it is a question line
                        if len(tokens) > 2:
                            category, sub_category, question_type, point, question, choice = tokens[:
                                                                                                    6]

                            correct = ""
                            if len(tokens) > 6 and tokens[6] != "":
                                correct = tokens[6]

                            if "questions" not in results:
                                results["questions"] = {}
                            results["questions"][current_question_id] = {}
                            results["questions"][current_question_id][
                                "category"] = category
                            results["questions"][current_question_id][
                                "sub_category"] = sub_category
                            results["questions"][current_question_id][
                                "question_type"] = question_type
                            results["questions"][current_question_id][
                                "point"] = point
                            results["questions"][current_question_id][
                                "question"] = question
                            results["questions"][current_question_id][
                                "choices"] = []
                            current_choice = {}
                            current_choice["caption"] = choice
                            if correct == "Y":
                                current_choice["correct"] = True
                            else:
                                current_choice["correct"] = False
                            results["questions"][current_question_id][
                                "choices"].append(current_choice)
                            current_question_id += 1
                        else:
                            # append to choices
                            current_choice = {}
                            current_choice["caption"] = tokens[0]
                            if len(tokens) > 1 and tokens[1] == "Y":
                                current_choice["correct"] = True
                            else:
                                current_choice["correct"] = False
                            results["questions"][current_question_id -
                                                 1]["choices"].append(
                                                     current_choice)
                    else:
                        print(f"At {parser_state}: Cannot parse {row}")

                if self.DEBUG:
                    print("Parsed:")
                    print(json.dumps(results, indent=1))

                uniq_categories = []

                for current in results['questions']:
                    current_category = results['questions'][current][
                        'category']
                    if current_category not in uniq_categories:
                        uniq_categories.append(current_category)

                # create categories
                for current in uniq_categories:
                    if not Category.objects.filter(name=current).exists():
                        category = Category()
                        category.name = current
                        category.save()

                # create sub-categories
                for current in results['questions']:
                    current_category = results['questions'][current][
                        "category"]
                    category = Category.objects.get(name=current_category)
                    current_sub_category = results['questions'][current][
                        "sub_category"]
                    if not SubCategory.objects.filter(
                            name=current_sub_category).exists():
                        sub_category = SubCategory()
                        sub_category.category = category
                        sub_category.name = current_sub_category
                        sub_category.save()

                # create quiz
                quiz = None
                if Quiz.objects.filter(name=results["quiz"]).exists():
                    raise CommandError(
                        f"Quiz {results['quiz']} already exists")
                else:
                    quiz = Quiz()
                    quiz.name = results['quiz']
                    quiz.category = Category.objects.get(
                        name=uniq_categories[0])
                    quiz.sub_category = SubCategory.objects.filter(
                        category=quiz.category).first()
                    if results['timed'] == "Y":
                        quiz.timed = True
                    else:
                        quiz.timed = False
                    quiz.time_limit = int(results['max_time_in_mins'])
                    quiz.save()

                # create quiz structure
                for current in results['quiz_structure']:
                    quiz_structure = QuizStructure()
                    quiz_structure.quiz = quiz
                    quiz_structure.category = Category.objects.get(
                        name=uniq_categories[0])
                    sub_category, frequency = list(current.keys())[0], list(
                        current.values())[0]
                    quiz_structure.sub_category = SubCategory.objects.get(
                        name=sub_category, category=quiz_structure.category)
                    quiz_structure.frequency = frequency
                    quiz_structure.save()

                # create question type
                for current in results['questions']:
                    current_question_type = results['questions'][current][
                        "question_type"]
                    if not QuestionType.objects.filter(
                            name=current_question_type).exists():
                        question_type = QuestionType()
                        question_type.name = current_question_type
                        question_type.save()

                # create question & choices
                for current in results['questions']:
                    question = Question()
                    question.category = Category.objects.get(
                        name=results['questions'][current]["category"])
                    question.sub_category = SubCategory.objects.get(
                        name=results['questions'][current]["sub_category"],
                        category=category)
                    question.question_type = QuestionType.objects.get(
                        name=results['questions'][current]["question_type"])
                    question.point = results['questions'][current]["point"]
                    question.blurb = results['questions'][current]["question"]
                    question.save()

                    for current_choice in results['questions'][current][
                            "choices"]:
                        choice = Choice()
                        choice.question = question
                        choice.choice = current_choice['caption']
                        choice.correct = current_choice['correct']
                        choice.save()