IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

outcome.py 3.08 KB
Newer Older
1
from mystic.request import UncheckedPID
2
3
4
from mystic.database import compute_alphaid

from enum import Enum, unique
5
from typing import Any, Collection, Mapping, NamedTuple, NewType, Tuple, TypeAlias, Union
6
7
8

@unique
class SignupFieldKind(Enum):
Emi Simpson's avatar
Emi Simpson committed
9
10
11
	"""
	Designates a field kind used in the signup form
	"""
12
13
14
15
16
17
	Username = "username"
	Password = "password"
	Name = "name"
	Email = "email"

class SignupField(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
18
19
20
	"""
	Designates a specific field in the signup form
	"""
21
22
23
24
25
26
	field: SignupFieldKind
	def __str__(self):
		return f"signup-{self.field.value}"

@unique
class LoginFieldKind(Enum):
Emi Simpson's avatar
Emi Simpson committed
27
28
29
	"""
	Designates a field kind used in the login form
	"""
30
31
32
33
	Username = "username"
	Password = "password"

class LoginField(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
34
35
36
	"""
	Designates a specific field in the login form
	"""
37
38
39
40
41
42
	field: LoginFieldKind
	def __str__(self):
		return f"login-{self.field.value}"

@unique
class ProjectFieldKind(Enum):
Emi Simpson's avatar
Emi Simpson committed
43
44
45
	"""
	Designates a field kind used in the project form
	"""
46
47
48
49
50
51
52
53
	AddSource = "add_source"
	AddOwner = "add_owner"
	ProjectName = "project-name"
	Slug = "slug"
	Description = "description"
	Generic = "generic"

class ProjectField(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
54
55
56
	"""
	Designates a specific field a the project form
	"""
57
	pid: UncheckedPID
58
59
60
61
62
	field: ProjectFieldKind
	def __str__(self):
		return f"{compute_alphaid(self.pid)}-{self.field.value}"

class GenericAlert(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
63
64
65
	"""
	Used to indicate that an alert is generic, and does not apply to a specific form
	"""
66
67
68
	pass

Field: TypeAlias = Union[GenericAlert, ProjectField, LoginField, SignupField]
Emi Simpson's avatar
Emi Simpson committed
69
70
71
"""
A generalization over the types of fields
"""
Emi Simpson's avatar
Emi Simpson committed
72

73
Backend = NewType('Backend', str)
Emi Simpson's avatar
Emi Simpson committed
74
75
76
"""
A `str`-like field exclusively for backends
"""
77
Url = NewType('Url', str)
Emi Simpson's avatar
Emi Simpson committed
78
79
80
"""
A `str`-like field exclusively for URLs
"""
81

Emi Simpson's avatar
Emi Simpson committed
82
class ForceLogin:
Emi Simpson's avatar
Emi Simpson committed
83
84
85
	"""
	An outcome that designates that the user should be redirected to the login page
	"""
Emi Simpson's avatar
Emi Simpson committed
86
	pass
Emi Simpson's avatar
Emi Simpson committed
87
class Logout:
Emi Simpson's avatar
Emi Simpson committed
88
89
90
	"""
	An outcome that designates that the user should be logged out
	"""
Emi Simpson's avatar
Emi Simpson committed
91
	pass
Emi Simpson's avatar
Emi Simpson committed
92
class RebuildProjectsJson:
Emi Simpson's avatar
Emi Simpson committed
93
94
95
	"""
	Designates that projects.json should be overwritten with data from the database
	"""
Emi Simpson's avatar
Emi Simpson committed
96
97
	pass
class Error(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
98
99
100
	"""
	Designates that an error should be shown, related to this specific field
	"""
101
	component: Field
Emi Simpson's avatar
Emi Simpson committed
102
	message: str
103
	def to_tuple(self) -> Tuple[str, str]:
Emi Simpson's avatar
Emi Simpson committed
104
105
106
107
108
		"""
		The "category" to set the error to in flask

		This is later parsed to determine where the error should be shown
		"""
109
		return (self.message, f"error-{self.component}")
110
class AmendSession(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
111
112
113
114
	"""
	Designates that the provided key-value mappings should be applied ontop the session
	"""
	new_values: Mapping[str, Any]
115
class SubmitCoordinatorJob(NamedTuple):
Emi Simpson's avatar
Emi Simpson committed
116
117
118
119
120
121
122
123
124
	"""
	Indicates that the described job should be sent to the coordinator

	See also:  :meth:`mystic.coordinator.CoordinatorConnection.post_job()`
	"""
	tags: Collection[str]
	desc: str
	priority: int
	backends: Mapping[Backend, Collection[Url]]
Emi Simpson's avatar
Emi Simpson committed
125
126
127

Outcome: TypeAlias         \
	= ForceLogin           \
Emi Simpson's avatar
Emi Simpson committed
128
	| Logout               \
Emi Simpson's avatar
Emi Simpson committed
129
	| RebuildProjectsJson  \
130
	| SubmitCoordinatorJob \
131
	| AmendSession  \
132
	| Error
Emi Simpson's avatar
Emi Simpson committed
133
134
135
"""
A type for all possible outcomes
"""
136
137

Outcomes: TypeAlias = Collection[Outcome]
Emi Simpson's avatar
Emi Simpson committed
138
139
140
"""
One or more :class:`Outcome`s
"""