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

#A02 - As admin, I would like to create new hackathon events #99

Merged
merged 27 commits into from
Oct 24, 2020
Merged

#A02 - As admin, I would like to create new hackathon events #99

merged 27 commits into from
Oct 24, 2020

Conversation

hebs87
Copy link
Contributor

@hebs87 hebs87 commented Oct 23, 2020

User Story ID:

  • #A02 ( As admin, I would like to create new hackathon events)
  • Feature

Description of PR:

All changes made are outlined below:

  • Added 'theme' field to Hackathon model to enable setting a theme when creating an event.
  • Added blank=True, null=True attributes to required Hackathon model fields to ensure they are required in the form.
  • Created HackathonForm ModelForm to enable rendering form with relevant fields in the frontend, and to enable basic form validation.
  • Created form template - create-events.html
  • Added datetimepicker to form template for start_date and end_date fields to enable selecting both date and time in same field.
  • Added the following properties to the datetimepicker (can be amended if needed):
    • Time step set to 30 mins.
    • Min and default start date set to tomorrow's date.
    • Min and default end date set to day after tomorrow's date.
    • Min selectable time is 8am and max selectable time is 6pm.
    • Week starts on Monday.
    • Year starts on current year (unable to select earlier year).
  • Created create_event view to render HTML template, and with following logic:
    • GET - checks if user is admin, redirects to hackathon list page if not.
    • POST - Ensures start date is tomorrow or later and that end date is at least 1 day after start date:
      • If not, display relevant error message and re-render form.
      • If yes, submit form and redirect to hackathon list.
      • If all checks pass, form is submitted and user is redirected to the hackathons list page with the newly added event visible.
  • Added 'Create Event' link to 'Hackathons' nav menu dropdown in base.html, only for admin users
  • Added 'Create' button to hackathon list page - hackathon_list.html - only for admin users

Tests:

Manually tested all implemented functionality, which is all working as expected.
Validated all code in relevant validators and all passed - HTML validator threw errors for Django template tags, but no other errors.

Know bugs/errors:

No know bugs or errors after testing.

Screenshots:

image

image

image

Does this introduce a breaking change

  • Yes
  • [] No

# Conflicts:
#	.gitignore
#	hackathon/urls.py
#	hackathon/views.py
@hebs87 hebs87 changed the title Feature/admin create hackathon #A02 - As admin, I would like to create new hackathon events Oct 23, 2020
Copy link
Member

@stefdworschak stefdworschak left a comment

Choose a reason for hiding this comment

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

@hebs87 This is a really good PR! Just a few general comments.

My main concern is that you changed and deleted quite a lot of migrations from what I can see. Could you please see what happened here and if instead of changing existing migrations can create new migrations making the changes you want?

@@ -1,18 +0,0 @@
# Generated by Django 3.1.1 on 2020-10-22 15:53
Copy link
Member

Choose a reason for hiding this comment

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

Why did you remove this migration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After pulling in and merging the latest PR, I kept getting an error when trying to navigate to the create_event page, which was to do with the CustomUser model. StackOverflow suggested that I clear the existing migrations and then run the makemigrations and migrate commands again, which resolved the issue. How do I get the previous migrations back?

Copy link
Member

Choose a reason for hiding this comment

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

When this happens what I would first do is just try to delete the db.sqlite3 file. If you have an existing file that you have done work on and others it might happen that there are issues then with the migrations. By deleting the database this should give you a clean slate. Running makemigrations then should just show "No changes needed" and on migrate it would just recreate the db with the most up-to-date models.

Copy link
Member

@stefdworschak stefdworschak Oct 23, 2020

Choose a reason for hiding this comment

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

To remove the the changes to the migrations you can run git revert <commit_id> (this will delete the changes, but keep the commit history) and you will then have to push the changes to your fork (probably with -f at the end). You can get the commit ID from the list of commits above or by running git log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I'll keep that in mind going forward and delete the db.sqlite3 file as the first port of call 👍 I ran into some trouble with reverting the branch though, so I had to do a hard reset instead and force push to the branch. A couple of my previous commits are now gone from the history, but all the migration files are reverted and my new changes are also added back in.



