<commit_before>from robocrm.models import Machine, Event, Project, RoboResource
from django.forms import ModelForm, ValidationError
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from robocrm.models import RoboUser

class UserProfileInline(admin.StackedInline):
  model = RoboUser
  can_delete = False
  verbose_name_plural = 'profile'
  filter_horizontal = ('machines',)

  def get_fieldsets(self, request, obj=None):
    if obj:
      # change user form
      return super(UserProfileInline, self).get_fieldsets(request, obj)
    else:
      # add user form
      return (
          (None, {'fields':
            ('cell', 'class_level', 'grad_year', 'major', 'sec_major_one',
              'sec_major_two', 'club_rank', 'dues_paid', 'tshirt_rec',
              'bench_status', 'shop_status')
          }),)

class RoboUserCreationForm(ModelForm):
  # This is modelled directly after django.contrib.auth.forms.UserCreationForm 

  error_messages = UserCreationForm.error_messages
  username = UserCreationForm.declared_fields['username']
  Meta = UserCreationForm.Meta

  def clean_username(self):
    username = self.cleaned_data['username']
    try:
      User.objects.get(username=username)
    except User.DoesNotExist:
      return username
    raise ValidationError(self.error_messages['duplicate_username'])

  def save(self, commit=True):
    user = super(RoboUserCreationForm, self).save(commit=False)
    user.set_password('<PASSWORD>')
    if commit:
      user.save()
    return user

class RoboUserAdmin(UserAdmin):
  inlines = (UserProfileInline, )
  add_fieldsets = (
      (None, {'fields': ('username',)}),
      ('Personal info', {
          'fields': ('first_name', 'last_name', 'email')}),
  )
  add_form = RoboUserCreationForm
  list_display = ('username', 'email', 'first_name', 'last_name')
  search_fields = ['username', 'email', 'first_name', 'last_name']


admin.site.unregister(User)
admin.site.register(User, RoboUserAdmin)
admin.site.register(Machine)
admin.site.register(Event)
admin.site.register(Project)
admin.site.register(RoboResource)
<commit_msg>Added automatic subscription to roboclub-gb on user creation<commit_after>import string
from robocrm.models import Machine, Event, Project, RoboResource
from django.core.mail import send_mail
from django.forms import ModelForm, ValidationError
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from robocrm.models import RoboUser

class UserProfileInline(admin.StackedInline):
  model = RoboUser
  can_delete = False
  verbose_name_plural = 'profile'
  filter_horizontal = ('machines',)

  def get_fieldsets(self, request, obj=None):
    if obj:
      # change user form
      return super(UserProfileInline, self).get_fieldsets(request, obj)
    else:
      # add user form
      return (
          (None, {'fields':
            ('cell', 'class_level', 'grad_year', 'major', 'sec_major_one',
              'sec_major_two', 'club_rank', 'dues_paid', 'tshirt_rec',
              'bench_status', 'shop_status')
          }),)

def subscribe_to_list(first_name, last_name, email, listname):
  if email == '':
    return

  name = string.strip(first_name + ' ' + last_name)
  if name == '':
    from_addr = email
  else:
    from_addr = '"' + name + '" <' + email + '>'

  to_addr = listname + '-<EMAIL>'

  send_mail('', '', from_addr, [to_addr])

class RoboUserCreationForm(ModelForm):
  # This is modelled directly after django.contrib.auth.forms.UserCreationForm 

  error_messages = UserCreationForm.error_messages
  username = UserCreationForm.declared_fields['username']
  Meta = UserCreationForm.Meta

  def clean_username(self):
    username = self.cleaned_data['username']
    try:
      User.objects.get(username=username)
    except User.DoesNotExist:
      return username
    raise ValidationError(self.error_messages['duplicate_username'])

  def save(self, commit=True):
    user = super(RoboUserCreationForm, self).save(commit=False)
    user.set_password('<PASSWORD>')
    if commit:
      user.save()
    subscribe_to_list(user.first_name, user.last_name, user.email, 'roboclub-gb')
    return user

class RoboUserAdmin(UserAdmin):
  inlines = (UserProfileInline, )
  add_fieldsets = (
      (None, {'fields': ('username',)}),
      ('Personal info', {
          'fields': ('first_name', 'last_name', 'email')}),
  )
  add_form = RoboUserCreationForm
  list_display = ('username', 'email', 'first_name', 'last_name')
  search_fields = ['username', 'email', 'first_name', 'last_name']


admin.site.unregister(User)
admin.site.register(User, RoboUserAdmin)
admin.site.register(Machine)
admin.site.register(Event)
admin.site.register(Project)
admin.site.register(RoboResource)

{'fraction_non_alphanumeric': 0.1209481361426256, 'fraction_numerical': 0.0, 'mean_word_length': 4.252127659574468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '72554', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1506, 'n_words': 330}