|
| 1 | +from django.contrib import admin, messages |
| 2 | +from django.shortcuts import render, redirect |
| 3 | +from django.urls import path |
| 4 | +from django.db import transaction |
| 5 | +from django.contrib.auth.models import User |
| 6 | + |
| 7 | +import io |
| 8 | +import csv |
| 9 | +from hknweb.coursesemester.models import Semester |
| 10 | +from hknweb.candidate.models import BitByteGroup |
| 11 | +from hknweb.forms import CsvImportForm |
| 12 | + |
| 13 | + |
| 14 | +@admin.register(BitByteGroup) |
| 15 | +class BitByteGroupAdmin(admin.ModelAdmin): |
| 16 | + change_list_template = "admin/candidate/bitbyte_group_change_list.html" |
| 17 | + |
| 18 | + fields = ["semester", "bytes", "bits"] |
| 19 | + list_display = ( |
| 20 | + "semester", |
| 21 | + "bytes_usernames", |
| 22 | + "bits_usernames", |
| 23 | + ) |
| 24 | + list_filter = ["semester"] |
| 25 | + search_fields = [ |
| 26 | + "bytes__username", |
| 27 | + "bytes__first_name", |
| 28 | + "bytes__last_name", |
| 29 | + "bits__username", |
| 30 | + "bits__first_name", |
| 31 | + "bits__last_name", |
| 32 | + ] |
| 33 | + autocomplete_fields = ["bytes", "bits"] |
| 34 | + |
| 35 | + def bytes_usernames(self, obj): |
| 36 | + return ", ".join([user.username for user in obj.bytes.all()]) |
| 37 | + |
| 38 | + def bits_usernames(self, obj): |
| 39 | + return ", ".join([user.username for user in obj.bits.all()]) |
| 40 | + |
| 41 | + def get_urls(self): |
| 42 | + urls = super().get_urls() |
| 43 | + custom_urls = [ |
| 44 | + path("import-csv/", self.import_csv, name="import_csv"), |
| 45 | + ] |
| 46 | + return custom_urls + urls |
| 47 | + |
| 48 | + # Refactor and move this outside of admin panel if this becomes front facing feature(i.e. you |
| 49 | + # want evp creating bitbyte groups) |
| 50 | + def import_csv(self, request): |
| 51 | + # Helper function. |
| 52 | + def _get_user_or_error(username): |
| 53 | + user = User.objects.filter(username__iexact=username).first() |
| 54 | + if not user: |
| 55 | + raise ValueError( |
| 56 | + f"Error on trying to add '{username}'." |
| 57 | + f" Check if the username is correct." |
| 58 | + ) |
| 59 | + return user |
| 60 | + |
| 61 | + if request.method == "POST": |
| 62 | + form = CsvImportForm(request.POST, request.FILES) |
| 63 | + if form.is_valid(): |
| 64 | + csv_file = request.FILES["csv_file"] |
| 65 | + data_set = csv_file.read().decode("UTF-8") |
| 66 | + io_string = io.StringIO(data_set) |
| 67 | + next(io_string) # Skip header |
| 68 | + |
| 69 | + try: |
| 70 | + # If an error happens we just don't do anything |
| 71 | + with transaction.atomic(): |
| 72 | + count = 0 |
| 73 | + for row in csv.reader(io_string, delimiter=";"): |
| 74 | + if not row or len(row) != 4: |
| 75 | + continue |
| 76 | + |
| 77 | + byte_usernames, bit_usernames, year, sem = [ |
| 78 | + s.strip() for s in row |
| 79 | + ] |
| 80 | + |
| 81 | + semester_obj = Semester.objects.get(year=year, semester=sem) |
| 82 | + group = BitByteGroup.objects.create(semester=semester_obj) |
| 83 | + for byte in byte_usernames.split(","): |
| 84 | + byte_user = _get_user_or_error(byte) |
| 85 | + group.bytes.add(byte_user) |
| 86 | + |
| 87 | + for bit in bit_usernames.split(","): |
| 88 | + bit_user = _get_user_or_error(bit) |
| 89 | + group.bits.add(bit_user) |
| 90 | + |
| 91 | + count += 1 |
| 92 | + self.message_user( |
| 93 | + request, f"Successfully imported {count} relationships." |
| 94 | + ) |
| 95 | + return redirect("..") |
| 96 | + except Exception as e: |
| 97 | + self.message_user(request, f"Error: {e}", level=messages.ERROR) |
| 98 | + |
| 99 | + form = CsvImportForm() |
| 100 | + payload = {"form": form} |
| 101 | + return render(request, "admin/candidate/bitbyte_group_csv_upload.html", payload) |
0 commit comments