예제 #1
0
def bulk_upload_activities(request):
    path = 'bulk_upload/activities.csv'
    with open(path, encoding="latin-1") as f:
        reader = csv.reader(f)
        for row in reader:
            activity = Activity()
            activity.name = row[0]
            activity.activity_type = row[1]
            activity.term = row[2]
            activity.location = row[3]
            activity.suburb = row[4]
            activity.postcode = row[5]
            activity.organiser = row[6]
            activity.contact_number = row[7]
            activity.description = row[8]
            activity.activity_date = row[9]
            activity.start_date = row[10]
            activity.end_date = row[11]
            activity.start_time = row[13]
            activity.end_time = row[14]
            activity.created_by = User.objects.get(username=row[15])

            # Image Upload
            if row[16] != '':
                file_dir = 'bulk_upload/images/' + row[16]
                image = open(file_dir, 'rb')
                image_file = File(image)
                activity.activity_img.save(row[16], image_file)
            # Flyer Upload
            if row[17] != '':
                file_dir = 'bulk_upload/flyers/' + row[17]
                flyer = open(file_dir, 'rb')
                flyer_file = File(flyer)
                activity.flyer.save(row[17], flyer_file)

            activity.min_age = row[18]
            activity.max_age = row[19]
            activity.background = row[20]
            activity.living_duration = row[21]
            activity.gender = row[22]
            if row[23] != '':
                activity.cost = row[23]
            if row[24] != '':
                activity.space = row[24]
            activity.cost_choice = row[25]
            activity.space_choice = row[26]
            activity.save()
        f.close()
    return HttpResponse("Success uploading activities.")
예제 #2
0
    def create(self, validated_data):
        print('Create activity')
        age = validated_data.pop('age')
        level = validated_data.pop('level')
        time = validated_data.pop('time')
        group = validated_data.pop('group')
        supervised = validated_data.pop('supervised')
        cost = validated_data.pop('cost')
        location = validated_data.pop('location')
        skills = validated_data.pop('skills')
        learning = validated_data.pop('learning')
        translations = validated_data.pop('translations')
        authors = validated_data.pop('authors')

        activity = Activity(**validated_data)

        if time:
            activity.time = MetadataOption.objects.get_or_create(**time)[0]

        if group:
            activity.group = MetadataOption.objects.get_or_create(**group)[0]

        if supervised:
            activity.supervised = MetadataOption.objects.get_or_create(
                **supervised)[0]

        if cost:
            activity.cost = MetadataOption.objects.get_or_create(**cost)[0]

        if location:
            activity.location = MetadataOption.objects.get_or_create(
                **location)[0]

        if learning:
            activity.learning = MetadataOption.objects.get_or_create(
                **learning)[0]

        activity.save()

        for age_item in age:
            activity.age.add(
                MetadataOption.objects.get_or_create(**age_item)[0])

        for level_item in level:
            activity.level.add(
                MetadataOption.objects.get_or_create(**level_item)[0])

        for skills_item in skills:
            activity.skills.add(
                MetadataOption.objects.get_or_create(**skills_item)[0])

        for author in authors:
            author.update(activity=activity)
            activity.authors.add(
                AuthorInstitution.objects.get_or_create(**author)[0])

        activity.save()

        for translation in translations:
            translation.update(master=activity)
            t = ActivityTranslation(**translation)
            t.save()

        return activity