Esempio n. 1
0
    def update(self, request, pk=None):
        event = self.get_object()
        event.categories.clear()

        old_event = models.Event(start=event.start,
                                 end=event.end,
                                 location=event.location)

        # now we trying to add relationship with categories
        categories = [
            t.strip() for t in request.data.get('categories', '').split(',')
        ]
        for cate_name in categories:
            category = models.Category.objects.filter(name=cate_name)
            if category.count() == 0:
                new_category = models.Category(name=cate_name)
                new_category.save()
                new_category.events.add(event)
            else:
                category = models.Category.objects.get(name=cate_name)
                category.events.add(event)

        serializer = self.get_serializer(event,
                                         data=request.data,
                                         partial=False)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        # now we are sending email if location or time range changed.
        try:
            if old_event.start != event.start or old_event.end != event.end or old_event.location != event.location:
                print("Datetime or location has changed!")
                participants = models.Participate.objects.filter(event=event)

                helpers.SendEmailThread(participants, old_event, event,
                                        helpers.EVENT_CHANGE).start()

        except expression as identifier:
            print(identifier)
            pass

        return Response(serializer.data, HTTP_200_OK)
Esempio n. 2
0
    def create(self, request):
        data = request.data
        new_event = models.Event(title=data.get('title', ''),
                                 description=data.get('description', ''),
                                 location=data.get('location', ''),
                                 start=data.get('start', ''),
                                 end=data.get('end', ''))
        if new_event.start > new_event.end:
            return Response(
                data={'message': 'Start date must be smaller than end date.'},
                status=HTTP_400_BAD_REQUEST)
        new_event.save()

        # now we trying to add relationship with categories
        categories = [
            t.strip() for t in request.data.get('categories', '').split(',')
        ]
        for cate_name in categories:
            category = models.Category.objects.filter(name=cate_name)
            if category.count() == 0:
                new_category = models.Category(name=cate_name)
                new_category.save()
                new_category.events.add(new_event)
            else:
                category = models.Category.objects.get(name=cate_name)
                category.events.add(new_event)

        # now we uploading file
        for file in request.FILES:
            file_extension = os.path.splitext(request.FILES[file].name)[1]
            print(file_extension)
            if file_extension not in helpers.valid_file_extension:
                continue
            image = models.Image(event=new_event)
            image.image.save(f'{uuid.uuid4()}{file_extension}',
                             request.FILES[file])
            image.save()

        serializer = serializers.EventSerializer(new_event)

        return Response(serializer.data, status=HTTP_200_OK)
Esempio n. 3
0
    def _initialize(self):
        print('loading data')
        dataframes = self._load_dataframes()
        print('Done')
        print('initialize models')
        models.Location.objects.all().delete()
        models.CategoryGroup.objects.all().delete()
        models.Category.objects.all().delete()
        categorygroups = dataframes['categorygroups']
        categorys = dataframes['categorys']
        locations = dataframes['locations']
        categorygroup_bulk = [
            models.CategoryGroup(id=categorygroup.id, name=categorygroup.name)
            for categorygroup in categorygroups.itertuples()
        ]
        models.CategoryGroup.objects.bulk_create(categorygroup_bulk)
        print('categorygroup_done')
        category_bulk = [
            models.Category(
                id=category.id,
                name=category.name,
                categorygroup_id=category.categorygroup,
            ) for category in categorys.itertuples()
        ]

        models.Category.objects.bulk_create(category_bulk)
        print('category_done')
        location_bulk = [
            models.Location(id=location.id,
                            name=location.name,
                            address=location.address,
                            tel=location.tel,
                            latitude=location.latitude,
                            longitude=location.longitude,
                            category_id=location.category,
                            rank=location.rank)
            for location in locations.itertuples()
        ]
        models.Location.objects.bulk_create(location_bulk)
        print('done')
