def handle(self, *args, **options):
        with open(options['file'], 'r') as csvfile:
            students = csv.reader(csvfile, delimiter=',')
            num = 0

            ceitba_service = Service.objects.get(
                name=config.CEITBA_SERVICE_NAME)

            for row in students:
                try:
                    student = Student.objects.get(student_id=row[0])

                    enrollment = Enrollment(student=student,
                                            service=ceitba_service)
                    enrollment.save()

                except Student.DoesNotExist:
                    self.stdout.write(
                        self.style.ERROR(
                            u'Student with ID = %s, Name = %s does not exist' %
                            (row[0], row[1])))

                    student = Student(name=row[1])
                    student.student_id = row[0]
                    student.save(add_ceitba_enrollment=True)

                num += 1

                if num % 100 == 0:
                    self.stdout.write(
                        self.style.NOTICE('Updated %d students so far' % num))

            self.stdout.write(
                self.style.SUCCESS('Update finished!\n\tUpdated %d students' %
                                   num))
示例#2
0
    def setUpTestData(cls):

        logger.info('Populating database.')
        with open('./names.txt') as f:
            names = f.readlines()

        if names is None:
            raise Exception("Couldn't find names")

        random.seed()

        for service, price in TEST_SERVICES:
            logger.info("Adding service %s" % service)
            s = Service(name=service, price=price)
            s.save()

        for name, i in zip(names, range(0, len(names))):
            student = Student(name=name,
                              date_created=rand_date(START_RAND_YEAR,
                                                     END_RAND_YEAR))
            student.student_id = i
            student.save()

            # print "Added student %s" % student.name

        for product in PRODUCTS:
            p = Product(name=product, price=random.randint(1, 300))
            p.save()
    def test_member_invalid_mail(self):
        student = Student()
        student.name = STUDENT_NAME
        student.student_id = STUDENT_ID
        student.email = STUDENT_INVALID_EMAIL

        with self.assertRaises(ValidationError):
            student.full_clean()
    def test_member_valid_email(self):
        student = Student()
        student.name = STUDENT_NAME
        student.student_id = STUDENT_ID
        student.email = STUDENT_VALID_EMAIL

        student.full_clean()
        student.save()

        self.assertIsNotNone(student.pk)
        self.assertTrue(student.is_active())
    def test_member_remove(self):
        student = Student()
        student.name = STUDENT_NAME
        student.student_id = STUDENT_ID
        student.email = STUDENT_VALID_EMAIL

        student.full_clean()
        student.save()

        self.assertIsNotNone(student.pk)
        self.assertIsNone(student.date_removed)

        student.delete()

        self.assertIsNotNone(student.date_removed)
    def handle(self, *args, **options):
        with open(options['file'], 'r') as csvfile:
            students = csv.reader(csvfile, delimiter=',')
            num = 0
            for row in students:

                student_exist = Student.objects.filter(student_id=row[0]).exists()

                if student_exist:
                    self.stdout.write(self.style.WARNING('Student [%s] %s already exists' % (row[0], row[1])))
                    continue

                student = Student(name=row[1])
                student.student_id = row[0]
                student.save(add_ceitba_enrollment=False)
                num += 1
                if num % 100 == 0:
                    self.stdout.write(self.style.NOTICE('Imported %d students so far' % num))

            self.stdout.write(self.style.SUCCESS('Import finished!\n\tImported %d students' % num))
示例#7
0
    def handle(self, *args, **options):
        with open(options['file'], 'r') as csvfile:
            students = csv.reader(csvfile, delimiter=',')
            num = 0

            for row in students:
                student = Student.objects.filter(student_id=row[0])

                if not student.exists():
                    new_student = Student(name=row[1])
                    new_student.student_id = row[0]
                    new_student.dni = row[2]
                    new_student.save(add_ceitba_enrollment=True)

                num += 1

                if num % 100 == 0:
                    self.stdout.write(
                        self.style.NOTICE('Added %d students so far' % num))

            self.stdout.write(
                self.style.SUCCESS('Update finished!\n\tUpdated %d students' %
                                   num))