class HackathonForm(forms.ModelForm):
display_name = forms.CharField(
Copy link
Member

Choose a reason for hiding this comment

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

Please add a docstring for the class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I normally do add docstrings! This one must have slipped the net. I've added this now and it will be visible when I push the changes.

description = models.TextField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
display_name = models.CharField(default="", max_length=254, blank=False, null=True)
Copy link
Member

Choose a reason for hiding this comment

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

Why are you adding blank=False, null=True for all of the below fields? They are all required so should not be null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was just from previous experience - when setting a field on an existing model to blank=False and there are existing records in the DB, I've received errors in the past when running the migrations again, or it has asked me to set a value for any existing blank fields. To resolve this, I've had to set null=True on the relevant fields, so this is a force of habit! I've removed null=True from the relevant fields now and it will be visible when I push the changes to the existing PR.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. That's fine, but I think it's either null=True or set a default. And as per Django documentation default="" for Char and TextFields is the preferred solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perfect, makes sense. I'll keep that in mind going forward 👍

{% endblock %}

{% block js %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js"
Copy link
Member

Choose a reason for hiding this comment

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

Do you need this and the CDN at the top of the template as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When you mention the CDN at the top of the template, are you referring to the link tag? If so, yes they are both needed. The link tag is for the datetimepicker CSS, and the script tag is for the relevant JS.

Copy link
Member

Choose a reason for hiding this comment

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

Cool. That's okay then

integrity="sha256-FEqEelWI3WouFOo2VWP/uJfs1y8KJ++FLh2Lbqc8SJk=" crossorigin="anonymous">
</script>
<script>
$(document).ready(function () {
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 please extract the JS to an external JS script file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The datetimepicker wouldn't render when I moved the JS into script.js, so I've had to move it into a new JS file - datetimepicker.js - and import it into the create_event.html template. This change will be visible once it is pushed to the existing PR.


template = "hackathon/create-event.html"
form = HackathonForm()
context = {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure you need to create a context dict just for one key. I'd just pass the form dict directly to the render function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just a personal preference, but changed as requested.

def create_event(request):
""" Allow users to create hackathon event """

if request.method == 'GET':
Copy link
Member

Choose a reason for hiding this comment

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

I would put this in an else clause for the if request.method == 'POST' instead of having two serparate if statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, this is my employer's preference, which is why I did it this way. I've changed that line to an else clause instead now.

# Submit form and save record
if form.is_valid():
form.instance.created_by = request.user
form.instance.start_date = start_date
Copy link
Member

Choose a reason for hiding this comment

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

Do you need to actually assign start and end_date again or should that not be in the POST already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did this becuase I've formatted the start_date and end_date a couple of lines above it (for date validation), so I passed in the formatted value to the form fields. But, you're right, I've added the input format to the relevant form field anyway so these two lines are unneeded. I've removed them now.

@@ -1,166 +1,170 @@
:root {
Copy link
Member

Choose a reason for hiding this comment

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

what's happended here? Did you replace the whole file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, not sure why it's showing like that there. I added a class but didn't change anything else in the file. I had to re-add the class once I pulled and merged the latest PR, so maybe that may have thrown git? I've checked the repo though and all the existing classes are still there, as well as my new class.

@@ -18,6 +18,10 @@
</a>
<div class="dropdown-menu" aria-labelledby="navbarHackathons">
<a class="dropdown-item" href="{% url 'hackathon:hackathon-list' %}">Events</a>
Copy link
Member

Choose a reason for hiding this comment

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

In line with my earlier comment, I would rename "Events" to "View Hackathons" and "Create Event" to "Create Hackathon" please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. The "Events" link wasn't one that I added by the way, but I've changed it while in the file.

Copy link
Member

@stefdworschak stefdworschak left a comment

Choose a reason for hiding this comment

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

LGTM

@stefdworschak stefdworschak merged commit 0c629a1 into Code-Institute-Community:master Oct 24, 2020
@stefdworschak stefdworschak added the hacktoberfest-accepted Accepted PR during Hacktoberfest label Oct 24, 2020
@stefdworschak stefdworschak linked an issue Oct 25, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accepted PR during Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#A02. As Admin, I would like to create new Hackathon events.
2 participants