diff --git a/polls/forms.py b/polls/forms.py
index a6deff7..636f285 100644
--- a/polls/forms.py
+++ b/polls/forms.py
@@ -5,6 +5,7 @@ from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
+from .models import Question, Tag
class SignUpForm(UserCreationForm):
tailwind_class = "w-full border-2 border-gray-300 bg-gray-100 rounded-lg focus:ring focus:border-blue-300 focus:shadow-none"
@@ -45,3 +46,30 @@ class PollSearchForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
+
+
+class PollCreateForm(forms.ModelForm):
+ box_style = "w-full py-2 px-2 border-2 border-gray-300 bg-gray-100 rounded-lg focus:ring focus:border-blue-300 focus:shadow-none"
+ large_box_style = "w-full border-2 border-gray-300 bg-gray-100 rounded-lg focus:ring focus:border-blue-300 focus:shadow-none"
+
+ question_text = forms.CharField(min_length=10, max_length=100, required=True,
+ widget=forms.TextInput(attrs={'class':box_style, 'placeholder':"What is your question?"}))
+ pub_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
+ end_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
+ short_description = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'class':box_style, 'placeholder':"Short description (Maximum 200 characters)"}))
+ long_description = forms.CharField(max_length=2000, widget=forms.Textarea(attrs={'class':large_box_style, 'placeholder':"Long description (Maximum 2000 characters)"}))
+ tags = forms.MultipleChoiceField(
+ choices=[(tag.id, tag.tag_text) for tag in Tag.objects.all()],
+ widget=forms.CheckboxSelectMultiple,
+ )
+ user_choice = forms.CharField(
+ widget=forms.TextInput(attrs={'placeholder': 'Enter a choice'}),
+ required=True
+ )
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ class Meta:
+ model = Question
+ fields = ['question_text', 'pub_date', 'end_date', 'short_description', 'long_description', 'tags']
+
\ No newline at end of file
diff --git a/polls/templates/polls/creation.html b/polls/templates/polls/creation.html
new file mode 100644
index 0000000..7bdac54
--- /dev/null
+++ b/polls/templates/polls/creation.html
@@ -0,0 +1,145 @@
+{% extends "polls/base.html" %}
+
+{% block content %}
+
+
+
+
+
+
+
+{% comment %} {% endcomment %}
+
+{% endblock content %}
diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html
index bf0c9b0..35a6380 100644
--- a/polls/templates/polls/index.html
+++ b/polls/templates/polls/index.html
@@ -12,9 +12,6 @@
- {% comment %}
-
- {% endcomment %}
{% if user.is_authenticated %}
-
+
Sign out ðŸ˜
diff --git a/polls/urls.py b/polls/urls.py
index 3eb16ce..8ab0dd6 100644
--- a/polls/urls.py
+++ b/polls/urls.py
@@ -12,4 +12,5 @@ urlpatterns = [
path("upvote/", views.up_down_vote, {'vote_type' : 'upvote'}, name="upvote"),
path("downvote/", views.up_down_vote, {'vote_type' : 'downvote'}, name="downvote"),
path("search", views.search_poll, name="search_poll"),
+ path("create", views.create_poll, name="create_poll")
]
diff --git a/polls/views.py b/polls/views.py
index 118c320..4e1d5b9 100644
--- a/polls/views.py
+++ b/polls/views.py
@@ -12,7 +12,7 @@ from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.db.models import Q
-from .forms import SignUpForm, PollSearchForm
+from .forms import SignUpForm, PollSearchForm, PollCreateForm
from .models import Choice, Question, Vote
@@ -222,4 +222,41 @@ def search_poll(request):
# * If user search with empty string then show every poll.
if q == '':
results = Question.objects.all()
- return render(request, 'polls/search.html', {'form':form, 'results':results, 'q':q})
\ No newline at end of file
+ return render(request, 'polls/search.html', {'form':form, 'results':results, 'q':q})
+
+
+@login_required
+def create_poll(request):
+ ip = get_client_ip(request)
+ if request.method == 'POST':
+ form = PollCreateForm(request.POST)
+ if form.is_valid():
+ question_text = form.cleaned_data['question_text']
+ pub_date = form.cleaned_data['pub_date']
+ end_date = form.cleaned_data['end_date']
+ short_description = form.cleaned_data['short_description']
+ long_description = form.cleaned_data.get('long_description', '')
+ user_choices = form.cleaned_data['user_choice']
+ tags = form.cleaned_data['tags']
+
+ question = Question.objects.create(
+ question_text=question_text,
+ pub_date=pub_date,
+ end_date=end_date,
+ short_description=short_description,
+ long_description=long_description,
+ )
+
+ choices = user_choices.split(',') # Split with comma
+ for choice_text in choices:
+ Choice.objects.create(question=question, choice_text=choice_text.strip())
+
+ # Add tags to the question
+ question.tags.set(tags)
+ logger.info(f"User {request.user.username} ({ip}) create poll : {question_text}")
+ return redirect('polls:index')
+
+ else:
+ form = PollCreateForm()
+
+ return render(request, 'polls/creation.html', {'form': form})