ankpan18 commited on
Commit
13198fd
·
1 Parent(s): 1ae15f5

version 1

Browse files
polls/models.py CHANGED
@@ -27,7 +27,7 @@ class Question(models.Model):
27
 
28
  class Choice(models.Model):
29
 
30
- question = models.ForeignKey(Question, on_delete=models.CASCADE)
31
  choice_text = models.CharField(max_length=200)
32
 
33
  def __str__(self):
@@ -44,4 +44,8 @@ class Choice(models.Model):
44
 
45
  class Vote(models.Model):
46
  user = models.ForeignKey(User, on_delete=models.CASCADE)
47
- choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
 
 
 
 
 
27
 
28
  class Choice(models.Model):
29
 
30
+ question = models.ForeignKey(Question, on_delete=models.CASCADE,related_name="choice_set")
31
  choice_text = models.CharField(max_length=200)
32
 
33
  def __str__(self):
 
44
 
45
  class Vote(models.Model):
46
  user = models.ForeignKey(User, on_delete=models.CASCADE)
47
+ choice = models.ForeignKey(Choice, on_delete=models.CASCADE,related_name="vote_set")
48
+
49
+
50
+ # def user_voted_questions(self,user):
51
+ # return self.objects
polls/static/polls/header.css CHANGED
@@ -12,9 +12,15 @@ header{
12
  justify-content: space-between;
13
  padding: 0 80px;
14
  }
15
- header>#app_name{
16
  font-size: larger;
17
  }
 
 
 
 
 
 
18
 
19
  #profile{
20
  background-color: #00000058;
 
12
  justify-content: space-between;
13
  padding: 0 80px;
14
  }
15
+ #app_name{
16
  font-size: larger;
17
  }
18
+ a{
19
+ padding: 0;
20
+ margin: 0;
21
+ text-decoration: none;
22
+ color:unset;
23
+ }
24
 
25
  #profile{
26
  background-color: #00000058;
polls/static/polls/index.css CHANGED
@@ -113,7 +113,7 @@
113
  align-items: center;
114
 
115
  }
116
- .question_header>.right_section>img{
117
  fill: white;
118
  height: 25px;
119
 
@@ -188,4 +188,19 @@
188
 
189
  .vote_btn:hover{
190
  cursor: pointer;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  }
 
113
  align-items: center;
114
 
115
  }
