Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

# Register your models here. 

 

from django.contrib import admin, messages 

from django.db import transaction 

from django.db.models import Q 

 

from common.models import Fanfic, Fandom, FandomFanfic, Type, CharacterFanfic, \ 

Pairing, SubmittedReport 

from fanfics.forms.admin_forms import FanficForm 

from fanfics.widgets.widget import SelectCustomWidget 

 

 

class FandomListFilter(admin.SimpleListFilter): 

# Human-readable title which will be displayed in the 

# right admin sidebar just above the filter options. 

title = 'fandom' 

 

# Parameter for the filter that will be used in the URL query. 

parameter_name = 'fandom' 

 

def lookups(self, request, model_admin): 

""" 

Returns a list of tuples. The first element in each 

tuple is the coded value for the option that will 

appear in the URL query. The second element is the 

human-readable name for the option that will appear 

in the right sidebar. 

""" 

fandoms = Fandom.objects.values('id', 'name') 

 

list_tuples = tuple([(x['id'], x['name']) for x in fandoms]) 

return list_tuples 

 

def queryset(self, request, queryset): 

""" 

Returns the filtered queryset based on the value 

provided in the query string and retrievable via 

`self.value()`. 

""" 

# Compare the requested value (either '80s' or '90s') 

# to decide how to filter the queryset. 

fandom_id = self.value() 

if fandom_id: 

# the user filtered by fandom 

fanfics_ids_with_fandom = list(FandomFanfic.objects.filter( 

fandom__id=fandom_id).distinct().values_list('fanfic__id', 

flat=True)) 

 

queryset = queryset.filter(id__in=fanfics_ids_with_fandom) 

return queryset 

 

 

class TypeListFilter(admin.SimpleListFilter): 

# Human-readable title which will be displayed in the 

# right admin sidebar just above the filter options. 

title = 'media type' 

 

# Parameter for the filter that will be used in the URL query. 

parameter_name = 'media' 

 

def lookups(self, request, model_admin): 

""" 

Returns a list of tuples. The first element in each 

tuple is the coded value for the option that will 

appear in the URL query. The second element is the 

human-readable name for the option that will appear 

in the right sidebar. 

""" 

types = Type.objects.values('name') 

 

list_tuples = tuple([(x['name'], x['name']) for x in types]) 

return list_tuples 

 

def queryset(self, request, queryset): 

""" 

Returns the filtered queryset based on the value 

provided in the query string and retrievable via 

`self.value()`. 

""" 

# Compare the requested value (either '80s' or '90s') 

# to decide how to filter the queryset. 

type_name = self.value() 

if type_name: 

# the user filtered by type 

type_obj = Type.objects.filter(name=type_name) 

if type_obj.exists(): 

type_obj = type_obj.first() 

 

fanfics_ids_with_fandom = list(FandomFanfic.objects.filter( 

fandom__type=type_obj).distinct().values_list( 

'fanfic__id', flat=True)) 

 

queryset = queryset.filter(id__in=fanfics_ids_with_fandom) 

return queryset 

 

 

@admin.register(Fanfic) 

class FanficAdmin(admin.ModelAdmin): 

list_display = ('id', 'name', 

'language', 'complete', 'date_added', 'last_time_checked', 

'average_score', 'primary_fandom', 'secondary_fandom') 

list_display_links = ('name',) 

list_filter = ( 

'author', 'language', 'complete', 'rating', TypeListFilter, 

FandomListFilter) 

 

search_fields = ('name', 'author') 

 

form = FanficForm 

 

def primary_fandom(self, obj): 

if obj: 

return obj.get_primary_fandom() 

 

def secondary_fandom(self, obj): 

if obj: 

return obj.get_secondary_fandom() 

 

 

class FandomListFilterCharacterFanfic(admin.SimpleListFilter): 

# Human-readable title which will be displayed in the 

# right admin sidebar just above the filter options. 

title = 'fandom' 

 

# Parameter for the filter that will be used in the URL query. 

parameter_name = 'fandom' 

 

def lookups(self, request, model_admin): 

""" 

Returns a list of tuples. The first element in each 

tuple is the coded value for the option that will 

appear in the URL query. The second element is the 

human-readable name for the option that will appear 

in the right sidebar. 

""" 

fandoms = Fandom.objects.values('id', 'name') 

 

list_tuples = tuple([(x['id'], x['name']) for x in fandoms]) 

return list_tuples 

 

def queryset(self, request, queryset): 

""" 

Returns the filtered queryset based on the value 

provided in the query string and retrievable via 

`self.value()`. 

""" 

# Compare the requested value (either '80s' or '90s') 

# to decide how to filter the queryset. 

fandom_id = self.value() 

if fandom_id: 

# the user filtered by fandom 

fanfics_ids_with_fandom = list(FandomFanfic.objects.filter( 

fandom__id=fandom_id).distinct().values_list('fanfic__id', 

flat=True)) 

 

queryset = queryset.filter(fanfic__id__in=fanfics_ids_with_fandom) 

return queryset 

 

 

class TypeListFilterCharacterFanfic(admin.SimpleListFilter): 

# Human-readable title which will be displayed in the 

# right admin sidebar just above the filter options. 

title = 'media type' 

 

