Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate Account Creation #631

Merged
merged 6 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion huxley/api/serializers/delegate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Copyright (c) 2011-2015 Berkeley Model United Nations. All rights reserved.
# Use of this source code is governed by a BSD License (see LICENSE).

from datetime import datetime

from django.contrib.auth.base_user import BaseUserManager
from django.core.mail import send_mail

from rest_framework import serializers

from huxley.accounts.models import User
from huxley.api.serializers.assignment import AssignmentNestedSerializer
from huxley.api.serializers.school import SchoolSerializer
from huxley.core.models import Delegate
from huxley.core.models import Assignment, Delegate


class DelegateSerializer(serializers.ModelSerializer):
Expand All @@ -25,6 +31,35 @@ class Meta:
'session_three',
'session_four', )

def update(self, instance, validated_data):
if ('assignment' in validated_data and
validated_data['assignment'] != None and
not instance.assignment and
not User.objects.filter(delegate__id=instance.id).exists()):

names = instance.name.split(' ')
username = names[0] + '_' + str(instance.id)
password = BaseUserManager().make_random_password(10)
user = User.objects.create_user(
username=username,
password=password,
delegate=instance,
user_type=User.TYPE_DELEGATE,
first_name=names[0],
last_name=names[-1],
email=instance.email)

send_mail('A new account has been created for {0}!\n'.format(instance.name),
'Username: {0}\n'.format(username) \
+ 'Password: {0}\n'.format(password) \
+ 'Please save this information! You will need it for '
+ 'important information and actions. You can access '
+ 'this account at huxley.bmun.org.',
'[email protected]',
[instance.email], fail_silently=False)

return super(DelegateSerializer, self).update(instance, validated_data)


class DelegateNestedSerializer(serializers.ModelSerializer):
assignment = AssignmentNestedSerializer(read_only=True)
Expand Down
19 changes: 19 additions & 0 deletions huxley/core/migrations/0024_auto_20170927_2010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-09-27 20:10
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [('core', '0023_auto_20170809_1436'), ]

operations = [
migrations.AlterField(
model_name='delegate',
name='email',
field=models.EmailField(
default='', max_length=254),
preserve_default=False, ),
]
2 changes: 1 addition & 1 deletion huxley/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class Delegate(models.Model):
null=True,
on_delete=models.SET_NULL)
name = models.CharField(max_length=64)
email = models.EmailField(blank=True, null=True)
email = models.EmailField()
created_at = models.DateTimeField(auto_now_add=True)
summary = models.TextField(default='', blank=True, null=True)
published_summary = models.TextField(default='', blank=True, null=True)
Expand Down
3 changes: 1 addition & 2 deletions huxley/www/js/components/DelegateSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var DelegateSelect = React.createClass({
return (
<select
onChange={this.props.onChange}
value={this.props.selectedDelegateID}
disabled>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remind me why this was disabled in the first place?

Copy link
Contributor Author

@m-j-mcdonald m-j-mcdonald Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was done in #576, I assume to disable some functionality before conference. This ties into why I opened #632

value={this.props.selectedDelegateID}>
<option value="0">None</option>
{this.renderDelegateOptions()}
</select>
Expand Down