#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Slackers.settings')

import django

django.setup()

import json
import argparse
import random
import datetime
import codecs
import os.path as op
from library.models import Book, Reader, Borrowing
from django.contrib.auth.models import User

from faker import Factory

fake = Factory.create('zh_CN')


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()

        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]

    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'], author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True

        if random.randint(1, 100) % 2 == 0:
            returned_flag = False

        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0

            b = Borrowing.objects.create(
                reader=reader,
                ISBN=isbn,
                date_issued=issued,
                date_due_to_returned=due_to_returned,
                date_returned=date_returned,
                amount_of_fine=fine,
            )
            b.save()
        else:

            if reader.max_borrowing > 0 and isbn.quantity > 0:
                b = Borrowing.objects.create(
                    reader=reader,
                    ISBN=isbn,
                    date_issued=issued,
                    date_due_to_returned=due_to_returned
                )

                reader.max_borrowing -= 1
                isbn.quantity -= 1
                reader.save()
                isbn.save()
                b.save()


if __name__ == '__main__':
    init_book_data()

    parser = argparse.ArgumentParser()
    parser.add_argument("data", help=u"你要生成的数据")
    args = parser.parse_args()

    if args.data == 'all':
        init_reader_data()
        init_book_data()
    elif args.data == 'book':
        init_book_data()
    elif args.data == 'reader':
        init_reader_data()
    elif args.data == 'borrowing':
        init_borrowing_data()

757b89b33f9fbf931ad367e6ee194282e9e56d5d
<mask token>


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()
        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=
            int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]
    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'],
                author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(
            1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True
        if random.randint(1, 100) % 2 == 0:
            returned_flag = False
        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned,
                date_returned=date_returned, amount_of_fine=fine)
            b.save()
        elif reader.max_borrowing > 0 and isbn.quantity > 0:
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned)
            reader.max_borrowing -= 1
            isbn.quantity -= 1
            reader.save()
            isbn.save()
            b.save()


<mask token>

<mask token>
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Slackers.settings')
<mask token>
django.setup()
<mask token>


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()
        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=
            int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]
    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'],
                author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(
            1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True
        if random.randint(1, 100) % 2 == 0:
            returned_flag = False
        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned,
                date_returned=date_returned, amount_of_fine=fine)
            b.save()
        elif reader.max_borrowing > 0 and isbn.quantity > 0:
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned)
            reader.max_borrowing -= 1
            isbn.quantity -= 1
            reader.save()
            isbn.save()
            b.save()


if __name__ == '__main__':
    init_book_data()
    parser = argparse.ArgumentParser()
    parser.add_argument('data', help=u'你要生成的数据')
    args = parser.parse_args()
    if args.data == 'all':
        init_reader_data()
        init_book_data()
    elif args.data == 'book':
        init_book_data()
    elif args.data == 'reader':
        init_reader_data()
    elif args.data == 'borrowing':
        init_borrowing_data()

<mask token>
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Slackers.settings')
<mask token>
django.setup()
<mask token>
fake = Factory.create('zh_CN')


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()
        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=
            int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]
    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'],
                author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(
            1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True
        if random.randint(1, 100) % 2 == 0:
            returned_flag = False
        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned,
                date_returned=date_returned, amount_of_fine=fine)
            b.save()
        elif reader.max_borrowing > 0 and isbn.quantity > 0:
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned)
            reader.max_borrowing -= 1
            isbn.quantity -= 1
            reader.save()
            isbn.save()
            b.save()


