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

#! /usr/bin/env python3 

# -*- coding: utf-8 -*- 

 

#       Copyright 2011-2012, Milan Boers 

#       Copyright 2012-2013, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

import os 

import urllib.parse 

import urllib.error 

import urllib.request 

import socket 

import json 

import base64 

import io 

 

MAXVERSION = 1.0 

 

def getConnectWidget(): 

        class ConnectWidget(QtWidgets.QWidget): 

                """We use HTTP so there's not actually a continuous connection, but 

                   this class is basically here to check whether a server exists. 

 

                """ 

                def __init__(self, connection, *args, **kwargs): 

                        super().__init__(*args, **kwargs) 

 

                        self.connection = connection 

 

                        self.connectLayout = QtWidgets.QFormLayout() 

                        self.setLayout(self.connectLayout) 

 

                        self.serverField = QtWidgets.QLineEdit() 

                        self.serverLabel = QtWidgets.QLabel() 

                        self.connectLayout.addRow(self.serverLabel, self.serverField) 

 

                        self.connectButton = QtWidgets.QPushButton() 

                        self.connectButton.clicked.connect(lambda: self.connection.connect(self.serverField.text())) 

                        self.connectLayout.addRow(self.connectButton) 

 

                        self.serverField.returnPressed.connect(self.connectButton.click) 

 

                        self.retranslate() 

 

                def retranslate(self): 

                        self.serverLabel.setText(_("Server IP or hostname:")) 

                        self.connectButton.setText(_("Connect")) 

        return ConnectWidget 

 

def getLoginWidget(): 

        class LoginWidget(QtWidgets.QWidget): 

                def __init__(self, connection, loginid, *args, **kwargs): 

                        super().__init__(*args, **kwargs) 

 

                        self.connection = connection 

 

                        self.loginLayout = QtWidgets.QFormLayout() 

                        self.setLayout(self.loginLayout) 

 

                        self.usernameLabel = QtWidgets.QLabel() 

                        self.usernameField = QtWidgets.QLineEdit() 

                        self.loginLayout.addRow(self.usernameLabel, self.usernameField) 

 

                        self.passwordLabel = QtWidgets.QLabel() 

                        self.passwordField = QtWidgets.QLineEdit() 

                        self.passwordField.setEchoMode(QtWidgets.QLineEdit.Password) 

                        self.loginLayout.addRow(self.passwordLabel, self.passwordField) 

 

                        self.checkButton = QtWidgets.QPushButton() 

                        self.checkButton.clicked.connect(lambda: connection.checkLogin( 

                                self.usernameField.text(), 

                                self.passwordField.text(), 

                                loginid 

                        )) 

                        self.loginLayout.addRow(self.checkButton) 

 

                        self.passwordField.returnPressed.connect(self.checkButton.click) 

 

                def retranslate(self): 

                        self.usernameLabel.setText(_("Username:")) 

                        self.passwordLabel.setText(_("Password:")) 

                        self.checkButton.setText(_("Login")) 

        return LoginWidget 

 

def getConnectLoginWidget(): 

        class ConnectLoginWidget(QtWidgets.QWidget): 

                def __init__(self, connection, loginid, *args, **kwargs): 

                        super().__init__(*args, **kwargs) 

 

                        self.layout = QtWidgets.QStackedLayout() 

                        self.setLayout(self.layout) 

 

                        self.connectWidget = ConnectWidget(connection) 

                        connection.connected.handle(self.afterConnect) 

                        self.layout.addWidget(self.connectWidget) 

 

                        self.loginWidget = LoginWidget(connection, loginid) 

                        self.layout.addWidget(self.loginWidget) 

 

                        self.retranslate() 

 

                def afterConnect(self): 

                        self.layout.setCurrentWidget(self.loginWidget) 

 

                def retranslate(self): 

                        self.connectWidget.retranslate() 

                        self.loginWidget.retranslate() 

        return ConnectLoginWidget 

 

