Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
red_brick_board
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ciella Francisco
red_brick_board
Commits
90a82479
Commit
90a82479
authored
Apr 13, 2024
by
Ciella Francisco
😵
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modified views.py to enable integration of formset for multiple image upload
parent
ce0cf129
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
10 deletions
+82
-10
views.py
redbrickboard/event_management/views.py
+82
-10
No files found.
redbrickboard/event_management/views.py
View file @
90a82479
...
...
@@ -5,11 +5,53 @@ from django.views.generic.detail import DetailView
from
django.views.generic.list
import
ListView
from
django.views.generic.edit
import
CreateView
,
UpdateView
from
.forms
import
EventForm
from
.models
import
Event
from
.forms
import
(
EventForm
,
PromoFormSet
)
from
.models
import
Event
,
Promo
def
delete_image
(
request
,
pk
):
try
:
image
=
Promo
.
objects
.
get
(
id
=
pk
)
except
Promo
.
DoesNotExist
:
messages
.
success
(
request
,
'Object Does not exist'
)
return
redirect
(
'event_management:event-update'
,
pk
=
image
.
event
.
id
)
image
.
delete
()
messages
.
success
(
request
,
'Image deleted successfully'
)
return
redirect
(
'event_management:event-update'
,
pk
=
image
.
event
.
id
)
class
PromoInline
():
form_class
=
EventForm
template_name
=
'event_management/event-form.html'
def
form_valid
(
self
,
form
):
named_formsets
=
self
.
get_named_formsets
()
if
not
all
((
x
.
is_valid
()
for
x
in
named_formsets
.
values
())):
return
self
.
render_to_response
(
self
.
get_context_data
(
form
=
form
))
self
.
object
=
form
.
save
()
for
name
,
formset
in
named_formsets
.
items
():
formset_save_func
=
getattr
(
self
,
'formset_{0}_valid'
.
format
(
name
),
None
)
if
formset_save_func
is
not
None
:
formset_save_func
(
formset
)
else
:
formset
.
save
()
return
redirect
(
'event_management:event-list'
)
def
formset_images_valid
(
self
,
formset
):
images
=
formset
.
save
(
commit
=
False
)
for
obj
in
formset
.
deleted_objects
:
obj
.
delete
()
for
image
in
images
:
image
.
event
=
self
.
object
image
.
save
()
class
EventDetailView
(
DetailView
):
model
=
Event
fields
=
'__all__'
template_name
=
'event_management/event-details.html'
class
EventListView
(
ListView
):
...
...
@@ -17,13 +59,43 @@ class EventListView(ListView):
fields
=
'__all__'
template_name
=
'event_management/event-list.html'
class
EventCreateView
(
CreateView
):
model
=
Event
form_class
=
EventForm
success_url
=
'/events/'
template_name
=
'event_management/event-form.html'
#
class EventCreateView(CreateView):
#
model = Event
#
form_class = EventForm
#
success_url = '/events/'
#
template_name = 'event_management/event-form.html'
class
EventUpdateView
(
UpdateView
):
# class EventUpdateView(UpdateView):
# model = Event
# fields = '__all__'
# # success_url =
# template_name = 'event_management/event-form.html'
class
EventCreateView
(
PromoInline
,
CreateView
):
model
=
Event
fields
=
'__all__'
template_name
=
'event_management/event-form.html'
\ No newline at end of file
def
get_context_data
(
self
,
**
kwargs
):
ctx
=
super
(
EventCreateView
,
self
)
.
get_context_data
(
**
kwargs
)
ctx
[
'named_formsets'
]
=
self
.
get_named_formsets
()
return
ctx
def
get_named_formsets
(
self
):
if
self
.
request
.
method
==
"GET"
:
return
{
'images'
:
PromoFormSet
(
prefix
=
'images'
)
}
else
:
return
{
'images'
:
PromoFormSet
(
self
.
request
.
POST
or
None
,
self
.
request
.
FILES
or
None
,
prefix
=
'images'
)
}
class
EventUpdateView
(
PromoInline
,
UpdateView
):
model
=
Event
def
get_context_data
(
self
,
**
kwargs
):
ctx
=
super
(
EventUpdateView
,
self
)
.
get_context_data
(
**
kwargs
)
ctx
[
'named_formsets'
]
=
self
.
get_named_formsets
()
return
ctx
def
get_named_formsets
(
self
):
return
{
'images'
:
PromoFormSet
(
self
.
request
.
POST
or
None
,
self
.
request
.
FILES
or
None
,
instance
=
self
.
object
,
prefix
=
'images'
),
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment