Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 8fdc9a7

Browse files
gallery: Add albums, photos APIs
1 parent 5c32964 commit 8fdc9a7

File tree

4 files changed

+125
-36
lines changed

4 files changed

+125
-36
lines changed

framework/schema.py

+32-30
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1+
import secrets
2+
import string
3+
from datetime import datetime, timedelta
4+
15
import graphene
26
import graphql_jwt
3-
from datetime import date, datetime, timedelta
4-
7+
from django.contrib.auth import get_user_model
58
from django.contrib.auth.hashers import check_password
6-
from django.utils import timezone
7-
from graphql_jwt.decorators import permission_required, login_required
89
from django.contrib.auth.models import User
10+
from django.core.mail import send_mail
911
from django.db.models import Avg
10-
from django.contrib.auth import get_user_model
12+
from django.template.loader import render_to_string
13+
from django.utils import timezone
14+
from django.utils.html import strip_tags
1115
from graphene_django import DjangoObjectType
16+
from graphql_jwt.decorators import login_required
1217

13-
import attendance.schema
1418
import activity.schema
15-
from dairy.schema import Query as dairyQuery
16-
from registration.schema import Mutation as registrationMutation, Query as registrationQuery
17-
import tasks.schema
18-
import status.schema
19+
import attendance.schema
1920
import password.schema
20-
from .api.APIException import APIException
21+
import status.schema
22+
import tasks.schema
23+
import gallery.schema
24+
from attendance.api.log import userAttendanceObj
25+
from attendance.models import Log
2126
from dairy.schema import Mutation as eventMutation
22-
23-
from members.schema import Query as MembersQuery, Mutation as membersMutation
24-
from members.api.profile import ProfileObj
27+
from dairy.schema import Query as dairyQuery
28+
from framework import settings
2529
from members.api.group import GroupObj
30+
from members.api.profile import ProfileObj
2631
from members.models import Profile, Group
27-
28-
from attendance.models import Log
29-
from attendance.api.log import userAttendanceObj
30-
31-
from .api.user import UserBasicObj
32+
from members.schema import Query as MembersQuery, Mutation as membersMutation
33+
from registration.schema import Mutation as registrationMutation, Query as registrationQuery
34+
from .api.APIException import APIException
3235
from .api.mutation import Mutation as PlatformMutation
33-
34-
import secrets,string
35-
36-
from framework import settings
37-
from django.core.mail import send_mail
38-
from django.template.loader import render_to_string
39-
from django.utils.html import strip_tags
36+
from .api.user import UserBasicObj
4037

4138
from_email = settings.EMAIL_HOST_USER
4239

