Coverage for src/backend/InvenTree/common/admin.py: 74%

102 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Admin for the common app.""" 

2 

3from django.contrib import admin 

4 

5import common.models 

6import common.validators 

7 

8 

9@admin.register(common.models.ParameterTemplate) 

10class ParameterTemplateAdmin(admin.ModelAdmin): 

11 """Admin interface for ParameterTemplate objects.""" 

12 

13 list_display = ('name', 'description', 'model_type', 'units') 

14 search_fields = ('name', 'description') 

15 

16 

17@admin.register(common.models.Parameter) 

18class ParameterAdmin(admin.ModelAdmin): 

19 """Admin interface for Parameter objects.""" 

20 

21 list_display = ( 

22 'template', 

23 'model_type', 

24 'model_id', 

25 'data', 

26 'updated', 

27 'updated_by', 

28 ) 

29 

30 autocomplete_fields = ('template', 'updated_by') 

31 list_filter = ('template', 'model_type', 'updated_by') 

32 search_fields = ('template__name', 'data', 'note') 

33 

34 

35class SelectionListEntryInlineAdmin(admin.StackedInline): 

36 """Inline admin class for the SelectionListEntry model.""" 

37 

38 model = common.models.SelectionListEntry 

39 extra = 0 

40 

41 

42@admin.register(common.models.SelectionList) 

43class SelectionListAdmin(admin.ModelAdmin): 

44 """Admin interface for SelectionList objects.""" 

45 

46 list_display = ('name', 'description', 'active', 'locked') 

47 search_fields = ('name', 'description') 

48 list_filter = ('active', 'locked') 

49 

50 inlines = [SelectionListEntryInlineAdmin] 

51 

52 

53@admin.register(common.models.Attachment) 

54class AttachmentAdmin(admin.ModelAdmin): 

55 """Admin interface for Attachment objects.""" 

56 

57 def formfield_for_dbfield(self, db_field, request, **kwargs): 

58 """Provide custom choices for 'model_type' field.""" 

59 if db_field.name == 'model_type': 

60 db_field.choices = common.validators.attachment_model_options() 

61 

62 return super().formfield_for_dbfield(db_field, request, **kwargs) 

63 

64 list_display = ( 

65 'model_type', 

66 'model_id', 

67 'attachment', 

68 'link', 

69 'upload_user', 

70 'upload_date', 

71 ) 

72 

73 list_filter = ['model_type', 'upload_user'] 

74 

75 readonly_fields = ['file_size', 'upload_date', 'upload_user'] 

76 

77 search_fields = ('content_type', 'comment') 

78 

79 

80@admin.register(common.models.DataOutput) 

81class DataOutputAdmin(admin.ModelAdmin): 

82 """Admin interface for DataOutput objects - view and delete only.""" 

83 

84 list_display = ('user', 'created', 'output_type', 'output') 

85 

86 list_filter = ('user', 'output_type') 

87 

88 def has_add_permission(self, request): 

89 """Prevent addition of new DataOutput objects via the admin interface.""" 

90 return False 

91 

92 def has_change_permission(self, request, obj=None): 

93 """Prevent modification of DataOutput objects via the admin interface.""" 

94 return False 

95 

96 

97@admin.register(common.models.BarcodeScanResult) 

98class BarcodeScanResultAdmin(admin.ModelAdmin): 

99 """Admin interface for BarcodeScanResult objects - read-only audit log.""" 

100 

101 list_display = ('data', 'timestamp', 'user', 'endpoint', 'result') 

102 

103 list_filter = ('user', 'endpoint', 'result') 

104 

105 def has_add_permission(self, request): 

106 """Prevent addition of new BarcodeScanResult objects via the admin interface.""" 

107 return False 

108 

109 def has_change_permission(self, request, obj=None): 

110 """Prevent modification of BarcodeScanResult objects via the admin interface.""" 

111 return False 

112 

113 

114@admin.register(common.models.ProjectCode) 

115class ProjectCodeAdmin(admin.ModelAdmin): 

116 """Admin settings for ProjectCode.""" 

117 

118 list_display = ('code', 'description') 

119 

120 search_fields = ('code', 'description') 

121 

122 

123@admin.register(common.models.InvenTreeSetting) 

124class SettingsAdmin(admin.ModelAdmin): 

125 """Admin settings for InvenTreeSetting.""" 

126 

127 list_display = ('key', 'value') 

128 

129 def get_readonly_fields(self, request, obj=None): # pragma: no cover 

130 """Prevent the 'key' field being edited once the setting is created.""" 

131 if obj: 

132 return ['key'] 

133 return [] 

134 

135 

136@admin.register(common.models.InvenTreeUserSetting) 

137class UserSettingsAdmin(admin.ModelAdmin): 

138 """Admin settings for InvenTreeUserSetting.""" 

139 

140 list_display = ('key', 'value', 'user') 

141 

142 def get_readonly_fields(self, request, obj=None): # pragma: no cover 

143 """Prevent the 'key' field being edited once the setting is created.""" 

144 if obj: 

145 return ['key'] 

146 return [] 

147 

148 

149@admin.register(common.models.WebhookEndpoint) 

150class WebhookAdmin(admin.ModelAdmin): 

151 """Admin settings for Webhook.""" 

152 

153 list_display = ('endpoint_id', 'name', 'active', 'user') 

154 

155 

156@admin.register(common.models.NotificationEntry) 

157class NotificationEntryAdmin(admin.ModelAdmin): 

158 """Admin settings for NotificationEntry - view and delete only.""" 

159 

160 list_display = ('key', 'uid', 'updated') 

161 

162 def has_add_permission(self, request): 

163 """Prevent addition of new NotificationEntry objects via the admin interface.""" 

164 return False 

165 

166 def has_change_permission(self, request, obj=None): 

167 """Prevent modification of NotificationEntry objects via the admin interface.""" 

168 return False 

169 

170 

171@admin.register(common.models.NotificationMessage) 

172class NotificationMessageAdmin(admin.ModelAdmin): 

173 """Admin settings for NotificationMessage - view and delete only.""" 

174 

175 list_display = ( 

176 'age_human', 

177 'user', 

178 'category', 

179 'name', 

180 'read', 

181 'target_object', 

182 'source_object', 

183 ) 

184 

185 list_filter = ('category', 'read', 'user') 

186 

187 search_fields = ('name', 'category', 'message') 

188 

189 def has_add_permission(self, request): 

190 """Prevent addition of new NotificationMessage objects via the admin interface.""" 

191 return False 

192 

193 def has_change_permission(self, request, obj=None): 

194 """Prevent modification of NotificationMessage objects via the admin interface.""" 

195 return False 

196 

197 

198@admin.register(common.models.NewsFeedEntry) 

199class NewsFeedEntryAdmin(admin.ModelAdmin): 

200 """Admin settings for NewsFeedEntry - view and delete only.""" 

201 

202 list_display = ('title', 'author', 'published', 'summary') 

203 

204 def has_add_permission(self, request): 

205 """Prevent addition of new NewsFeedEntry objects via the admin interface.""" 

206 return False 

207 

208 def has_change_permission(self, request, obj=None): 

209 """Prevent modification of NewsFeedEntry objects via the admin interface.""" 

210 return False 

211 

212 

213class ReadOnlyAdmin(admin.ModelAdmin): 

214 """Base admin class that prevents all modifications.""" 

215 

216 def has_add_permission(self, request): 

217 """Prevent addition of new objects via the admin interface.""" 

218 return False 

219 

220 def has_change_permission(self, request, obj=None): 

221 """Prevent modification of objects via the admin interface.""" 

222 return False 

223 

224 def has_delete_permission(self, request, obj=None): 

225 """Prevent deletion of objects via the admin interface.""" 

226 return False 

227 

228 

229admin.site.register(common.models.WebhookMessage, ReadOnlyAdmin) 

230admin.site.register(common.models.EmailMessage, ReadOnlyAdmin) 

231admin.site.register(common.models.EmailThread, ReadOnlyAdmin)