if __name__ == '__main__':
    init_book_data()
    parser = argparse.ArgumentParser()
    parser.add_argument('data', help=u'你要生成的数据')
    args = parser.parse_args()
    if args.data == 'all':
        init_reader_data()
        init_book_data()
    elif args.data == 'book':
        init_book_data()
    elif args.data == 'reader':
        init_reader_data()
    elif args.data == 'borrowing':
        init_borrowing_data()

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Slackers.settings')
import django
django.setup()
import json
import argparse
import random
import datetime
import codecs
import os.path as op
from library.models import Book, Reader, Borrowing
from django.contrib.auth.models import User
from faker import Factory
fake = Factory.create('zh_CN')


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()
        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=
            int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]
    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'],
                author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(
            1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True
        if random.randint(1, 100) % 2 == 0:
            returned_flag = False
        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned,
                date_returned=date_returned, amount_of_fine=fine)
            b.save()
        elif reader.max_borrowing > 0 and isbn.quantity > 0:
            b = Borrowing.objects.create(reader=reader, ISBN=isbn,
                date_issued=issued, date_due_to_returned=due_to_returned)
            reader.max_borrowing -= 1
            isbn.quantity -= 1
            reader.save()
            isbn.save()
            b.save()


if __name__ == '__main__':
    init_book_data()
    parser = argparse.ArgumentParser()
    parser.add_argument('data', help=u'你要生成的数据')
    args = parser.parse_args()
    if args.data == 'all':
        init_reader_data()
        init_book_data()
    elif args.data == 'book':
        init_book_data()
    elif args.data == 'reader':
        init_reader_data()
    elif args.data == 'borrowing':
        init_borrowing_data()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Slackers.settings')

import django

django.setup()

import json
import argparse
import random
import datetime
import codecs
import os.path as op
from library.models import Book, Reader, Borrowing
from django.contrib.auth.models import User

from faker import Factory

fake = Factory.create('zh_CN')


def init_reader_data(amount=50):
    for i in range(amount):
        u = User.objects.get_or_create(username=fake.phone_number())[0]
        u.set_password('password')
        u.save()

        r = Reader.objects.get_or_create(user=u, name=fake.name(), phone=int(u.username))[0]
        r.balance = round(random.random() * 100, 2)
        r.photo = 'images/' + str(r.user_id) + '.jpg'
        r.save()


def init_book_data():
    with codecs.open('books.json', 'r', 'utf-8') as f:
        books = [json.loads(l) for l in f.readlines()]

    for b in books:
        if 'description' in b and b['description']:
            print(b)
            B = Book.objects.get_or_create(ISBN=b['ISBN'], title=b['title'], author=b['author'], press=b['press'])[0]
            B.description = b['description']
            B.price = b['price']
            B.cover = b['cover']
            B.quantity = random.randint(0, 7)
            B.save()


def init_borrowing_data(amount=50):
    for i in range(amount):
        reader = random.choice(Reader.objects.all())
        isbn = random.choice(Book.objects.all())
        issued = datetime.date.today() + datetime.timedelta(random.randint(1, 30))
        due_to_returned = issued + datetime.timedelta(30)
        date_returned = issued + datetime.timedelta(random.randint(1, 40))
        returned_flag = True

        if random.randint(1, 100) % 2 == 0:
            returned_flag = False

        if returned_flag:
            if (date_returned - issued).days > 30:
                fine = ((date_returned - issued).days - 30) * 0.1
            else:
                fine = 0

            b = Borrowing.objects.create(
                reader=reader,
                ISBN=isbn,
                date_issued=issued,
                date_due_to_returned=due_to_returned,
                date_returned=date_returned,
                amount_of_fine=fine,
            )
            b.save()
        else:

            if reader.max_borrowing > 0 and isbn.quantity > 0:
                b = Borrowing.objects.create(
                    reader=reader,
                    ISBN=isbn,
                    date_issued=issued,
                    date_due_to_returned=due_to_returned
                )

                reader.max_borrowing -= 1
                isbn.quantity -= 1
                reader.save()
                isbn.save()
                b.save()


if __name__ == '__main__':
    init_book_data()

    parser = argparse.ArgumentParser()
    parser.add_argument("data", help=u"你要生成的数据")
    args = parser.parse_args()

    if args.data == 'all':
        init_reader_data()
        init_book_data()
    elif args.data == 'book':
        init_book_data()
    elif args.data == 'reader':
        init_reader_data()
    elif args.data == 'borrowing':
        init_borrowing_data()