116
+ .question_header>.right_section>a>img{
117
  fill: white;
118
  height: 25px;
119
 
 
188
 
189
  .vote_btn:hover{
190
  cursor: pointer;
191
+ }
192
+
193
+ .delete_btn{
194
+ margin: 10px 0;
195
+ border:none;
196
+ outline: none;
197
+
198
+ background-color: red;
199
+ color: white;
200
+ padding: 10px 30px;
201
+ border-radius: 8px;
202
+ }
203
+
204
+ .delete_btn:hover{
205
+ cursor: pointer;
206
  }
polls/static/polls/index.js CHANGED
@@ -55,6 +55,7 @@ function validate(elem){
55
  }
56
 
57
  function Vote(elem){
 
58
  if(!validate(elem.parentElement)) return;
59
 
60
  var data_to_send = {};
@@ -62,7 +63,7 @@ function Vote(elem){
62
 
63
  data_to_send["choice_id"]=elem.parentElement.querySelector('.choice.active').dataset.id;
64
 
65
- fetch("vote/",{
66
  method:"POST",
67
  headers:{
68
  'X-CSRFToken': csrftoken
 
55
  }
56
 
57
  function Vote(elem){
58
+ elem=elem.parentElement;
59
  if(!validate(elem.parentElement)) return;
60
 
61
  var data_to_send = {};
 
63
 
64
  data_to_send["choice_id"]=elem.parentElement.querySelector('.choice.active').dataset.id;
65
 
66
+ fetch(vote_url,{
67
  method:"POST",
68
  headers:{
69
  'X-CSRFToken': csrftoken
polls/static/polls/your_polls.css CHANGED
@@ -1,180 +0,0 @@
1
- /* .questions{
2
- display: flex;
3
- flex-wrap: wrap;
4
- }
5
- .question{
6
- background-color: rgb(133, 127, 255);
7
- color: white;
8
- padding: 4px;
9
- margin:20px 4px;
10
- width:140px;
11
- height: 150px;
12
- border-radius: 10px;
13
- position:relative;
14
- }
15
- .question>.question_text{
16
- font: caption;
17
- margin: 0px;
18
- padding: 0px;
19
- margin-bottom: 14px;
20
- }
21
- .question:hover{
22
- cursor: pointer;
23
- }
24
-
25
- .total_count{
26
- font:-webkit-control;
27
- color: black;
28
- position:absolute;
29
- bottom: 5px;
30
- right: 5px;
31
- padding: 0;
32
- margin: 0;
33
- } */
34
-
35
-
36
- /***********************New css*****************************/
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
- /*********************sub header***************************/
46
- #sub_header{
47
- font-weight:100;
48
- padding:40px 40px;
49
- padding-bottom: 0px;
50
- }
51
-
52
-
53
- /*******************main container*******************************/
54
-
55
- #main_container{
56
- display: flex;
57
- flex-direction: column;
58
- align-items: center;
59
-
60
- }
61
-
62
- .question_container{
63
- width:500px;
64
-
65
- color:#ffffff;
66
- background-color: #5B66C6;
67
-
68
- border-radius: 7px;
69
- box-shadow: 0px 20px 52px 0px #0000004b;
70
- overflow: hidden;
71
- margin-top:40px;
72
- }
73
- .question_container:last-child{
74
- margin-bottom:40px;
75
- }
76
-
77
- .question_header{
78
- padding: 10px 20px;
79
- display: flex;
80
- flex-direction: row;
81
- align-items: center;
82
- justify-content: space-between;
83
-
84
- }
85
-
86
- .question_meta{
87
- display: flex;
88
- flex-direction: column;
89
- align-items: center;
90
- justify-content: center;
91
- margin-right: 30px;
92
-
93
- font-size: small;
94
- font-weight: 100;
95
- }
96
-
97
- .question_header>.question_text{
98
- width: 60%;
99
- color:white;
100
- background-color: #5B66C6;
101
- border:none;
102
- outline: none;
103
-
104
- overflow: hidden;
105
- resize: none;
106
- }
107
- .question_header>.right_section{
108
- display: flex;
109
- align-items: center;
110
-
111
- }
112
- .question_header>.right_section>img{
113
- fill: white;
114
- height: 25px;
115
-
116
-
117
- }
118
-
119
- .question_container>.form_container{
120
- box-sizing: border-box;
121
- padding: 10px 20px;
122
- width: 100%;
123
- border-radius: 7px;
124
-
125
- background-color: white;
126
-
127
-
128
- display:flex;
129
- flex-direction: column;
130
- align-items: center;
131
-
132
- }
133
- .choices{
134
- margin-top: 30px;
135
- width: 100%;
136
- }
137
-
138
- .choice{
139
- margin: 10px 0;
140
- padding: 10px 20px;
141
-
142
- background-color: #5b66c670;
143
-
144
- border-radius: 3px;
145
- display: flex;
146
- }
147
-
148
- .choice_text{
149
- margin-left: 20px;
150
- color:white;
151
- background-color:transparent;
152
- border:none;
153
- outline: none;
154
- overflow: hidden;
155
- resize: none;
156
- }
157
- .choice_text:hover{
158
- cursor:pointer;
159
- }
160
-
161
- .choice:hover{
162
- background-color: #5B66C6;
163
- /* color:#C3C3C3; */
164
- cursor: pointer;
165
- }
166
-
167
- .vote_btn{
168
- margin: 10px 0;
169
- border:none;
170
- outline: none;
171
-
172
- background-color: #5B66C6;
173
- color: white;
174
- padding: 10px 30px;
175
- border-radius: 8px;
176
- }
177
-
178
- .vote_btn:hover{
179
- cursor: pointer;
180
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
polls/static/polls/your_polls.js CHANGED
@@ -1,64 +1,29 @@
 
 
 
1
 
2
- var tx = document.getElementsByTagName('textarea');
3
- for (var i = 0; i < tx.length; i++) {
4
- tx[i].setAttribute('style', 'height:' + (tx[i].scrollHeight) + 'px;overflow-y:hidden;');
5
- tx[i].addEventListener("input", OnInput, false);
6
- }
7
 
8
- function OnInput(e) {
9
- e.target.style.height = 'auto';
10
- e.target.style.height = (e.target.scrollHeight) + 'px';
11
- }
12
 
 
13
 
14
- function getCookie(name) {
15
- let cookieValue = null;
16
-
17
- if (document.cookie && document.cookie !== '') {
18
- const cookies = document.cookie.split(';');
19
- for (let i = 0; i < cookies.length; i++) {
20
- const cookie = cookies[i].trim();
21
-
22
- // Does this cookie string begin with the name we want?
23
- if (cookie.substring(0, name.length + 1) === (name + '=')) {
24
- cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
 
25
 
26
- break;
27
- }
28
  }
29
- }
30
-
31
- return cookieValue;
32
- }
33
- const csrftoken = getCookie('csrftoken');
34
-
35
-
36
-
37
- function validate(){
38
- if(question_text.value==''){
39
- question_text.style.borderBottom="2px solid red";
40
- question_text.focus();
41
-
42
- return false;
43
- }
44
- else{
45
- question_text.style.removeProperty("border-bottom");
46
- }
47
-
48
- choice_list=document.querySelectorAll(".choice");
49
- var i=0;
50
- for(i=0;i<choice_list.length;i++){
51
- choice_text=choice_list[i].querySelector(".choice_text");
52
 
53
- if(choice_text.value==""){
54
- choice_text.style.borderBottom="2px solid red";
55
- choice_text.focus();
56
- return false
57
- }
58
- else{
59
- choice_text.style.removeProperty("border-bottom");
60
- }
61
- }
62
 
63
- return true;
64
  }
 
1
+ function DeletePoll(elem){
2
+ if(!confirm("do you really wanna delete?"))
3
+ return ;
4
 
 
 
 
 
 
5
 
6
+ elem=elem.parentElement.parentElement.parentElement;
7
+ console.log(elem.dataset.id);
8
+ var data_to_send = {};
 
9
 
10
+ data_to_send["question_id"]=elem.dataset.id;
11
 
12
+ fetch(delete_url,{
13
+ method:"POST",
14
+ headers:{
15
+ 'X-CSRFToken': csrftoken
16
+ },
17
+ body:JSON.stringify(data_to_send)
18
+ }).then(res=>res.json()).then(res=>{
19
+ console.log(res);
20
+
21
+ if (res["message"]=="success"){
22
+
23
+ elem.remove();
24
 
 
 
25
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ });
 
 
 
 
 
 
 
 
28
 
 
29
  }
polls/templates/polls/detail.html CHANGED
@@ -1,50 +1,15 @@
1
-
2
- <html>
3
- <head>
4
- {% load static %}
5
- <link type="text/css" rel="stylesheet" href="{% static 'polls/detail.css' %}">
6
- </head>
7
- <body>
8
-
9
- <div class="question">
10
- <p class="question_text">{{ question.question_text }}</p>
11
- <div class="options">
12
- {% for choice in question.choice_set.all %}
13
- <div class="option" onclick="choose_option(this);" data-id="{{ choice.id }}">{{ choice.choice_text }}</div>
14
- {% endfor %}
15
- </div>
16
- <input id="vote_btn" onclick="vote();" type="button" value="Vote">
17
- </div>
18
-
19
- <script>
20
- var vote_url="{% url 'polls:vote' question.id %}";
21
- </script>
22
- <script src="{% static 'polls/detail.js' %}"></script>
23
- </body>
24
- </html>
25
-
26
- <!-- <form method="POST" action="{% url 'polls:vote' question.id %}">
27
- {% csrf_token %}
28
- <fieldset>
29
- <legend>
30
- <h1>{{ question.question_text }}</h1>
31
- {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
32
- {% for choice in question.choice_set.all %}
33
- <input type="radio" name="choice" value="{{ choice.id }}" >
34
- <label>{{ choice.choice_text }}</label><br>
35
- {% endfor %}
36
- </legend>
37
- </fieldset>
38
- <input type="submit" value="Vote">
39
- </form>
40
- -->
41
-
42
- <!--
43
- <div class="question">
44
- <p>Question</p>
45
- <div class="options">
46
- <div class="option">option A</div>
47
- <div class="option">option B</div>
48
- <div class="option">option C</div>
49
- </div>
50
- </div> -->
 
1
+ {% extends 'polls/base.html' %}
2
+
3
+ {% block head %}
4
+ {% load index %}
5
+ {% load static %}
6
+ <link type="text/css" rel="stylesheet" href="{% static 'polls/index.css' %}">
7
+ {% endblock %}
8
+
9
+ {% block body %}
10
+
11
+ <h2 id="sub_header">Details</h2>
12
+ {% include 'polls/question_list.html' %}
13
+
14
+ <script src="{% static 'polls/index.js' %}"></script>
15
+ {% endblock %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
polls/templates/polls/header.html CHANGED
@@ -1,5 +1,8 @@
1
  <header>
2
- <span id="app_name">Polling App</span>
 
 
 
3
 
4
  <div id="profile" onclick="toggle_profile_options(event)">
5
  <span id="first_alpha">{{ request.user }}</span>
 
1
  <header>
2
+ <a href="{% url 'polls:index' %}">
3
+ <span id="app_name">Polling App</span>
4
+ </a>
5
+ <!-- <span id="app_name">Polling App</span> -->
6
 
7
  <div id="profile" onclick="toggle_profile_options(event)">
8
  <span id="first_alpha">{{ request.user }}</span>
polls/templates/polls/index.html CHANGED
@@ -9,78 +9,7 @@
9
  {% block body %}
10
 
11
  <h2 id="sub_header">Public Polls</h2>
12
-
13
- {% if latest_questions_list %}
14
-
15
-
16
- <div id="main_container">
17
- {% for question in latest_questions_list %}
18
-
19
- <div class="question_container">
20
- <div class="question_header">
21
- <textarea oninput="OnInput(event)" class="question_text" readonly="true">{{ question.question_text }}</textarea>
22
- <div class="right_section">
23
- <div class="question_meta">
24
- <span>
25
- <span class="total_votes">{{ question.total_votes }}</span>
26
- Votes
27
- </span>
28
-
29
- <span>Results in
30
- <span>1 hour</span>
31
- </span>
32
- </div>
33
-
34
- <img src="{% static 'polls/arrow-up-right-from-square-solid.svg' %}">
35
-
36
- </div>
37
- </div>
38
- <div class="form_container">
39
- <div class="choices {{ question.has_voted|yesno:'result,' }}">
40
-
41
- {% for choice in question.choice_set.all %}
42
- <div class="choice" onclick="select_option(this)" data-id="{{ choice.pk }}"
43
- {% if question.has_voted %}
44
- data-percent="{{ choice.vote_percent }}"
45
- {% endif %}
46
- >
47
-
48
- <span>
49
- <span>{{ forloop.counter|add:"64"|stringformat:"c" }}</span>
50
- <span> -</span>
51
- </span>
52
- <textarea class="choice_text" oninput="OnInput(event)" readonly="true">{{ choice.choice_text }}</textarea>
53
-
54
-
55
- </div>
56
- {% endfor %}
57
- </div>
58
- <button class="vote_btn" onclick="Vote(this)">Vote</button>
59
-
60
- </div>
61
- </div>
62
- {% endfor %}
63
- </div>
64
-
65
-
66
- {% else %}
67
- <p>No questions yet</p>
68
- {% endif %}
69
- <!-- {% if latest_questions_list %}
70
-
71
- <div class="questions" >
72
- {% for question in latest_questions_list %}
73
-
74
- <div class="question" onclick="window.location.href = `{% url 'polls:detail' question.id %}`;">
75
- <p class="question_text">{{ question }}</p>
76
- <p class="total_count">Votes:{{ question.total_votes }}</p>
77
- </div>
78
- {% endfor %}
79
- </div>
80
-
81
-
82
- {% else %}
83
- <p>No questions yet</p>
84
- {% endif %} -->
85
  <script src="{% static 'polls/index.js' %}"></script>
86
  {% endblock %}
 
9
  {% block body %}
10
 
11
  <h2 id="sub_header">Public Polls</h2>
12
+ {% include 'polls/question_list.html' %}
13
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  <script src="{% static 'polls/index.js' %}"></script>
15
  {% endblock %}
polls/templates/polls/question_list.html ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% load static %}
2
+ {% if latest_questions_list %}
3
+
4
+
5
+ <div id="main_container">
6
+ {% for question in latest_questions_list %}
7
+
8
+ <div class="question_container" data-id="{{ question.pk }}">
9
+ <div class="question_header">
10
+ <textarea oninput="OnInput(event)" class="question_text" readonly="true">{{ question.question_text }}</textarea>
11
+ <div class="right_section">
12
+ <div class="question_meta">
13
+ <span>
14
+ <span class="total_votes">{{ question.total_votes }}</span>
15
+ Votes
16
+ </span>
17
+
18
+ <span>Results in
19
+ <span>1 hour</span>
20
+ </span>
21
+ </div>
22
+
23
+ <a href="{% url 'polls:detail' question.pk %}">
24
+ <img src="{% static 'polls/arrow-up-right-from-square-solid.svg' %}">
25
+ </a>
26
+
27
+ </div>
28
+ </div>
29
+ <div class="form_container">
30
+ <div class="choices {{ question.has_voted|yesno:'result,' }}">
31
+
32
+ {% for choice in question.choice_set.all %}
33
+ <div class="choice" onclick="select_option(this)" data-id="{{ choice.pk }}"
34
+ {% if question.has_voted %}
35
+ data-percent="{{ choice.vote_percent }}"
36
+ {% endif %}
37
+ >
38
+
39
+ <span>
40
+ <span>{{ forloop.counter|add:"64"|stringformat:"c" }}</span>
41
+ <span> -</span>
42
+ </span>
43
+ <textarea class="choice_text" oninput="OnInput(event)" readonly="true">{{ choice.choice_text }}</textarea>
44
+
45
+
46
+ </div>
47
+ {% endfor %}
48
+ </div>
49
+
50
+ <div>
51
+ {% if canDelete %}
52
+ <button class="delete_btn" onclick="DeletePoll(this)">Delete</button>
53
+ {% endif %}
54
+ <button class="vote_btn" onclick="Vote(this)">Vote</button>
55
+ </div>
56
+ </div>
57
+ </div>
58
+ {% endfor %}
59
+ </div>
60
+
61
+
62
+ {% else %}
63
+ <p>No questions yet</p>
64
+ {% endif %}
65
+
66
+ <script>
67
+ var vote_url="{% url 'polls:vote' %}";
68
+ </script>
polls/templates/polls/your_polls.html CHANGED
@@ -1,78 +1,20 @@
1
  {% extends 'polls/base.html' %}
2
 
3
  {% block head %}
 
4
  {% load static %}
5
  <link type="text/css" rel="stylesheet" href="{% static 'polls/index.css' %}">
 
6
  {% endblock %}
7
 
8
  {% block body %}
9
 
10
  <h2 id="sub_header">Your Polls</h2>
11
-
12
- {% if latest_questions_list %}
13
-
14
-
15
- <div id="main_container">
16
- {% for question in latest_questions_list %}
17
-
18
- <div class="question_container">
19
- <div class="question_header">
20
- <textarea oninput="OnInput(event)" class="question_text" readonly="true">{{ question.question_text }}</textarea>
21
- <div class="right_section">
22
- <div class="question_meta">
23
- <span>
24
- <span>{{ question.total_votes }}</span>
25
- Votes
26
- </span>
27
-
28
- <span>Results in
29
- <span>1 hour</span>
30
- </span>
31
- </div>
32
-
33
- <img src="{% static 'polls/arrow-up-right-from-square-solid.svg' %}">
34
-
35
- </div>
36
- </div>
37
- <div class="form_container">
38
- <div class="choices">
39
-
40
- {% for choice in question.choice_set.all %}
41
- <div class="choice">
42
- <span>
43
- <span>{{ forloop.counter|add:"64"|stringformat:"c" }}</span>
44
- <span> -</span>
45
- </span>
46
- <textarea class="choice_text" oninput="OnInput(event)" readonly="true">{{ choice.choice_text }}</textarea>
47
- </div>
48
- {% endfor %}
49
- </div>
50
- <button class="vote_btn">Vote</button>
51
-
52
- </div>
53
- </div>
54
- {% endfor %}
55
- </div>
56
-
57
-
58
- {% else %}
59
- <p>No questions yet</p>
60
- {% endif %}
61
- <!-- {% if latest_questions_list %}
62
-
63
- <div class="questions" >
64
- {% for question in latest_questions_list %}
65
-
66
- <div class="question" onclick="window.location.href = `{% url 'polls:detail' question.id %}`;">
67
- <p class="question_text">{{ question }}</p>
68
- <p class="total_count">Votes:{{ question.total_votes }}</p>
69
- </div>
70
- {% endfor %}
71
- </div>
72
-
73
-
74
- {% else %}
75
- <p>No questions yet</p>
76
- {% endif %} -->
77
  <script src="{% static 'polls/index.js' %}"></script>
 
78
  {% endblock %}
 
1
  {% extends 'polls/base.html' %}
2
 
3
  {% block head %}
4
+ {% load index %}
5
  {% load static %}
6
  <link type="text/css" rel="stylesheet" href="{% static 'polls/index.css' %}">
7
+ <link type="text/css" rel="stylesheet" href="{% static 'polls/your_polls.css' %}">
8
  {% endblock %}
9
 
10
  {% block body %}
11
 
12
  <h2 id="sub_header">Your Polls</h2>
13
+ {% include 'polls/question_list.html' %}
14
+
15
+ <script>
16
+ var delete_url="{% url 'polls:delete' %}";
17
+ </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  <script src="{% static 'polls/index.js' %}"></script>
19
+ <script src="{% static 'polls/your_polls.js' %}"></script>
20
  {% endblock %}
polls/templates/polls/your_votes.html ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'polls/base.html' %}
2
+
3
+ {% block head %}
4
+ {% load index %}
5
+ {% load static %}
6
+ <link type="text/css" rel="stylesheet" href="{% static 'polls/index.css' %}">
7
+ {% endblock %}
8
+
9
+ {% block body %}
10
+
11
+ <h2 id="sub_header">Your Votes</h2>
12
+
13
+ {% include 'polls/question_list.html' %}
14
+
15
+
16
+ <script src="{% static 'polls/index.js' %}"></script>
17
+ {% endblock %}
polls/urls.py CHANGED
@@ -5,15 +5,19 @@ from . import views
5
  app_name="polls" # for namespace
6
  urlpatterns = [
7
  path("",views.index,name="index"),
 
8
  path("signup/",views.signup,name="signup"),
9
  path("login/",views.login,name="login"),
10
- path("details/<int:question_id>/",views.detail,name="detail"),
11
  path("vote/",views.vote,name="vote"),
12
- path("result/<int:question_id>/",views.result,name="result"),
 
13
 
14
  path("create/",views.create,name="create"),
 
 
15
  path("your_polls/",views.your_polls,name="your_polls"),
16
- path("votes/",views.your_votes,name="your_votes"),
17
  path("settings/",views.settings,name="settings"),
18
  path("logout/",views.logout,name="logout"),
19
 
 
5
  app_name="polls" # for namespace
6
  urlpatterns = [
7
  path("",views.index,name="index"),
8
+
9
  path("signup/",views.signup,name="signup"),
10
  path("login/",views.login,name="login"),
11
+
12
  path("vote/",views.vote,name="vote"),
13
+ path("<int:question_id>/",views.detail,name="detail"),
14
+
15
 
16
  path("create/",views.create,name="create"),
17
+ path("delete/",views.delete_view,name="delete"),
18
+
19
  path("your_polls/",views.your_polls,name="your_polls"),
20
+ path("your_votes/",views.your_votes,name="your_votes"),
21
  path("settings/",views.settings,name="settings"),
22
  path("logout/",views.logout,name="logout"),
23
 
polls/views.py CHANGED
@@ -2,6 +2,7 @@ from django.shortcuts import render,get_object_or_404
2
  from django.http import HttpResponse,Http404,HttpResponseRedirect,JsonResponse
3
  from django.contrib.auth import login as auth_login,logout as auth_logout
4
  from django.contrib.auth import get_user_model
 
5
  from django.urls import reverse
6
  import json
7
  from .models import Question,Choice,Vote
@@ -11,6 +12,9 @@ from .forms import UserSignupForm,UserLoginForm
11
  User = get_user_model() # user model
12
 
13
  # Create your views here.
 
 
 
14
  def index(request):
15
  latest_questions_list = Question.objects.order_by("-pub_date")[:5]
16
 
@@ -26,14 +30,7 @@ def index(request):
26
 
27
  return render(request,"polls/index.html",context)
28
 
29
- def detail(request,question_id):
30
- try:
31
- question=Question.objects.get(pk=question_id)
32
- except Question.DoesNotExist:
33
- raise Http404("Question Does Not Exists")
34
-
35
- return render(request,"polls/detail.html",{"question":question})
36
-
37
  def vote(request):
38
  if request.method=='POST':
39
  json_data=json.loads(request.body)
@@ -67,10 +64,6 @@ def vote(request):
67
  })
68
 
69
 
70
- def result(request,question_id):
71
- question=get_object_or_404(Question,pk=question_id)
72
- return render(request,"polls/result.html",{"question":question})
73
-
74
  def signup(request):
75
  form=UserSignupForm(request.POST or None)
76
  context={}
@@ -136,7 +129,7 @@ def login(request):
136
 
137
  return render(request,"polls/login.html",context)
138
 
139
-
140
  def create(request):
141
  if request.method=="GET":
142
  return render(request,'polls/create.html',{})
@@ -155,16 +148,64 @@ def create(request):
155
 
156
  "redirect":reverse("polls:index")
157
  })
158
-
 
159
  def your_polls(request):
160
  latest_questions_list = Question.objects.filter(author=request.user).order_by("-pub_date")[:5]
161
- context={"latest_questions_list":latest_questions_list}
 
 
 
 
 
162
  return render(request,'polls/your_polls.html',context)
163
 
 
164
  def your_votes(request):
165
- pass
166
- def settings(request):
167
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  def logout(request):
169
  auth_logout(request)
170
- return HttpResponseRedirect(reverse('polls:login'))
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from django.http import HttpResponse,Http404,HttpResponseRedirect,JsonResponse
3
  from django.contrib.auth import login as auth_login,logout as auth_logout
4
  from django.contrib.auth import get_user_model
5
+ from django.contrib.auth.decorators import login_required
6
  from django.urls import reverse
7
  import json
8
  from .models import Question,Choice,Vote
 
12
  User = get_user_model() # user model
13
 
14
  # Create your views here.
15
+ login_url="/polls/login/"
16
+
17
+ @login_required(login_url=login_url)
18
  def index(request):
19
  latest_questions_list = Question.objects.order_by("-pub_date")[:5]
20
 
 
30
 
31
  return render(request,"polls/index.html",context)
32
 
33
+ @login_required(login_url=login_url)
 
 
 
 
 
 
 
34
  def vote(request):
35
  if request.method=='POST':
36
  json_data=json.loads(request.body)
 
64
  })
65
 
66
 
 
 
 
 
67
  def signup(request):
68
  form=UserSignupForm(request.POST or None)
69
  context={}
 
129
 
130
  return render(request,"polls/login.html",context)
131
 
132
+ @login_required(login_url=login_url)
133
  def create(request):
134
  if request.method=="GET":
135
  return render(request,'polls/create.html',{})
 
148
 
149
  "redirect":reverse("polls:index")
150
  })
151
+
152
+ @login_required(login_url=login_url)
153
  def your_polls(request):
154
  latest_questions_list = Question.objects.filter(author=request.user).order_by("-pub_date")[:5]
155
+
156
+ for q in latest_questions_list:
157
+ q.has_voted=q.has_user_voted(request.user)
158
+
159
+ context={"latest_questions_list":latest_questions_list,
160
+ "canDelete":True}
161
  return render(request,'polls/your_polls.html',context)
162
 
163
+ @login_required(login_url=login_url)
164
  def your_votes(request):
165
+ user_voted_questions = Vote.objects.filter(user=request.user)
166
+ user_voted_questions = Choice.objects.filter(vote_set__in=user_voted_questions)
167
+ user_voted_questions = Question.objects.filter(choice_set__in=user_voted_questions)
168
+ # user_voted_questions = Question.objects.all()
169
+
170
+ # print(user_voted_questions)
171
+ print(user_voted_questions[0].choice_set)
172
+
173
+ for q in user_voted_questions:
174
+ q.has_voted=True
175
+
176
+
177
+ context={"latest_questions_list":user_voted_questions}
178
+ return render(request,'polls/your_votes.html',context)
179
+
180
+ @login_required(login_url=login_url)
181
+ def detail(request,question_id):
182
+ try:
183
+ question=Question.objects.get(pk=question_id)
184
+ except Question.DoesNotExist:
185
+ raise Http404("Question Does Not Exists")
186
+
187
+ # question=get_object_or_404(Question,pk=question_id)
188
+
189
+ question.has_voted=True
190
+
191
+
192
+ return render(request,"polls/detail.html",{"latest_questions_list":[question]})
193
+
194
+
195
+
196
+
197
  def logout(request):
198
  auth_logout(request)
199
+ return HttpResponseRedirect(reverse('polls:login'))
200
+
201
+ @login_required(login_url=login_url)
202
+ def delete_view(request):
203
+ if request.method=="POST":
204
+ json_data=json.loads(request.body)
205
+ question=get_object_or_404(Question,pk=json_data['question_id'])
206
+ question.delete()
207
+ return JsonResponse({"message":"success"})
208
+
209
+
210
+ def settings(request):
211
+ raise Http404()