-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtutorial.py
More file actions
83 lines (65 loc) Β· 2.74 KB
/
Copy pathtutorial.py
File metadata and controls
83 lines (65 loc) Β· 2.74 KB
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
import graphene
from django.utils.translation import ugettext_lazy as _
from graphene_django import DjangoObjectType
from graphql_extensions.auth.decorators import login_required
from graphql_extensions.exceptions import GraphQLError
from api.models.program import Tutorial
from api.schemas.common import LanguageNode, PlaceNode, SeoulDateTime, UserEmailNode, has_owner_permission
from api.schemas.user import UserNode
from ticket.models import Ticket
class TutorialNode(DjangoObjectType):
class Meta:
model = Tutorial
description = """
Sprint
"""
language = graphene.Field(LanguageNode)
place = graphene.Field(PlaceNode)
owner = graphene.Field(UserNode)
started_at = graphene.Field(SeoulDateTime)
finished_at = graphene.Field(SeoulDateTime)
participants = graphene.List(UserEmailNode)
def resolve_participants(self, info):
user = info.context.user
if not has_owner_permission(user, self.owner):
return None
if not self.ticket_product:
return None
tickets = self.ticket_product.ticket_set.filter(status=Ticket.STATUS_PAID)
return [t.owner.profile for t in tickets]
class TutorialInput(graphene.InputObjectType):
desc_ko = graphene.String()
desc_en = graphene.String()
class UpdateTutorial(graphene.Mutation):
tutorial = graphene.Field(TutorialNode)
class Arguments:
id = graphene.Int()
data = TutorialInput(required=True)
@login_required
def mutate(self, info, id, data):
user = info.context.user
try:
if id:
tutorial = Tutorial.objects.get(pk=id, owner=user, accepted=True)
else:
tutorial = Tutorial.objects.last(owner=user, accepted=True)
for k, v in data.items():
setattr(tutorial, k, v)
tutorial.save()
return UpdateTutorial(tutorial=tutorial)
except Tutorial.DoesNotExist:
raise GraphQLError(_('νν λ¦¬μΌ μ§νμκ° μλλλ€. '
'λ¬Έμ κ° μμ κ²½μ° νμ΄μ½ νκ΅ μ€λΉμμνμκ² λ¬Έμ λΆνλ립λλ€.'))
class Mutations(graphene.ObjectType):
update_tutorial = UpdateTutorial.Field()
class Query(graphene.ObjectType):
tutorials = graphene.List(TutorialNode)
tutorial = graphene.Field(TutorialNode, id=graphene.Int())
my_tutorials = graphene.List(TutorialNode)
def resolve_tutorials(self, info):
return Tutorial.objects.filter(visible=True, accepted=True)
def resolve_tutorial(self, info, id):
return Tutorial.objects.get(pk=id, accepted=True)
@login_required
def resolve_my_tutorials(self, info):
return Tutorial.objects.filter(owner=info.context.user)