def my_profile(request): ## User is redirected here upon LOGIN this_user = functions.get_this_user(request) if this_user != None: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'There is still missing necessary information before you to get started.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Your account is inactive, contact [email protected] for help or more information.' ) return logout(request) else: if functions.is_instructor(request) or functions.is_customer( request): return profile_with_user(request, this_user.id) else: messages.error( request, 'Something went really wrong, contact [email protected] and we will help you.' ) return logout(request) else: messages.error(request, 'You are not logged in.') return redirect('account_login')
def my_profile(request): ## User is redirected here upon LOGIN this_user = functions.get_this_user(request) if this_user != None: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'Det saknas fortfarande nödvändig information om dig. Fyll i dem för att komma igång.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Ditt konto är inte aktivt, kontakta [email protected] så hjälper vi dig.' ) return logout(request) else: if functions.is_instructor(request) or functions.is_customer( request): return profile_with_user(request, this_user.id) else: messages.error( request, 'Något gick väldigt fel kontakta [email protected] så hjälper vi dig' ) return logout(request) else: messages.error(request, 'Du är inte inloggad') return redirect('account_login')
def single_event(request, event_id): if request.user.is_authenticated: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'Det saknas fortfarande nödvändig information om dig. Fyll i dem för att komma igång.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Ditt konto är inte aktivt, kontakta [email protected] så hjälper vi dig.' ) return logout(request) this_user = functions.get_this_user(request) try: this_event = HobbyEvent.objects.get(pk=event_id) except HobbyEvent.DoesNotExist: messages.error(request, 'Det där evenemanget existerar inte längre.') return all_events(request) VisitHobbyEvent.objects.create(user=this_user, hobby_event=this_event) if this_event.event_host.user == this_user or ( this_event.is_hidden == False and this_event.is_active == True and this_event.is_accepted == True and this_event.has_happened == False): if this_event.is_accepted == False: return create_event_terminal(request, this_event.id) else: if this_event.has_happened: messages.error(request, 'Det här evenemanget har passerat') else: messages.error(request, 'Det här evenemanget går inte längre att ses.') return all_events(request) try: HobbyEventSignup.objects.get(hobby_event=this_event, user=this_user, is_success=True) signed_up = True except HobbyEventSignup.DoesNotExist: signed_up = False return render(request, 'single_event_page.html', context={ 'this_event': this_event, 'this_user': this_user, 'signed_up': signed_up, 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' })
def profile_with_user(request, user_id): if request.user.is_authenticated: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'There is still missing necessary information before you to get started..' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Your account is inactive, contact [email protected] for help or more information.' ) return logout(request) this_user = functions.get_this_user(request) try: user = User.objects.get(pk=user_id) except User.DoesNotExist: messages.error(request, 'This profile no longer exists.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) profile_model = functions.get_profile_model(user) try: profile = profile_model.objects.get(user=user) except profile_model.DoesNotExist: messages.error(request, 'This profile no longer exists.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) if profile.is_active == False: messages.error(request, 'This profile is inactive.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) return render(request, 'profile_page.html', context={ 'profile': profile, 'this_user': this_user, 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' })
def follow_instructor(request): if request.user.is_authenticated: this_user = functions.get_this_user(request) profile_model = functions.get_profile_model(this_user) if request.method == "POST": user_id = request.POST.get('user_id') else: return my_profile(request) try: instructor_user = User.objects.get(pk=user_id) temp_model = functions.get_profile_model(instructor_user) if temp_model != Instructor: messages.error(request, 'You can only follow instructors.') return profile_with_user(request, user_id) except User.DoesNotExist: messages.error(request, 'This profile no longer exists.') return profile_with_user(request, user_id) try: Follower.objects.get(instructor=instructor_user.instructor, follower=this_user) messages.error( request, 'You are already following ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name + '.') return profile_with_user(request, user_id) except Follower.DoesNotExist: pass if profile_model == None: messages.error(request, 'You are not authorized to follow this profile') return profile_with_user(request, user_id) else: profile_model.objects.get(user=this_user) if this_user == instructor_user: messages.error(request, 'You cannot follow yourself') return profile_with_user(request, user_id) else: Follower.objects.create(instructor=instructor_user.instructor, follower=this_user) messages.success( request, 'You are now following ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name) return profile_with_user(request, user_id) else: messages.error(request, 'You have to be logged in') return profile_with_user(request, user_id)
def follow_instructor(request): if request.user.is_authenticated: this_user = functions.get_this_user(request) profile_model = functions.get_profile_model(this_user) if request.method == "POST": user_id = request.POST.get('user_id') else: return my_profile(request) try: instructor_user = User.objects.get(pk=user_id) temp_model = functions.get_profile_model(instructor_user) if temp_model != Instructor: messages.error(request, 'Du kan bara följa instruktörer.') return profile_with_user(request, user_id) except User.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre.') return profile_with_user(request, user_id) try: Follower.objects.get(instructor=instructor_user.instructor, follower=this_user) messages.error( request, 'Du följer redan ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name + '.') return profile_with_user(request, user_id) except Follower.DoesNotExist: pass if profile_model == None: messages.error(request, 'Du är inte behörig att följa instruktörer.') return profile_with_user(request, user_id) else: profile_model.objects.get(user=this_user) if this_user == instructor_user: messages.error(request, 'Du kan inte följa dig själv.') return profile_with_user(request, user_id) else: Follower.objects.create(instructor=instructor_user.instructor, follower=this_user) messages.success( request, 'Du följer nu ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name) return profile_with_user(request, user_id) else: messages.error(request, 'Du måste vara inloggad') return profile_with_user(request, user_id)
def unfollow_instructor(request): if request.user.is_authenticated: this_user = functions.get_this_user(request) profile_model = functions.get_profile_model(this_user) if request.method == "POST": user_id = request.POST.get('user_id') else: return my_profile(request) try: instructor_user = User.objects.get(pk=user_id) temp_model = functions.get_profile_model(instructor_user) if temp_model != Instructor: messages.error(request, 'You can only unfollow users.') return profile_with_user(request, user_id) except User.DoesNotExist: messages.error(request, 'This profile no longer exists.') return profile_with_user(request, user_id) if profile_model == None: messages.error(request, 'You are not authorized to unfollow instructors.') return profile_with_user(request, user_id) else: profile_model.objects.get(user=this_user) if this_user == instructor_user: messages.error(request, 'You cannot unfollow yourself.') return profile_with_user(request, user_id) else: follow_objects = Follower.objects.filter( instructor=instructor_user.instructor, follower=this_user) if len(follow_objects) == 0: messages.error(request, 'You are already not following this user.') return profile_with_user(request, user_id) else: for object in follow_objects: object.delete() messages.info( request, 'You have unfollowed ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name) return profile_with_user(request, user_id) else: messages.error(request, 'You have to be logged in.') return profile_with_user(request, user_id)
def unfollow_instructor(request): if request.user.is_authenticated: this_user = functions.get_this_user(request) profile_model = functions.get_profile_model(this_user) if request.method == "POST": user_id = request.POST.get('user_id') else: return my_profile(request) try: instructor_user = User.objects.get(pk=user_id) temp_model = functions.get_profile_model(instructor_user) if temp_model != Instructor: messages.error(request, 'Du kan bara avfölja instruktörer.') return profile_with_user(request, user_id) except User.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre.') return profile_with_user(request, user_id) if profile_model == None: messages.error(request, 'Du är inte behörig att avfölja instruktörer.') return profile_with_user(request, user_id) else: profile_model.objects.get(user=this_user) if this_user == instructor_user: messages.error(request, 'Du kan inte avfölja dig själv.') return profile_with_user(request, user_id) else: follow_objects = Follower.objects.filter( instructor=instructor_user.instructor, follower=this_user) if len(follow_objects) == 0: messages.error( request, 'Du följer redan inte den här instruktören.') return profile_with_user(request, user_id) else: for object in follow_objects: object.delete() messages.info( request, 'Du har nu avföljt ' + instructor_user.instructor.first_name + ' ' + instructor_user.instructor.last_name) return profile_with_user(request, user_id) else: messages.error(request, 'Du måste vara inloggad') return profile_with_user(request, user_id)
def edit_profile(request): this_user = functions.get_this_user(request) if this_user != None: if functions.is_instructor(request): account_form = InstructorForm account_obj = this_user.instructor elif functions.is_customer(request): account_form = CustomerForm account_obj = this_user.customer else: messages.error( request, 'Something went really wrong, contact [email protected] and we will help you.' ) return index(request) if request.method == "POST": form = account_form(request.POST, request.FILES, instance=account_obj) if form.is_valid(): form.save() account_obj.valid_profile = True account_obj.save() messages.success(request, 'Your profile has been changed.') request.method = "GET" return my_profile(request) else: messages.error( request, 'You profile was not changed. Double check to see that yourinformation is correct.' ) return render(request, 'edit_profile_page.html', context={'form': form}) else: form = account_form(None, instance=account_obj) return render(request, 'edit_profile_page.html', context={'form': form}) else: messages.error(request, 'You are not logged in.') return redirect('account_login')
def edit_profile(request): this_user = functions.get_this_user(request) if this_user != None: if functions.is_instructor(request): account_form = InstructorForm account_obj = this_user.instructor elif functions.is_customer(request): account_form = CustomerForm account_obj = this_user.customer else: messages.error( request, 'Något gick väldigt fel. Kontakta [email protected] för hjälp.' ) return index(request) if request.method == "POST": form = account_form(request.POST, request.FILES, instance=account_obj) if form.is_valid(): form.save() account_obj.valid_profile = True account_obj.save() messages.success(request, 'Din profil är ändrad.') request.method = "GET" return my_profile(request) else: messages.error( request, 'Din profil ändrades inte. Dubbelkolla gärna att allting är rätt.' ) return render(request, 'edit_profile_page.html', context={'form': form}) else: form = account_form(None, instance=account_obj) return render(request, 'edit_profile_page.html', context={'form': form}) else: messages.error(request, 'Du är inte inloggad som en instruktör') return redirect('account_login')
def instructors(request, hobby_name): this_user = functions.get_this_user(request) input_zip_code = request.GET.get('input_zip_code') if hobby_name == "malning": hobby_name = "målning" try: hobby = Hobby.objects.get(hobby_name=hobby_name) InstructorSearch.objects.create(user=this_user, search_string=hobby_name, zip_code_search=input_zip_code) instructors = Instructor.objects.filter(hobbies=hobby, valid_profile=True, is_private_instructor=True, is_active=True) hobby_events = HobbyEvent.objects.filter(hobby=hobby, is_active=True, is_accepted=True, is_hidden=False) hobby_events = [ event for event in hobby_events if event.has_happened == False ] except Hobby.DoesNotExist: hobby = None hobby_events = None instructors = [] instructors = list(instructors) worked, instructors, error = functions.sort_by_proximity( instructors, input_zip_code, request) if worked == False and error != None: messages.error(request, error) return render(request, 'instructors_page.html', context={ 'hobby': hobby, 'hobby_name': hobby_name, 'instructors': instructors, 'hobby_events': hobby_events })
def create_event_terminal(request, event_id): if functions.is_instructor(request): this_user = functions.get_this_user(request) try: this_event = HobbyEvent.objects.get(pk=event_id) except HobbyEvent.DoesNotExist: messages.error(request, 'Där gick något snett, försök igen.') return create_events(request) if this_event.event_host.user == this_user: if not this_event.is_accepted: if request.method == "POST": this_event.is_accepted = True this_event.save() request.method = "GET" messages.success(request, 'Ditt evenemang har skapats!') return single_event(request, event_id) else: return render(request, 'event_terminal_page.html', context={'this_event': this_event}) else: messages.error(request, 'Du har redan publicerat det här evenemanget.') return single_event(request, event_id) else: messages.error( request, 'Du har inte tillgång till det där evenmanget. Dubbelkolla så att du är inloggad på rätt konto.' ) return create_events(request) else: messages.error(request, 'Du är inte inloggad som instruktör.') return all_events(request)
def all_events(request): """ Filter and sort all events on five factors: 1. is_active, is_accepted, is_hidden: Do not include events that does not meet these criteria. 2. search_hobby_event: Filter away events that does not match the search string. 3. event_id: Sort by having the most recent events first. (Secondary priority sort) 4. event.has_happened: Do not include all events that already has happened. 5. input_zip_code: Sort by having the closest events first. (Primary priority sort) """ if request.user.is_authenticated: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'Det saknas fortfarande nödvändig information om dig. Fyll i dem för att komma igång.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Ditt konto är inte aktivt, kontakta [email protected] så hjälper vi dig.' ) return logout(request) this_user = functions.get_this_user(request) search_hobby_event = request.GET.get('search_hobby_event') input_zip_code = request.GET.get('input_zip_code') #Filter away events that does not match the search string. if search_hobby_event != None or input_zip_code != None: EventSearch.objects.create(user=this_user, search_string=search_hobby_event, zip_code_search=input_zip_code) if search_hobby_event != None and search_hobby_event != "": event_list1 = HobbyEvent.objects.filter( event_name__icontains=search_hobby_event, is_active=True, is_accepted=True, is_hidden=False) event_list3 = HobbyEvent.objects.filter( city__icontains=search_hobby_event, is_active=True, is_accepted=True, is_hidden=False) event_list4 = HobbyEvent.objects.filter( city_district__icontains=search_hobby_event, is_active=True, is_accepted=True, is_hidden=False) try: hobby = Hobby.objects.get(hobby_name__iexact=search_hobby_event) event_list2 = HobbyEvent.objects.filter(hobby=hobby, is_active=True, is_accepted=True, is_hidden=False) temp_list = event_list1 | event_list2 | event_list3 | event_list4 except Hobby.DoesNotExist: hobby = None event_list2 = None temp_list = event_list1 | event_list3 | event_list4 event_list = temp_list.distinct() if len(event_list) == 0: messages.error( request, 'Vi kunde inte hitta några evenemang för den här hobbyn.') request.method = "GET" else: hobby = None event_list = HobbyEvent.objects.filter(is_active=True, is_accepted=True, is_hidden=False) #Sort by having the most recent events first. (Secondary priority sort) event_list = event_list.order_by('-id') #Do not include events that already has happened event_list = [event for event in event_list if event.has_happened == False] worked, event_list, error = functions.sort_by_proximity( event_list, input_zip_code, request) if worked == False and error != None: messages.error(request, error) return render(request, 'all_events_page.html', context={ 'event_list': event_list, 'hobby': hobby, 'search_hobby_event': search_hobby_event, 'input_zip_code': input_zip_code })
def settings(request): this_user = functions.get_this_user(request) if this_user != None: return render(request, 'settings_page.html', {'this_user': this_user}) else: return index(request)
def profile_with_user_hobby(request, user_id, hobby): if request.user.is_authenticated: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'Det saknas fortfarande nödvändig information om dig. Fyll i dem för att komma igång.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Ditt konto är inte aktivt, kontakta [email protected] så hjälper vi dig.' ) return logout(request) try: profile_user = User.objects.get(pk=user_id) except User.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) try: instructor = Instructor.objects.get(user=profile_user) except Instructor.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) try: hobby = Hobby.objects.get(hobby_name=hobby) except Hobby.DoesNotExist: return profile_with_user(request, user_id) this_user = functions.get_this_user(request) VisitInstructor.objects.create(user=this_user, instructor=instructor, hobby=hobby) hobby_list = Hobby.objects.filter(instructor=instructor) if hobby not in hobby_list: return profile_with_user(request, user_id) if len( Follower.objects.filter(instructor=profile_user.instructor, follower=this_user)) > 0: following = True else: following = False followers = len( Follower.objects.filter(instructor=profile_user.instructor)) return render(request, 'profile_page.html', context={ 'hobby': hobby, 'profile': instructor, 'this_user': this_user, 'profile_user': profile_user, 'following': following, 'followers': followers, 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' })
def profile_with_user(request, user_id): if request.user.is_authenticated: status = functions.check_user_valid_profile(request) if status == 'not_valid': request.method = "GET" messages.info( request, 'Det saknas fortfarande nödvändig information om dig. Fyll i dem för att komma igång.' ) return edit_profile(request) elif status == 'not_active': messages.error( request, 'Ditt konto är inte aktivt, kontakta [email protected] så hjälper vi dig.' ) return logout(request) this_user = functions.get_this_user(request) try: user = User.objects.get(pk=user_id) except User.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) profile_model = functions.get_profile_model(user) try: profile = profile_model.objects.get(user=user) except profile_model.DoesNotExist: messages.error(request, 'Den här profilen existerar inte längre.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) if profile.is_active == False: messages.error(request, 'Den här profilen är avaktiverad.') return render(request, 'profile_page.html', context={ 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' }) if profile_model == Instructor: hobby_list = Hobby.objects.filter(instructor=profile) if len(hobby_list) == 1: return profile_with_user_hobby(request, user_id, hobby_list[0]) else: hobby_list = [] return render(request, 'profile_page.html', context={ 'hobby_list': hobby_list, 'profile': profile, 'this_user': this_user, 'spam_uri': 'https://www.spelapaintball.com/#wpcf7-f2197-o1' })