Esempio n. 4
0
    def _initialize(self):
        '''
        기존의 dataframe pkl파일을 읽어와서 DB에 저장합니다.
        '''
        print("Loading part data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "part.p"
        with open(cur_file, 'rb') as f:
            part_list = pickle.load(f)
        print("complete")
        print("Loading set data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "set.p"
        with open(cur_file, 'rb') as f:
            set_list = pickle.load(f)
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "set2.p"
        with open(cur_file, 'rb') as f:
            set_list2 = pickle.load(f)
        print("complete")
        print("Loading color data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "color.p"
        with open(cur_file, 'rb') as f:
            color_list = pickle.load(f)["results"]
        print("complete")
        print("Loading part_categories data")
        cur_file = Path(
            settings.BASE_DIR) / "crawling" / "data" / "part_categories.p"
        with open(cur_file, 'rb') as f:
            part_categories_list = pickle.load(f)["results"]
        print("complete")
        print("Loading theme data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "theme.p"
        with open(cur_file, 'rb') as f:
            theme_list = pickle.load(f)
        print("complete")
        print("Loading setpart data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "setpart.p"
        with open(cur_file, 'rb') as f:
            setpart_list = pickle.load(f)
        print("complete")
        print("Loading user data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "user.p"
        with open(cur_file, 'rb') as f:
            user_list = pickle.load(f)
        print("complete")
        print("Loading review data")
        cur_file = Path(settings.BASE_DIR) / "crawling" / "data" / "review.p"
        with open(cur_file, 'rb') as f:
            review_list = pickle.load(f)
        print("complete")

        print("[*] Delete all data...")
        # DB에 저장된 정보를 모두 지워 초기화해 줍니다.
        models.CustomUser.objects.all().delete()
        models.Theme.objects.all().delete()
        models.LegoSet.objects.all().delete()
        models.OfficialMapping.objects.all().delete()
        models.Category.objects.all().delete()
        models.Review.objects.all().delete()
        models.LegoPart.objects.all().delete()
        models.Color.objects.all().delete()
        models.UserPart.objects.all().delete()
        models.SetPart.objects.all().delete()
        print("[+] Done")

        print("[*] Initializing categories...")
        categories_bulk = [
            models.Category(id=category["id"],
                            name=category["name"],
                            part_count=category["part_count"])
            for category in part_categories_list
        ]
        models.Category.objects.bulk_create(categories_bulk)
        print("[+] Done")

        print("[*] Initializing colors...")
        colors_bulk = [
            models.Color(
                id=color["id"],
                name=color["name"],
                rgb=color["rgb"],
                bricklink_ids="|".join(
                    map(str, color["external_ids"]["BrickLink"]["ext_ids"]))
                if color["external_ids"].get("BrickLink")
                and color["external_ids"]["BrickLink"].get("ext_ids") else "",
                bricklink_descrs="|".join(
                    color["external_ids"]["BrickLink"]["ext_descrs"][0])
                if color["external_ids"].get("BrickLink") and
                color["external_ids"]["BrickLink"].get("ext_descrs") else "",
                official_ids="|".join(
                    map(str, color["external_ids"]["LEGO"]["ext_ids"]))
                if color["external_ids"].get("LEGO")
                and color["external_ids"]["LEGO"].get("ext_ids") else "",
                official_descrs="|".join(
                    color["external_ids"]["LEGO"]["ext_descrs"][0])
                if color["external_ids"].get("LEGO")
                and color["external_ids"]["LEGO"].get("ext_descrs") else "",
            ) for color in color_list
        ]
        models.Color.objects.bulk_create(colors_bulk)
        models.Color.objects.create(id=9999, name="not a color", rgb="000000")
        models.Color.objects.create(id=-1, name="Unknown", rgb="0033B2")
        print("[+] Done")

        print("[*] Initializing themes...")
        themes_bulk = [
            models.Theme(id=theme["id"],
                         parent_id=theme["parent_id"],
                         name=theme["name"],
                         root_id=theme["root_id"]) for theme in theme_list
        ]
        models.Theme.objects.bulk_create(themes_bulk)
        print("[+] Done")

        print("[*] Initializing sets...")
        # theme     name    n  um_parts     images  review_count
        sets_bulk = [
            models.LegoSet(
                theme_id=legoset[0],
                name=legoset[1],
                num_parts=legoset[2],
                images=legoset[3],
                review_count=legoset[4],
            ) for legoset in set_list
        ]
        models.LegoSet.objects.bulk_create(sets_bulk)
        print("[+] Done")

        print("[*] Initializing official mapping table...")
        mapping_table = [
            models.OfficialMapping(id=v["set_num"], lego_set_id=i)
            for i, v in enumerate(set_list2, 1)
        ]
        models.OfficialMapping.objects.bulk_create(mapping_table)
        print("[+] Done")

        print("[*] Initializing lego parts...")
        lego_part_bulk = [
            models.LegoPart(
                id=part["part_num"],
                name=part["name"],
                category_id=part["part_cat_id"],
                image=part["part_img_url"] if part["part_img_url"] else "",
                bricklink_ids="|".join(part["external_ids"]["BrickLink"])
                if part["external_ids"].get("BrickLink") else "",
                official_ids="|".join(part["external_ids"]["LEGO"])
                if part["external_ids"].get("LEGO") else "")
            for part in part_list
        ]
        models.LegoPart.objects.bulk_create(lego_part_bulk)
        print("[+] Done")

        print("[*] Initializing setparts...")
        # inventory_id	part_num	color_id	quantity
        setpart_bulk = [
            models.SetPart(lego_set_id=part[0],
                           quantity=part[3],
                           color_id=part[2],
                           part_id=part[1]) for part in setpart_list
        ]
        models.SetPart.objects.bulk_create(setpart_bulk)
        print("[+] Done")

        print("[*] Initializing users...")
        #   id  username    nickname    age gender  review_count
        user_bulk = [
            models.CustomUser(id=user[0],
                              username=user[1],
                              nickname=user[1],
                              age=user[3],
                              gender=user[4],
                              review_count=user[5]) for user in user_list
        ]
        models.CustomUser.objects.bulk_create(user_bulk)
        print("[+] Done")

        print("[*] Initializing reviews...")
        # score user_id lego_set_id
        review_bulk = [
            models.Review(score=review[0],
                          user_id=review[1],
                          lego_set_id=review[2]) for review in review_list
        ]
        models.Review.objects.bulk_create(review_bulk)
        print("[+] Done")
Esempio n. 5
0
z_matrix = marketshare @ supply_demand
sectors = list(zip(sectors, added_value[len(added_value) - 2][1:]))
I = np.eye(z_matrix.shape[0])
va = np.repeat(np.array([added_value[-2][1:]]), z_matrix.shape[0], axis=0)
a_matrix = z_matrix / va
l_matrix = np.linalg.inv(I - a_matrix)

cats = ["Importação", "Impostos indiretos líquidos de Subsídios"] + list(
    map(lambda a: a[0].strip(), added_value))
cats_val = np.array([imports.tolist(), taxes.tolist()] +
                    list(map(lambda a: a[1:], added_value)))
cats_val /= np.repeat(np.array([added_value[-2][1:]]), len(cats), axis=0)

nmodel = models.Model(name=f"IBGE {year}",
                      economic_matrix=a_matrix,
                      leontief_matrix=l_matrix,
                      catimpct_matrix=cats_val)
# noinspection PyUnresolvedReferences
nmodel.sectors.extend(
    models.Sector(name=x[1][0], pos=x[0], value_added=x[1][1])
    for x in enumerate(sectors))
# noinspection PyUnresolvedReferences
nmodel.categories.extend(
    models.Category(name=x[1], pos=x[0], description=x[1], unit='none')
    for x in enumerate(cats))

db.add(nmodel)
db.commit()
db.flush()