1+ from braces .forms import UserKwargModelFormMixin
12from crispy_forms .layout import Button , Div , Field , Fieldset , Reset , Submit
23from django import forms
4+ from django .urls import reverse
35from django .utils .translation import ugettext_lazy as _
46
57from toolhub_auth .models import User
6- from tools .models import ClearancePermission , UserTool
8+ from tools .models import ClearancePermission , ToolPhoto , UserTool
79from utils .forms import CrispyFormMixin
810
911
@@ -19,7 +21,9 @@ def layout_args(self, helper):
1921 Fieldset (
2022 None ,
2123 Field ("title" ),
22- Field ("description" , css_class = "h-100" , label_class = "" , field_class = "" ),
24+ # attempts
25+ # css_class="h-100", label_class="", field_class="", help_text="test"
26+ Field ("description" ),
2327 Field ("taxonomies" ),
2428 Field ("visibility" ),
2529 Field ("clearance" ),
@@ -34,6 +38,24 @@ class Meta:
3438 fields = ("title" , "description" , "taxonomies" , "visibility" , "clearance" )
3539 model = UserTool
3640
41+ def post_super_init (self ):
42+ super ().post_super_init ()
43+ self .update_markdown_upload_path ()
44+
45+ def update_markdown_upload_path (self ):
46+ if hasattr (self , "instance" ) and self .pk_field :
47+ reverse_kwargs = {self .pk_field : self .instance .pk }
48+ else :
49+ reverse_kwargs = {}
50+ attrs = self .fields ["description" ].widget .attrs
51+ attrs .update (
52+ {
53+ "data-markdownx-upload-urls-path" : reverse (
54+ "tools:upload_tool_photo" , kwargs = reverse_kwargs
55+ )
56+ }
57+ )
58+
3759
3860class UserToolUpdateForm (UserToolCreateForm ):
3961 action_button_label = _ ("Update tool" )
@@ -116,3 +138,27 @@ def save(self):
116138 )
117139 removed_users = init_users - edited_users
118140 self .instance .permissions .filter (cleared_user_id__in = removed_users ).delete ()
141+
142+
143+ class UploadToolPhotoForm (UserKwargModelFormMixin , forms .ModelForm ):
144+ """
145+ Optionally takes a tool to make association
146+ Also set the `uploading_user`?
147+ """
148+
149+ class Meta :
150+ model = ToolPhoto
151+ fields = ("file" ,)
152+
153+ def __init__ (self , * args , ** kwargs ):
154+ # move file uploaded to what the form expects
155+ kwargs ["files" ]["file" ] = kwargs ["files" ]["image" ]
156+ self .tool = kwargs .pop ("tool" , None )
157+ return super ().__init__ (* args , ** kwargs )
158+
159+ def save (self , commit = True ):
160+ # This will set the photo tool to null if none was passed
161+ self .instance .tool = self .tool
162+ self .instance .uploading_user = self .user
163+ # NOTE: get title from file name temporarily
164+ return super ().save (commit = commit )
0 commit comments