def AddProject(request, profile_slug): if this_is_admin(request): if request.method == 'POST': ## save data ## get profile from user then save it profile = get_object(UserProfile, slug=profile_slug) if profile: ## create project add_project_form = ProjectForm(request.POST) if add_project_form.is_valid(): project = add_project_form.save() ## add project to profile profile.user_projects.add(project) ## redirect to profile user_link = reverse('profiles:profile', kwargs={'slug': profile_slug}) return redirect(user_link) else: return redirect_prev_page(request) else: return HttpResponse('profile not found') elif request.method == 'GET': add_project_form = ProjectForm context = { 'add_project_form': add_project_form, } return render(request, 'add-project.html', context)
def contact_view(request): # Проверка метода запроса if request['method'] == 'POST': data = request['data'] title = data['title'] text = data['text'] email = data['email'] print( f'Нам пришло сообщение от {email} с темой {title} и текстом {text}' ) return '200 OK', render('contact_post.html', title=title, text=text, email=email) else: return '200 OK', render('contact.html')
def Login(request): """ To login user and check if he is admin """ ## first check if he already logined and he try to XSS if is_logined(request): return redirect(profile) if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] ## if there is match for username and password user = authenticate(request, username=username, password=password) if user: login(request, user) ## if admin who logined redirect him to adminstration page if this_is_admin(request): return redirect(adminstration) return redirect(profile) else: err_msg(request, 'username or password is incorrect') return redirect(log_in) else: return render(request, 'login.html')
def Adminstration(request): if this_is_admin(request): context = {} if request.method == 'POST': ## this is create user from if request.POST.get('username'): ## get the field username = request.POST.get('username') ## check if exist already user = get_object(User, username=username) if not user: password = random_password() user = User() user.username = username user.set_password(password) user.save() userProfile = get_object(UserProfile, user=user) userProfile.p = password userProfile.save() context.update({'password': password}) else: err_msg(request, 'user already exist') ## this is search from if request.POST.get('search'): query = request.POST.get('search') search_type = request.POST.get('search-type') if search_type == 'user': search_response = UserProfile.objects.filter( user__username__startswith=query) elif search_type == 'project': search_response = Project.objects.filter( project_name__startswith=query) context.update({ 'search_type': search_type, 'search_response': search_response, }) ## - => to reverse the order profiles = UserProfile.objects.order_by('-user') projects = Project.objects.order_by('-created') context.update({ 'profiles': profiles, 'projects': projects, }) return render(request, 'adminstration.html', context) else: return redirect(profile)
def twilio_route(): phone_number = request.form['From'] message = request.form['Body'] user = Users.query.filter_by(contact = phone_number).filter_by(platform = 'twilio').first() if user == None: new_user = Users(generate_random_string(10,20), 'twilio', phone_number) db.session.add(new_user) db.session.commit() user = Users.query.filter_by(contact = phone_number).filter_by(platform = 'twilio').first() response_message = render(user, message) xml = '<Response><Message>' + response_message + '</Message></Response>' return Response(xml, mimetype = 'text/xml')
def load_profile(request, slug, context={}): """ to load user profile throw the slug """ profile = get_object(UserProfile,slug=slug) if profile: context.update({ 'profile' : profile, 'user' : profile.user, ## forignkey 'projects': profile.user_projects.all(), ## ManyToMany }) return render(request, 'profile.html', context) else: return HttpResponse('there is no profile for this user<br> contact with us')
def OpenProject(request, slug): ## check he is_logined(request) if is_logined(request): ## be sure that user ask for his project or admin who ask project, context = has_access_to_project(request, slug) if project: versions = project.project_versions.all() comment_form = CommentForm() context.update({ 'project': project, 'versions': versions, 'comment_field': comment_form, }) return render(request, 'project2.html', context) else: err_msg(request, 'You Dont Have Access For This Project') return redirect(profile) else: return redirect(log_in)
def EditProject(request, project_slug): if this_is_admin(request): if request.method == 'POST': data = loads(request.POST['data']) ## change Original data of project project_org_data = data['project_data'] project = get_object(Project, slug=project_slug) if project: project.project_name = project_org_data['project_name'] project.breif = project_org_data['brief'] project.under_work = project_org_data['worked_on'] project.can_try = project_org_data['can_try'] project.finished = project_org_data['finished'] project.save() ## if i create new version if data['new_version']: version_data = data['new_version'] version = create_elm_in_table( Version, version_number=version_data['version_number'], critical_version=version_data['cretical'], combitable_with_old_dependencies=version_data[ 'combitable'], ) project.project_versions.add(version) version_changes = version_data['changes'] for change_type, values in version_changes.items(): Class = change_types[change_type] for val in values: change = create_elm_in_table(Class, explain=val) connect_change_and_version(version, change) ## if i edited an old version if data['old_version']: version_data = data['old_version'] editted_meta_data = version_data['editted_meta_data'] editted_changes = version_data['editted_changes'] added_changes = version_data['added_changes'] ## editted_meta_data : is dict of version id to its changes for version_id, values in editted_meta_data.items(): version = get_object(Version, id=version_id) if version: version.version_number = values['version_number'] version.critical_version = values['cretical'] version.combitable_with_old_dependencies = values[ 'combitable'] version.save() ## editted_changes: change_id to change explain value for version_id, values in editted_changes.items(): for val in values: change_id = val['id'] value = val['val'] change = get_object(Change, id=change_id) if change: if value: change.explain = value change.save() else: version = get_object(Version, id=version_id) disconnect_change_and_version(version, change) ## new_changes for old versions for version_id, values in added_changes.items(): version = get_object(Version, id=version_id) for val in values: change_type = val['type'] Class = change_types[change_type] change = create_elm_in_table(Class, explain=val['val']) connect_change_and_version(version, change) url = reverse('projects:project', kwargs={'slug': project.slug}) return redirect(url) else: project = get_object(Project, slug=project_slug) if project: context = { 'project': project, } return render(request, 'edit-project.html', context) else: return HttpResponse('there is no such project')
def about(request): names = request.get('name', None) return '200 OK', render('about.html', names=names)
def index(request): secret = request.get('secret_key', None) # Используем шаблонизатор return '200 OK', render('index.html', secret=secret)