class Connection: 

        def __init__(self, modules, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._modules = modules 

 

                # Connected event 

                self.connected = self._modules.default(type="event").createEvent() 

                # LoggedIn event 

                self.loggedIn = self._modules.default(type="event").createEvent() 

 

                # Setup opener 

                self.opener = urllib.request.build_opener() 

                self.server = None 

                self.serverName = None 

                self.auth = False 

 

        def retranslate(self): 

143                if hasattr(self, "loginTab"): 

                        self.connectLoginWidget.retranslate() 

                        self.loginTab.title = _("Login") 

 

        @property 

        def connectedLoggedIn(self): 

                """'Connected' to the server and logged in?""" 

 

                return self.server != None and self.auth == True 

 

        def connect(self, hostname): 

                """'Connect' to server""" 

 

                try: 

                        assert hostname 

                        #Replace hostname by IP (so DNS is not needed at every 

                        #request. Will speed things up.) 

                        hostname = socket.gethostbyname(hostname) 

                except (socket.gaierror, AssertionError): 

                        # Could not connect 

                        dialogShower = self._modules.default(type="dialogShower") 

                        dialogShower.showError.send(self.loginTab, _("Could not connect to the server. Possibly wrong hostname.")) 

                else: 

                        print(hostname) 

                        # Everything OK, Connected 

                        self.server = hostname 

                        # Try to fetch the index page 

                        index = self.get("https://%s:8080/" % (hostname)) 

                        self.serverName = index["name"] 

 

                        self.connected.send() 

 

        def _open(self, request): 

                try: 

                        body = io.TextIOWrapper(self.opener.open(request), encoding='UTF-8') 

                        return json.load(body) 

                except urllib.error.HTTPError as e: 

                        return e 

 

        def get(self, path): 

                """Get path""" 

 

                url = "https://%s:8080/%s" % (self.server, path) 

                return self._open(url) 

 

        def post(self, path, data): 

                data = urllib.parse.urlencode(data) 

                request = urllib.request.Request("https://%s:8080/%s" % (self.server, path), data) 

                return self._open(request) 

 

        def put(self, path, data): 

                data = urllib.parse.urlencode(data) 

                request = urllib.request.Request("https://%s:8080/%s" % (self.server, path), data) 

                request.get_method = lambda: 'PUT' 

                return self._open(request) 

 

        def login(self, loginid): 

                """Method to log in. You need to send a uuid along, so your 

                   loggedIn doesn't react to other requests. 

 

                """ 

                if self.connectedLoggedIn: 

                        self._afterLogin(loginid) 

                else: 

                        self.connectLoginWidget = ConnectLoginWidget(self, loginid) 

 

                        module = self._modules.default("active", type="ui") 

                        self.loginTab = module.addCustomTab(self.connectLoginWidget) 

                        #set tab title by retranslating 

                        self.retranslate() 

 

                        self.loginTab.closeRequested.handle(self.loginTab.close) 

 

        def checkLogin(self, username, passwd, loginid): 

                """Checks if login is right (and implicitly fetches the token) 

                   (to be used only inside this module) 

 

                """ 

                loginCreds = u"%s:%s" % (username, passwd) 

                loginCreds = loginCreds.encode("utf-8") 

                loginCreds = base64.b64encode(loginCreds) 

 

                self.opener.addheaders = [("Authorization", b"Basic " + loginCreds)] 

 

                me = self.get("users/me") 

 

                if type(me) == urllib.error.HTTPError: 

                        # User was not logged in 

                        dialogShower = self._modules.default("active", type="dialogShower") 

                        dialogShower.showError.send(self.loginTab, _("Could not login. Wrong username or password.")) 

                else: 

                        self.userId = int(os.path.basename(me)) 

                        self._afterLogin(loginid) 

 

        def _afterLogin(self, loginid): 

                """Logged in, set var to server""" 

 

                self.auth = True 

                self.loginTab.close() 

                self.loggedIn.send(loginid) 

 

class TestModeConnectionModule: 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "testModeConnection" 

                self.priorities = { 

                        "default": 444, 

                } 

 

                self.uses = ( 

                        self._mm.mods(type="translator"), 

                ) 

                self.requires = ( 

                        self._mm.mods(type="event"), 

                        self._mm.mods(type="ui"), 

                ) 

                self.filesWithTranslations = ("connection.py",) 

 

        def _retranslate(self): 

                #Translations 

                global _ 

                global ngettext 

 

                try: 

                        translator = self._modules.default("active", type="translator") 

                except IndexError: 

exit                        _, ngettext = str, lambda a, b, n: a if n == 1 else b 

                else: 

                        _, ngettext = translator.gettextFunctions( 

                                self._mm.resourcePath("translations") 

                        ) 

 

                self.connection.retranslate() 

 

        def enable(self): 

                global QtWidgets 

                try: 

                        from PyQt5 import QtWidgets 

                except ImportError: 

                        return 

                global ConnectLoginWidget, ConnectWidget, LoginWidget 

                ConnectLoginWidget = getConnectLoginWidget() 

                ConnectWidget = getConnectWidget() 

                LoginWidget = getLoginWidget() 

 

                self._modules = set(self._mm.mods(type="modules")).pop() 

 

                self.connection = Connection(self._modules) 

 

                #setup translation 

                try: 

                        translator = self._modules.default("active", type="translator") 

                except IndexError: 

                        pass 

                else: 

                        translator.languageChanged.handle(self._retranslate) 

                self._retranslate() 

 

                self.active = True 

 

        def disable(self): 

                self.active = False 

 

                del self.connection 

                del self._modules 

 

        def getConnection(self): 

                return self.connection 

 

def init(moduleManager): 

        return TestModeConnectionModule(moduleManager)