def before_request(): ''' Globale variabler Bliver kaldt før hver eneste kald ''' g.year = datetime.datetime.now().year g.siteName = app.config['APPLICATION_NAME'] g.siteOwner = app.config['APPLICATION_OWNER'] g.siteVersion = app.config['APPLICATION_VERSION'] g.baseUrl = url_for('index') g.PREFERRED_URL_SCHEME = app.config['PREFERRED_URL_SCHEME'] if 'LoggedIn' in session: g.userIsloggedIn = True else: g.userIsloggedIn = False if g.userIsloggedIn: g.userId = session['userID'] g.userFirstname = session['firstname'] g.userLastName = session['lastname']
def logout(): ''' Her skal kommentaren stå :) ''' if g.userIsloggedIn != True: flash('Du er ikke logget ind', 'info') return redirect(url_for('login')) else: session.pop('username', None) session.clear() flash('Du er nu logget ud') return redirect(url_for('index'))
def index(): print(session) try: if session['activeUser']: return redirect(url_for('login')) except KeyError: redirect('before_first_request') return render_template('main/home.html')
def decorated_function(*args, **kwargs): if g.user == None: return redirect( url_for('login', next=replaceHTTP(request.url), _external=True, _scheme=app.config['PREFERRED_URL_SCHEME'])) return f(*args, **kwargs)
def decorated_function(*args, **kwargs): ''' Er siden allerede logget ind så send brugen til forsiden ''' if g.userIsloggedIn == True: #hvis brugren er logget ind return redirect(url_for('index', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME'])) return f(*args, **kwargs)
def logout(): session['activeUser'] = False i = 0 found = False while i < len(users) and not found: if users[i] == session['username']: users.pop(i) found = True i += 1 session['username'] = None return redirect(url_for('index'))
def logoff(): ''' Log brugeren af og send til forsiden ''' session.clear() flash('Logget af', 'info') return redirect(url_for('index', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']))
def createUser(): ''' Her skal kommentaren stå :) ''' serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) returnURL = url_for('index') if (request.method == 'POST') and request.form['fromFrontPage'] == 'true' and g.userIsloggedIn == False: serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) return render_template('createuser.html',formerUsernameInput=request.form['username'], formerPasswordInput=request.form['userPassword'], setAuthenticationCode=serverAuthenticationCode) elif (request.method == 'POST') and g.userIsloggedIn == False: theUser = tryCreateUser(request.form['username'], request.form['userPassword'], request.form['KeepMeLoggedIn'], request.form['authenticity_token']) if theUser[2] != '': flash(theUser[1], theUser[2]) else: flash(theUser[1]) if theUser[0] == False: serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) return render_template('createuser.html',formerUsernameInput=request.form['username'], setAuthenticationCode=serverAuthenticationCode) return redirect(returnURL) else: if g.user != None: flash('Du er allrede logget ind som {}'.format(g.user),'info') return redirect(url_for('index')) else: serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) return render_template('createuser.html', setAuthenticationCode=serverAuthenticationCode)
def login(): sessionKeys = session.keys() if 'activeUser' in sessionKeys and 'username' in sessionKeys and 'currentUsernameReceiver' in sessionKeys: if session['activeUser']: return render_template('logged/chat.html') else: redirect('before_first_request') # GET if request.method == "GET": return redirect(url_for('index')) # POST if request.method == 'POST': username = request.form['username'] if username == '': flash("You must enter your username to login", 'danger') return redirect(url_for('index')) # try: # if username != session['username']: # userFound = chat.searchUser(session['username']) # if users[i] == session['username']: # chat.deleteUser() # except KeyError: # print('error') # redirect('before_first_request') if not users.searchUserByUsername(username): users.appendUser(username) chatCreated = users.appendChat([username, username]) chatCreated.submitMessage(username, username, "Hola") chatCreated.submitMessage(username, username, "Buenas") session['username'] = username session['activeUser'] = True session['currentUsernameReceiver'] = username return render_template('logged/chat.html')
def decorated_function(*args, **kwargs): ''' Hvis brugeren er ikke logget ind, så videre send brugen til login siden og tilføj et parameter med den efterspurgte side. ''' if g.userIsloggedIn == False: #hvis brugren ikke er logget ind #vidre send til LogUserIn funktionen, med HTTPS return redirect(url_for('logUserIn', next=replaceHTTP(request.url), _external=True, _scheme=app.config['PREFERRED_URL_SCHEME'])) return f(*args, **kwargs)
def before_request(): g.year = datetime.now().year g.siteName = 'Johannes\' Flask Boilerplate' g.baseUrl = url_for('index') if 'username' in session: user = session['username'] else: user = None if 'LoggedIn' in session: g.userIsloggedIn = True else: g.userIsloggedIn = False g.user = user
def update_document_from_dict(fileId, metadataDict, file): ''' Updater dokument fra metadata Dict og eller fil ''' orgDocument = Document.query.filter_by(id=fileId).first() # hente dokument if int(orgDocument.user_id) == int(g.userId): #hvis brugeren er ejer af dokument orgDocument.version = int(orgDocument.version) + 1 #opdater version 1+1 orgDocument.created = datetime.datetime.now() # ny dato if 'documentTitle' in metadataDict: # er 'documentTitle' i metadataDict så opdater dokument.'documentTitle' orgDocument.name = u''+str(metadataDict['documentTitle']) if 'documentSubtitle' in metadataDict: # er 'documentSubtitle' i metadataDict så opdater dokument.'documentSubtitle' orgDocument.subtitle = u''+str(metadataDict['documentSubtitle']) if 'documentDescription' in metadataDict: # er 'documentDescription' i metadataDict så opdater dokument.'documentDescription' orgDocument.description = u''+str(metadataDict['documentDescription']) if 'documentSubject' in metadataDict: # er 'documentSubject' i metadataDict så opdater dokument.'documentSubject' orgDocument.subject = u''+str(metadataDict['documentSubject']) if 'documentTopic' in metadataDict: # er 'documentTopic' i metadataDict så opdater dokument.'documentTopic' orgDocument.topic = u''+str(metadataDict['documentTopic']) if 'documentEducation_level' in metadataDict: # er 'documentEducation_level' i metadataDict så opdater dokument.'documentEducation_level' orgDocument.education_level = u''+str(metadataDict['documentEducation_level']) fileBlob = BytesIO(file.read()) #Læs fil i hukommelse if (zipfile.is_zipfile(fileBlob)): #Er fil docx ? - tilføjet pga. fejl i firefox med drag and drop orgDocument.original_file = fileBlob.getvalue() db.session.commit() #affyr sql return redirect(url_for('viewDocument', fileId=orgDocument.id, _external=True, _scheme=app.config['PREFERRED_URL_SCHEME'])) else: abort(403) #ellers set "not allowed http error"
def login(): ''' Her skal kommentaren stå :) ''' if 'next' in request.args: returnURL = request.args['next'] else: returnURL = url_for('index') if request.method == 'POST' and g.userIsloggedIn == False: loggedIn = tryLogin(request.form['username'], request.form['password'], request.form['KeepMeLoggedIn'], request.form['authenticity_token']) if loggedIn[2] != '': flash(loggedIn[1], loggedIn[2]) else: flash(loggedIn[1]) if loggedIn[0] == False: serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) return render_template('login.html', formerUsernameInput=request.form['username'], setAuthenticationCode=serverAuthenticationCode) return redirect(returnURL) else: if 'next' in request.args and g.userIsloggedIn: return redirect(request.args['next']) elif g.userIsloggedIn: flash('Du er allrede logget ind som {}'.format(g.user),'info') return redirect(returnURL) else: serverAuthenticationCode = bcrypt.generate_password_hash(app.config['SECRET_KEY'], 2) return render_template('login.html', setAuthenticationCode=serverAuthenticationCode)
def createUser(): ''' Hvis allrede loggedet ind send til forsiden (/) (GET) Hvis opret bruger side (POST) Lave ny bruger fra form ''' if 'next' in request.args: returnURL = request.args['next'] else: returnURL = url_for('index', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']) if (request.method == 'GET'): return render_create_user() if (request.method == 'POST'): return createUserFromDict(returnURL, request.form)
def logUserIn(): ''' Log brugeren ind (GET) Hvis Login bruger side (POST) Login fra form ''' if 'next' in request.args: returnURL = request.args['next'] else: returnURL = url_for('index', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']) if (request.method == 'GET'): return render_login_user() if (request.method == 'POST'): return loginUserFromDict(returnURL, request.form)
def decorated_function(*args, **kwargs): if g.user == None: return redirect(url_for('login', next=replaceHTTP(request.url), _external=True, _scheme=app.config['PREFERRED_URL_SCHEME'])) return f(*args, **kwargs)
def Add_Docx_to_database(file, metadata): ''' Henter infomation fra docx fil Og hvis der sendes metadata med, overskriver de docx standart metadater Derefter oprettet dockument i databasen ''' filename = file.filename.split(".docx",1)[0] #filnavn fileBlob = BytesIO(file.read()) #Læs docx i hukommelse newDocument = DATA_HTML(fileBlob, filename) # nyt objekt fra filblob og filnavn if 'documentTitle' in metadata: # er 'documentTitle' i metadata så lav ny variable name = u''+str(metadata['documentTitle']) else: name = u''+str(newDocument.documentTitle) if 'documentSubtitle' in metadata: # er 'documentSubtitle' i metadata så lav ny variable subtitle = u''+str(metadata['documentSubtitle']) else: subtitle = u''+str(None) if 'documentDescription' in metadata: # er 'documentDescription' i metadata så lav ny variable description = u''+str(metadata['documentDescription']) else: description = u''+str(newDocument.documentDescription) if 'documentSubject' in metadata: # er 'documentSubject' i metadata så lav ny variable subject = u''+str(metadata['documentSubject']) else: subject = u''+str(newDocument.documentSubject) if 'documentTopic' in metadata: # er 'documentTopic' i metadata så lav ny variable topic = u''+str(metadata['documentTopic']) else: topic = u''+str(newDocument.documentCategory) if 'documentEducation_level' in metadata: # er 'documentEducation_level' i metadata så lav ny variable education_level = u''+str(metadata['documentEducation_level']) else: education_level = u'' #opret nyt dokument objekt med al metadaten addDocument = Document(g.userId, name, subtitle, description, subject, topic, education_level, fileBlob.getvalue()) db.session.add(addDocument) #tilføj til databas db.session.commit() #affyr sql #og send til dokument visnings siden return redirect(url_for('viewDocument', fileId=addDocument.id, _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']))
def login(): ''' Gammle function skal slettes ''' return redirect(url_for('logUserIn'))