Skip to content

Commit

Permalink
feat(backend): add export Skyroom credentials action (#45)
Browse files Browse the repository at this point in the history
* feat: extract emails for every w/p

* add json files to gitignore

* feat: xlsx

* feat(backend): make exporter an action & change output format to .csv

---------

Co-authored-by: Adibov <[email protected]>
  • Loading branch information
azare242 and Adibov authored Dec 5, 2023
1 parent ff55303 commit dfa9c9e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 440 deletions.
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data
media
static
static
*.json
77 changes: 53 additions & 24 deletions backend/backend_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,8 @@

from backend_api import models
from backend_api.email import MailerThread
from backend_api.models import Discount


def desc_creator(selected_model):
class AdminForm(forms.ModelForm):
desc = forms.CharField(widget=forms.Textarea)

class Meta:
model = selected_model
fields = '__all__'

class Admin(admin.ModelAdmin):
form = AdminForm
if selected_model == models.Workshop:
list_display = ('__str__', 'capacity', 'cost', 'has_project', 'level', 'no_of_participants', 'year')
readonly_fields = ('participants',)
elif selected_model == models.Presentation:
list_display = ('__str__', 'level', 'no_of_participants', 'year')
readonly_fields = ('participants',)

return Admin
from backend_api.models import Discount, Presentation
from utils.skyroom_exporter import SkyroomCredentials, convert_credentials_to_csv_response


class TeacherAdminForm(forms.ModelForm):
Expand Down Expand Up @@ -129,6 +110,54 @@ class Meta:
fields = '__all__'


class PresentationAdmin(admin.ModelAdmin):
list_display = ('__str__', 'level', 'no_of_participants', 'year')
readonly_fields = ('participants',)
actions = ['export_login_credentials']

class Meta:
model = Presentation
fields = '__all__'

@admin.action(description='Export login credentials')
def export_login_credentials(self, request, obj):
user_credentials: list[SkyroomCredentials] = []
for presentation in obj:
for presentation_registration in presentation.presentationparticipation_set.all():
user_credentials.append(
SkyroomCredentials(presentation_registration.username, presentation_registration.password,
presentation_registration.user.account.email))
return convert_credentials_to_csv_response(user_credentials)


class WorkshopAdmin(admin.ModelAdmin):
list_display = ('__str__', 'capacity', 'cost', 'has_project', 'level', 'no_of_participants', 'year')
readonly_fields = ('participants',)
actions = ['export_login_credentials']

class Meta:
model = models.Workshop
fields = '__all__'

@admin.action(description='Export login credentials')
def export_login_credentials(self, request, obj):
user_credentials: list[SkyroomCredentials] = []
for workshop in obj:
for workshop_registration in workshop.workshopregistration_set.all():
user_credentials.append(
SkyroomCredentials(workshop_registration.username, workshop_registration.password,
workshop_registration.user.account.email))
return convert_credentials_to_csv_response(user_credentials)


class MiscAdmin(admin.ModelAdmin):
list_display = ('__str__', 'year')

class Meta:
model = models.Misc
fields = '__all__'


admin.site.register(models.Teacher, TeacherAdmin)
admin.site.register(models.Presenter, PresenterAdmin)
admin.site.register(models.User, UserAdmin)
Expand All @@ -141,6 +170,6 @@ class Meta:
admin.site.register(models.Committee)
admin.site.register(models.FieldOfInterest)
admin.site.register(models.Staff)
admin.site.register(models.Workshop, desc_creator(models.Workshop))
admin.site.register(models.Presentation, desc_creator(models.Presentation))
admin.site.register(models.Misc, desc_creator(models.Misc))
admin.site.register(models.Workshop, WorkshopAdmin)
admin.site.register(models.Presentation, PresentationAdmin)
admin.site.register(models.Misc, MiscAdmin)
26 changes: 26 additions & 0 deletions backend/backend_api/migrations/0057_make_passwords_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.4 on 2023-12-05 21:26

from django.db import migrations

from utils.random import create_random_string


def make_passwords_random(apps, schema_editor):
WorkshopRegistration = apps.get_model('backend_api', 'WorkshopRegistration')
PresentationParticipation = apps.get_model('backend_api', 'PresentationParticipation')
for registration in WorkshopRegistration.objects.all():
registration.password = create_random_string(16)
registration.save()
for participation in PresentationParticipation.objects.all():
participation.password = create_random_string(16)
participation.save()


class Migration(migrations.Migration):
dependencies = [
('backend_api', '0056_presentationparticipation_password_and_more'),
]

operations = [
migrations.RunPython(make_passwords_random)
]
6 changes: 3 additions & 3 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import uuid
from urllib.parse import quote
from urllib.parse import urljoin

from django.contrib.auth.models import AbstractBaseUser
Expand All @@ -10,18 +11,17 @@
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils import timezone
from django.utils.html import escape
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError
from rest_framework import status
from django.utils.html import escape
from rest_framework.exceptions import ValidationError

from aaiss_backend import settings
from aaiss_backend.settings import BASE_URL
from backend_api import validators
from backend_api.email import MailerThread
from utils.random import create_random_string
from utils.renderers import new_detailed_response
from urllib.parse import quote

SMALL_MAX_LENGTH = 255
BIG_MAX_LENGTH = 65535
Expand Down
Binary file modified backend/requirements.txt
Binary file not shown.
Loading

0 comments on commit dfa9c9e

Please sign in to comment.