Skip to content

Commit

Permalink
Add role to alter of users only use on local/staging
Browse files Browse the repository at this point in the history
  • Loading branch information
VenturaFranklin committed Aug 11, 2019
1 parent da0e967 commit 3c3bc39
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
8 changes: 5 additions & 3 deletions thetatauCMT/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tempus_dominus.widgets import DatePicker
from core.models import BIENNIUM_YEARS
from chapters.models import Chapter, ChapterCurricula
from .models import UserAlterChapter, User, UserSemesterGPA,\
from .models import UserAlter, User, UserSemesterGPA,\
UserSemesterServiceHours, UserOrgParticipate


Expand Down Expand Up @@ -83,9 +83,11 @@ class UserLookupForm(forms.Form):


class UserAlterForm(forms.ModelForm):
role = forms.ChoiceField(choices=UserAlter.ROLES, required=False)

class Meta:
model = UserAlterChapter
fields = ['chapter']
model = UserAlter
fields = ['chapter', 'role']


class UserForm(forms.ModelForm):
Expand Down
28 changes: 28 additions & 0 deletions thetatauCMT/users/migrations/0027_auto_20190810_1635.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.2.3 on 2019-08-10 23:35

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('chapters', '0011_auto_20190222_0946'),
('users', '0026_auto_20190217_1202'),
]

operations = [
migrations.CreateModel(
name='UserAlter',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('role', models.CharField(choices=[('scribe', 'Scribe'), ('regent', 'Regent'), ('treasurer', 'Treasurer'), ('vice regent', 'Vice Regent'), ('corresponding secretary', 'Corresponding Secretary')], max_length=50, null=True)),
('chapter', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='altered_member', to='chapters.Chapter')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='altered', to=settings.AUTH_USER_MODEL)),
],
),
migrations.DeleteModel(
name='UserAlterChapter',
),
]
23 changes: 18 additions & 5 deletions thetatauCMT/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
RegexValidator
from address.models import AddressField
from core.models import StartEndModel, YearTermModel, TODAY_END, CHAPTER_OFFICER, \
CHAPTER_ROLES_CHOICES, TimeStampedModel, NATIONAL_OFFICER, COL_OFFICER_ALIGN
CHAPTER_ROLES_CHOICES, TimeStampedModel, NATIONAL_OFFICER, COL_OFFICER_ALIGN,\
CHAPTER_OFFICER_CHOICES
from chapters.models import Chapter


Expand Down Expand Up @@ -60,8 +61,8 @@ def current_chapter(self):
# without actually changing their chapter
chapter = self.chapter
if self.groups.filter(name='natoff').exists():
if self.altered_chapter.all():
chapter = self.altered_chapter.first().chapter
if self.altered.all():
chapter = self.altered.first().chapter
return chapter

def get_absolute_url(self):
Expand Down Expand Up @@ -94,6 +95,12 @@ def chapter_officer(self):
current_roles.add(role_name)
# officer = not current_roles.isdisjoint(CHAPTER_OFFICER)
officer_roles = CHAPTER_OFFICER & current_roles
if self.is_national_officer_group:
if 'local' in settings.SETTINGS_MODULE or 'staging' in settings.SETTINGS_MODULE:
if self.altered.all():
new_role = self.altered.first().role
if new_role is not None:
officer_roles.add(new_role)
return officer_roles

@property
Expand All @@ -104,6 +111,10 @@ def is_national_officer_group(self):
def is_chapter_officer_group(self):
return self.groups.filter(name='officer').exists()

@property
def is_officer_group(self):
return self.is_national_officer_group or self.is_chapter_officer_group

def is_national_officer(self):
role_objs = self.get_current_roles()
officer = False
Expand All @@ -129,17 +140,19 @@ def is_officer_group(self):
return set(groups).intersection(set(user_groups))


class UserAlterChapter(models.Model):
class UserAlter(models.Model):
'''
This is used for altering things for natoffs
ie. when a natoff wants to check things for another chapter
'''
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="altered_chapter")
related_name="altered")
chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE,
default=1,
related_name="altered_member")
ROLES = CHAPTER_OFFICER_CHOICES + [(None, '------------')]
role = models.CharField(max_length=50, choices=ROLES, null=True)


class UserSemesterServiceHours(YearTermModel):
Expand Down
8 changes: 6 additions & 2 deletions thetatauCMT/users/templatetags/custom_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django import template
from ..forms import UserAlterForm
from ..models import UserAlterChapter

register = template.Library()

Expand All @@ -11,5 +10,10 @@ def user_alter_form(context):
if request:
user = context['request'].user
if not user.is_anonymous and user.is_national_officer_group:
return UserAlterForm(data={'chapter': user.current_chapter.pk})
new_role = None
if user.altered.all():
new_role = user.altered.first().role
return UserAlterForm(
data={'chapter': user.current_chapter.pk,
'role': new_role, })
return None
10 changes: 6 additions & 4 deletions thetatauCMT/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from core.models import TODAY_END, annotate_role_status, combine_annotations,\
BIENNIUM_YEARS
from dal import autocomplete
from .models import User, UserAlterChapter, UserSemesterGPA,\
from .models import User, UserAlter, UserSemesterGPA,\
UserSemesterServiceHours, UserOrgParticipate
from .tables import UserTable
from .filters import UserListFilter
Expand Down Expand Up @@ -291,7 +291,7 @@ def get_queryset(self):


class UserAlterView(NatOfficerRequiredMixin, LoginRequiredMixin, FormView):
model = UserAlterChapter
model = UserAlter
form_class = UserAlterForm

def get_success_url(self):
Expand All @@ -306,14 +306,16 @@ def form_valid(self, form):
user = self.request.user
form.instance.user = user
try:
instance = UserAlterChapter.objects.get(user=user)
except UserAlterChapter.DoesNotExist:
instance = UserAlter.objects.get(user=user)
except UserAlter.DoesNotExist:
instance = None
if self.request.POST['alter-action'] == 'Reset':
form.instance.chapter = self.request.user.chapter # This should remain origin chapter
form.instance.role = None
form.is_valid()
if instance:
instance.chapter = form.instance.chapter
instance.role = form.instance.role
instance.save()
else:
form.save()
Expand Down

0 comments on commit 3c3bc39

Please sign in to comment.