@@ -210,6 +207,7 @@ def mutate(self, info, username=None, firstName=None, lastName=None, email=None,
210207
profile.save()
211208
return userResponseObj(id=user.id)
212209

210+
213211
class ResetPassword(graphene.Mutation):
214212
class Arguments:
215213
email = graphene.String(required=True)
@@ -223,16 +221,18 @@ def mutate(self, info, email):
223221
user.set_password(newPassword)
224222
user.save()
225223
context = {
226-
"password" : newPassword,
227-
"username" : user.username
224+
"password": newPassword,
225+
"username": user.username
228226
}
229227
message = render_to_string('email/password_reset_email.html', context)
230-
send_mail('Reset Password | amFOSS CMS', strip_tags(message) , from_email, [email], fail_silently=False, html_message=message)
228+
send_mail('Reset Password | amFOSS CMS', strip_tags(message), from_email, [email], fail_silently=False,
229+
html_message=message)
231230
return userStatusObj(status=True)
232231
else:
233232
raise APIException('Email is not registered',
234233
code='WRONG_EMAIL')
235234

235+
236236
class Query(
237237
dairyQuery,
238238
MembersQuery,
@@ -242,6 +242,7 @@ class Query(
242242
tasks.schema.Query,
243243
activity.schema.Query,
244244
status.schema.Query,
245+
gallery.schema.Query,
245246
graphene.ObjectType
246247
):
247248
user = graphene.Field(UserObj, username=graphene.String(required=True))
@@ -294,7 +295,8 @@ def resolve_inActiveUsers(self, info, **kwargs):
294295
code='ONLY_SUPERUSER_HAS_ACCESS')
295296

296297

297-
class Mutation(membersMutation, attendance.schema.Mutation, registrationMutation, eventMutation, PlatformMutation, graphene.ObjectType):
298+
class Mutation(membersMutation, attendance.schema.Mutation, registrationMutation, eventMutation, PlatformMutation,
299+
graphene.ObjectType):
298300
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
299301
verify_token = graphql_jwt.Verify.Field()
300302
refresh_token = graphql_jwt.Refresh.Field()

gallery/admin.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
from django.contrib import admin
22
from .models import *
33
from easy_select2 import select2_modelform
4+
from import_export.admin import ImportExportModelAdmin, ExportActionMixin
45

56

67
@admin.register(Photo)
7-
class PhotoAdmin(admin.ModelAdmin):
8+
class PhotoAdmin(ImportExportModelAdmin, ExportActionMixin, admin.ModelAdmin):
89
fields = [
910
('uploader', 'date'),
1011
'image',
1112
'caption'
1213
]
13-
list_display = ('image', 'uploader', 'date')
14+
list_display = ('caption', 'uploader', 'date')
1415
list_filter = ['date']
1516
search_fields = ['uploader']
1617
select2 = select2_modelform(Photo, attrs={'width': '250px'})
1718
form = select2
1819

1920

2021
@admin.register(Album)
21-
class AlbumAdmin(admin.ModelAdmin):
22+
class AlbumAdmin(ImportExportModelAdmin, ExportActionMixin, admin.ModelAdmin):
2223
fields = [
2324
'title',
2425
('uploader', 'date'),
@@ -27,6 +28,6 @@ class AlbumAdmin(admin.ModelAdmin):
2728
]
2829
list_display = ('title', 'uploader', 'date')
2930
list_filter = ('date',)
30-
search_fields = ['uploader',]
31+
search_fields = ['uploader']
3132
select2 = select2_modelform(Album, attrs={'width': '250px'})
3233
form = select2

gallery/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def get_gallery_path(self, filename):
1414

1515
uploader = models.ForeignKey(User, on_delete=models.CASCADE)
1616
date = models.DateField(default=date.today)
17-
image = ProcessedImageField(default='', verbose_name='Image', upload_to=get_gallery_path,validators=[validate_file_size], **processed_image_field_specs)
17+
image = ProcessedImageField(default='', verbose_name='Image', upload_to=get_gallery_path, validators=[validate_file_size], **processed_image_field_specs)
1818
caption = models.CharField(max_length=200, null=True, blank=True)
1919

2020
def __str__(self):
21-
return str(self.id) + str(self.uploader.username) + str(self.date)
21+
return self.caption
2222

2323
class Meta:
2424
verbose_name_plural = "Photos"

gallery/schema.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import graphene
2+
from gallery.models import *
3+
from framework.api.user import UserBasicObj
4+
from django.db.models import F
5+
from framework.api.APIException import APIException
6+
7+
8+
class PhotoObj(graphene.ObjectType):
9+
caption = graphene.String()
10+
image = graphene.String()
11+
uploader = graphene.Field(UserBasicObj)
12+
date = graphene.Date()
13+
14+
def resolve_caption(self, info):
15+
return self['caption']
16+
17+
def resolve_image(self, info):
18+
return self['image']
19+
20+
def resolve_uploader(self, info):
21+
return User.objects.values().get(id=self['uploader_id'])
22+
23+
def resolve_date(self, info):
24+
return self['date']
25+
26+
27+
class AlbumObj(graphene.ObjectType):
28+
title = graphene.String()
29+
uploader = graphene.Field(UserBasicObj)
30+
date = graphene.Date()
31+
description = graphene.String()
32+
photos = graphene.List(PhotoObj)
33+
34+
def resolve_title(self, info):
35+
return self['title']
36+
37+
def resolve_uploader(self, info):
38+
return User.objects.values().get(id=self['uploader_id'])
39+
40+
def resolve_date(self, info):
41+
return self['date']
42+
43+
def resolve_description(self, info):
44+
return self['description']
45+
46+
@graphene.resolve_only_args
47+
def resolve_photos(self):
48+
return Album.objects.values().annotate(
49+
caption=F('photos__caption'),
50+
image=F('photos__image'),
51+
date=F('photos__date'),
52+
uploader=F('photos__uploader')
53+
).filter(id=self['id'])
54+
55+
56+
class Query(graphene.ObjectType):
57+
photos = graphene.List(PhotoObj)
58+
photo = graphene.Field(PhotoObj, caption=graphene.String())
59+
albums = graphene.List(AlbumObj)
60+
album = graphene.Field(AlbumObj, title=graphene.String())
61+
62+
def resolve_photos(self, info):
63+
return reversed(Photo.objects.values().all().order_by('date'))
64+
65+
def resolve_photo(self, info, **kwargs):
66+
caption = kwargs.get('caption')
67+
if caption is not None:
68+
return Photo.objects.values().get(caption=caption)
69+
else:
70+
raise APIException('Caption is required',
71+
code='CAPTION_IS_REQUIRED')
72+
73+
def resolve_albums(self, info):
74+
return Album.objects.values().all().order_by('date')
75+
76+
def resolve_album(self, info, **kwargs):
77+
title = kwargs.get('title')
78+
if title is not None:
79+
return Album.objects.values().get(title=title)
80+
else:
81+
raise APIException('Title is required',
82+
code='TITLE_IS_REQUIRED')
83+
84+
85+
schema = graphene.Schema(query=Query)
86+

0 commit comments

Comments
 (0)