# Parameter for the filter that will be used in the URL query. 

parameter_name = 'media' 

 

def lookups(self, request, model_admin): 

""" 

Returns a list of tuples. The first element in each 

tuple is the coded value for the option that will 

appear in the URL query. The second element is the 

human-readable name for the option that will appear 

in the right sidebar. 

""" 

types = Type.objects.values('name') 

 

list_tuples = tuple([(x['name'], x['name']) for x in types]) 

return list_tuples 

 

def queryset(self, request, queryset): 

""" 

Returns the filtered queryset based on the value 

provided in the query string and retrievable via 

`self.value()`. 

""" 

# Compare the requested value (either '80s' or '90s') 

# to decide how to filter the queryset. 

type_name = self.value() 

if type_name: 

# the user filtered by type 

type_obj = Type.objects.filter(name=type_name) 

if type_obj.exists(): 

type_obj = type_obj.first() 

 

fanfics_ids_with_fandom = list(FandomFanfic.objects.filter( 

fandom__type=type_obj).distinct().values_list( 

'fanfic__id', flat=True)) 

 

queryset = queryset.filter( 

fanfic__id__in=fanfics_ids_with_fandom) 

return queryset 

 

 

@admin.register(CharacterFanfic) 

class CharacterFanficAdmin(admin.ModelAdmin): 

list_display = ('id', 'fanfic', 'character', 'pairing_id') 

list_display_links = ('fanfic',) 

 

list_filter = ( 

TypeListFilterCharacterFanfic, 

FandomListFilterCharacterFanfic) 

 

search_fields = ('character__name_surname', 'fanfic__name') 

 

fields = ('fanfic', 'character') 

 

actions = ['create_pairing', 'delete_pairing'] 

 

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

return False 

 

def pairing_id(self, obj): 

a = list(Pairing.objects.filter(fanfic=obj.fanfic).filter(Q( 

character_one=obj.character) | Q( 

character_two=obj.character)).values_list( 

'id', flat=True)) 

return a 

 

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

if db_field.name == "character": 

# change widget 

kwargs['widget'] = SelectCustomWidget( 

attrs={'class': 'custom-select character-select'}) 

elif db_field.name == "fanfic": 

# change widget 

kwargs['widget'] = SelectCustomWidget( 

attrs={'class': 'custom-select fanfic-select'}) 

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

 

def create_pairing(self, request, queryset): 

""" Create pairing between two characters for a fanfic """ 

try: 

with transaction.atomic(): 

if queryset.count() > 2: 

self.message_user(request, 

"A pairing can only have two " 

"characters at the same time.", 

level=messages.ERROR) 

else: 

first = queryset.first() 

second = queryset.last() 

if first.fanfic.id != second.fanfic.id: 

# error, different fanfics 

self.message_user(request, 

"The selected characters are from " 

"different fanfics.", 

level=messages.ERROR) 

else: 

p = Pairing.objects.filter( 

fanfic=first.fanfic).filter(Q( 

character_one=first.character, 

character_two=second.character) | Q( 

character_one=second.character, 

character_two=first.character)) 

if p.exists(): 

# error, the pairing already exists 

self.message_user(request, 

"This pairing already exists.", 

level=messages.ERROR) 

else: 

Pairing.objects.create( 

fanfic=first.fanfic, 

character_one=first.character, 

character_two=second.character) 

# success 

self.message_user( 

request, 

"The pairing has been created.", 

level=messages.SUCCESS) 

except Exception: 

self.message_user(request, 

"There was an unexpected error.", 

level=messages.ERROR) 

 

create_pairing.short_description = "Create a pairing between the " \ 

"characters" 

 

def delete_pairing(self, request, queryset): 

""" Delete the pairing between two characters for a fanfic """ 

try: 

with transaction.atomic(): 

if queryset.count() > 2: 

self.message_user(request, 

"A pairing can only have two " 

"characters at the same time.", 

level=messages.ERROR) 

else: 

first = queryset.first() 

second = queryset.last() 

if first.fanfic.id != second.fanfic.id: 

# error, different fanfics 

self.message_user(request, 

"The selected characters are from " 

"different fanfics.", 

level=messages.ERROR) 

else: 

p = Pairing.objects.filter( 

fanfic=first.fanfic).filter(Q( 

character_one=first.character, 

character_two=second.character) | Q( 

character_one=second.character, 

character_two=first.character)) 

if p.exists(): 

# the pairing already exists 

p.delete() 

# success 

self.message_user( 

request, 

"The pairing has been deleted.", 

level=messages.SUCCESS) 

else: 

# the pairing does not exist 

self.message_user(request, 

"This pairing does not " 

"exists.", 

level=messages.ERROR) 

except Exception: 

self.message_user(request, 

"There was an unexpected error.", 

level=messages.ERROR) 

 

delete_pairing.short_description = "Delete the pairing" 

 

 

@admin.register(SubmittedReport) 

class SubmittedReportAdmin(admin.ModelAdmin): 

list_display = ('id', 'fanfic', 'date', 'issue', 'comment') 

list_display_links = ('fanfic',) 

 

search_fields = ('fanfic__name',) 

 

def has_add_permission(self, request, obj=None): 

return False 

 

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

return False