Skip to content
This repository was archived by the owner on Jul 7, 2019. It is now read-only.

Add a "Tag" model for event classification (Issue #161). #176

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions ACM_General/events/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

# Django
from django.forms import ModelForm
from django.forms.widgets import DateTimeInput, Textarea, TextInput
from django.forms.widgets import DateTimeInput, Textarea, TextInput, CheckboxSelectMultiple

# local Django
from .models import Event
from .models import Event, Tag


class EventForm(ModelForm):
"""
This class is used for combining the Event Class model with a form
(ModelForm).
"""

class Meta:
model = Event
exclude = ('id', 'creator', 'date_created')
Expand All @@ -24,4 +25,8 @@ class Meta:
'title': Textarea(attrs={'rows': 3}),
'description': Textarea(attrs={'rows': 3}),
'link': TextInput(),
'tags': CheckboxSelectMultiple(),
}
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['tags'].queryset = Tag.objects.filter(is_disabled=False)
26 changes: 26 additions & 0 deletions ACM_General/events/migrations/0002_auto_20180330_0722.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.0.3 on 2018-03-30 07:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('events', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Tag',
fields=[
('display_name', models.CharField(help_text='The name of the tag that will be displayed to users.', max_length=256, unique=True, verbose_name='Event Tag')),
('id', models.CharField(help_text='Unique ID of the tag.', max_length=256, primary_key=True, serialize=False, verbose_name='ID')),
('is_disabled', models.BooleanField(default=True, help_text='Whether or not the tag is to be displayed.', verbose_name='Display')),
],
),
migrations.AddField(
model_name='event',
name='tags',
field=models.ManyToManyField(help_text='The name of the Tag that will be displayed to users', to='events.Tag', verbose_name='Event Tags'),
),
]
18 changes: 18 additions & 0 deletions ACM_General/events/migrations/0003_auto_20180408_0410.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.3 on 2018-04-08 04:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('events', '0002_auto_20180330_0722'),
]

operations = [
migrations.AlterField(
model_name='event',
name='tags',
field=models.ManyToManyField(blank=True, help_text='The name of the Tag that will be displayed to users', to='events.Tag', verbose_name='Event Tags'),
),
]
48 changes: 46 additions & 2 deletions ACM_General/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ def get_path_for_flier(instance, filename):
)


class Tag(models.Model):
"""
Model used for adding tags on events.
"""

#: The name of the tag that is displayed; represented as a CharField.
display_name = models.CharField(
verbose_name=_('Event Tag'),
help_text=_('The name of the tag that will be displayed to users.'),
unique=True,
max_length=256,
)

#: Unique id of the tag; represented as an CharField.
id = models.CharField(
verbose_name=_('ID'),
help_text=_('Unique ID of the tag.'),
primary_key=True,
max_length=256,
)

#: If the tag should be diplayed; represented as a BooleanField.
is_disabled = models.BooleanField(
verbose_name=_('Display'),
help_text=_('Whether or not the tag is to be displayed.'),
default = True,
)

def __str__(self):
"""
:returns: Display the name of the tag.
:rtype: str
"""
return self.display_name


class Event(models.Model):
"""
Class used to define what is needed when creating new events.
Expand Down Expand Up @@ -154,13 +190,21 @@ class Event(models.Model):
blank=True,
)

#: Tags available to attach to events; represented as a ManyToManyField.
tags = models.ManyToManyField(
Tag,
verbose_name=_('Event Tags'),
help_text=_('The name of the Tag that will be displayed to users'),
blank=True,
)

@property
def is_active(self):
"""
Checking whether or not an event has already expired
(gone past the current date).

:return: Bool value representing whether or not the event is
:returns: Bool value representing whether or not the event is
considered 'active'.
:rtype: bool
"""
Expand All @@ -171,7 +215,7 @@ def clean(self):
The clean function is used for making checks on the data posted to the
form.

:return: None.
:returns: None.
:rtype: None

:raises ValidationError: if date_expire or date_hosted are invalid.
Expand Down
6 changes: 6 additions & 0 deletions ACM_General/events/static/events/css/create-event.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ a {
color: white;
}

ul {
list-style-type: none;
margin: 0;
padding: 0;
}

input, textarea, option {
font-size: 15px;
}
Expand Down
6 changes: 4 additions & 2 deletions ACM_General/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def create_event(request):
- If the user is submitting a GET request, it will send them to the
blank create event page.
- If the user is submitting a POST request:
- If the form is valid, it will save the event to the database and
redirect the user to the homepage.
- If the form is valid, it will save the event as well as the
ManyToMany relation to the database and redirect the user to the
homepage.
- If the form is invalid, the user will be redirected back to the
same create event page with same information that they filled
out, but also with information that now explains the errors that
Expand All @@ -74,6 +75,7 @@ def create_event(request):
event = form.save(commit=False)
event.creator = request.user
event.save()
form.save_m2m()
return HttpResponseRedirect("/")

return render(request, 'events/create-event.html', {'form': form})
Expand Down