#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os, sys, django

sys.path.append(".")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")

django.setup()

from brand.models import *

import json


class ProductDB():

  def __init__(self, model):

    self.model = model

  def create(self, pid, cat1id, cat2id, cat3id, pname, brand, imgurl):

    if self.model == Product:

      p = Product(pid=pid, cat1id=cat1id, cat2id=cat2id, cat3id=cat3id, pname=pname, brand=brand, imgurl=imgurl)

      p.save()


class DataFile():

  def __init__(self, fp):

    self.f = open(fp, 'r')

  def read(self):

    line = self.f.readline().strip()

    return line

  def json_data(self):

    line = self.f.readline().strip()

    data = json.loads(line)

    return data


def main():

  data_dir = "./data/json.txt"

  f = DataFile(data_dir)


  while(True):

    data = f.json_data()

    if not data:  break

    pid = data['id']

    cat1id = data['category1Id']

    cat2id = data['category2Id']

    cat3id = data['category3Id']

    pname = data['productName']

    brand = data['brand']

    imgurl = data['imageUrl']


    p = ProductDB(Product)

    p.create(pid, cat1id, cat2id, cat3id, pname, brand, imgurl)


####################################################

# main

####################################################

if __name__ == '__main__':

  main()



+ Recent posts