Best Python code snippet using playwright-python
WikipediaPage.py
Source:WikipediaPage.py
1from .exceptions import PageError, RedirectError, ODD_ERROR_MESSAGE2from .InfoBox import InfoBox3from .get_wikipedia_id import get_wikipedia_id, get_page_title, get_page_language, get_page_namespace4from .Page_helpers import *5from .is_wikipedia_page_url import is_wikipedia_page_url6from .is_wikipedia_page_url import is_mobile_wikipedia_page_url7from .is_wikipedia_page_url import convert_mobile_wikipedia_page_url_to_normal_page8import re9from pensieve import Pensieve10from slytherin.collections import remove_list_duplicates, flatten11from ravenclaw.wrangling import standardize_columns12from chronometry.progress import ProgressBar13from silverware import Spoon14from bs4 import BeautifulSoup15from pandas import DataFrame16import warnings17class WikipediaPage:18 def __init__(19 self, wikipedia, id=None, url=None, title=None, namespace=None, redirect=True, disambiguation_url=None,20 ignore_error=False21 ):22 self._wikipedia = wikipedia23 self._id = id24 self._url = url25 self._title = title26 self._ignore_error = ignore_error27 self._setup_pensieve()28 self.pensieve['namespace'] = namespace29 self.pensieve['redirect'] = redirect30 self.pensieve['disambiguation_url'] = disambiguation_url31 self._load_primary()32 try:33 self._load_the_rest()34 except Exception as e:35 warnings.warn(f'failed to load the rest id: "{self._id}", url: "{self._url}", title: "{self._title}"')36 if not ignore_error:37 raise e38 _STATE_ATTRIBUTES_ = ['_id', '_url', '_title', '_ignore_error']39 def __getstate__(self):40 state = {key: getattr(self, key) for key in self._STATE_ATTRIBUTES_}41 state['_pensieve'] = self._pensieve.get_contents()42 return state43 def __setstate__(self, state):44 for key, value in state.items():45 setattr(self, key, value)46 self._pensieve = state['_pensieve']47 self._load_primary()48 try:49 self._load_the_rest()50 except Exception as e:51 warnings.warn(f'failed to load the rest id: "{self._id}", url: "{self._url}", title: "{self._title}"')52 if not self._ignore_error:53 raise e54 def __hashkey__(self):55 return (self.__class__.__name__, tuple(getattr(self, name) for name in self._STATE_ATTRIBUTES_))56 def _setup_pensieve(self):57 self._pensieve = Pensieve(58 safe=False, function_durations=self.wikipedia.function_durations, warn_unsafe=False, hide_ignored=False59 )60 def _load_primary(self):61 if self._id or self._title or self._url:62 pass63 else:64 raise ValueError('Either id or title or url should be given!')65 if self._id:66 self.pensieve['original_id'] = self._id67 try:68 self._load_from_id()69 except Exception as e:70 warnings.warn(f'failed to load from id: "{self._id}"')71 if not self._ignore_error:72 raise e73 elif self._url:74 if is_wikipedia_page_url(url=self._url):75 if is_mobile_wikipedia_page_url(url=self._url):76 url = convert_mobile_wikipedia_page_url_to_normal_page(url=self._url)77 else:78 url = self._url79 self.pensieve['url'] = url80 try:81 self._load_from_url()82 except Exception as e:83 warnings.warn(f'failed to load from url: "{self._url}"')84 if not self._ignore_error:85 raise e86 else:87 raise ValueError(f'{self._url} does not match the wikipedia page pattern!')88 elif self._title:89 self.pensieve['original_title'] = self._title90 try:91 self._load_from_title()92 except Exception as e:93 warnings.warn(f'failed to load from title: "{self._title}"')94 if not self._ignore_error:95 raise e96 @property97 def wikipedia(self):98 """99 :rtype: .Wikipedia_class.Wikipedia100 """101 if self._wikipedia is None:102 raise AttributeError('Wikipedia API is missing!')103 else:104 return self._wikipedia105 def __eq__(self, other):106 """107 :type other: WikipediaPage108 :rtype: bool109 """110 if isinstance(other, self.__class__):111 return self['url'] == other['url']112 else:113 return False114 def __str__(self):115 if 'url' in self.pensieve:116 url = self.pensieve['url']117 return f'{self.title}: {url} '118 else:119 return f'{self.title}: {self.id} '120 def __repr__(self):121 return str(self)122 def __getitem__(self, item):123 return self.pensieve[item]124 def __graph__(self):125 return self.pensieve.__graph__()126 @property127 def title(self):128 """129 :rtype: str130 """131 if 'title' in self.pensieve:132 return self.pensieve['title']133 else:134 return self.pensieve['original_title']135 @property136 def id(self):137 """138 :rtype: int139 """140 if 'id' in self.pensieve:141 return self.pensieve['id']142 else:143 return self.pensieve['original_id']144 @property145 def url(self):146 if 'url' not in self.pensieve:147 raise AttributeError(f'Page {self} does not have a url!')148 elif self.pensieve['url'] is None:149 raise AttributeError(f'Page {self} does not have a url!')150 return self.pensieve['url']151 @property152 def pensieve(self):153 """154 :rtype: Pensieve155 """156 return self._pensieve157 @property158 def base_url(self):159 """160 :rtype: str161 """162 return 'http://' + self.wikipedia.language + '.wikipedia.org'163 @wikipedia.setter164 def wikipedia(self, wikipedia):165 """166 :type wikipedia: .Wikipedia_class.Wikipedia167 """168 self._wikipedia = wikipedia169 def get_children(self, echo=1):170 link_lists = self['link_list']171 if link_lists:172 urls = remove_list_duplicates([link.url for link in flatten(link_lists)])173 wikipedia_urls = [url for url in urls if re.match('^https://.+\.wikipedia.org/', url)]174 non_php_urls = [url for url in wikipedia_urls if '/index.php?' not in url]175 pages = ProgressBar.map(176 function=lambda x: self.__class__(url=x, redirect=self['redirect'], wikipedia=self.wikipedia),177 iterable=non_php_urls, echo=echo, text=self['url']178 )179 return pages180 else:181 return []182 def request(self, url=None, parameters=None, format='html'):183 return self.wikipedia.request(url=url, parameters=parameters, format=format)184 def clear(self):185 new_pensieve = Pensieve(safe=True)186 for key in ['original_id', 'original_title', 'namespace', 'redirect', 'redirected_from']:187 new_pensieve[key] = self.pensieve[key]188 self._pensieve = new_pensieve189 def _search_page(self, id, title, redirect, redirected_from, num_recursions=0):190 if num_recursions > 3:191 raise RecursionError()192 # print(dict(title=title, id=id, redirect=redirect, redirected_from=redirected_from, num_recursions=num_recursions))193 search_query_parameters = get_search_parameters(id=id, title=title)194 search_request = self.request(parameters=search_query_parameters, format='json')195 query = search_request['query']196 id = list(query['pages'].keys())[0]197 page = query['pages'][id]198 title = page['title']199 full_url = page['fullurl']200 language = page['pagelanguage']201 namespace = page['ns']202 # missing is present if the page is missing203 if 'missing' in page:204 raise PageError(id=id, title=title)205 # same thing for redirect, except it shows up in query instead of page for206 # whatever silly reason207 elif 'redirects' in query:208 if redirect:209 redirects = query['redirects'][0]210 if 'normalized' in query:211 normalized = query['normalized'][0]212 assert normalized['from'] == self.title, ODD_ERROR_MESSAGE213 from_title = normalized['to']214 else:215 from_title = self.title216 assert redirects['from'] == from_title, ODD_ERROR_MESSAGE217 # change the title and reload the whole object218 return self._search_page(219 id=id, title=redirects['to'],220 redirect=redirect, redirected_from=redirects['from'],221 num_recursions=num_recursions+1222 )223 else:224 raise RedirectError(getattr(self, 'title', page['title']))225 # since we only asked for disambiguation in pageprop,226 # if a pageprop is returned,227 # then the page must be a disambiguation page228 elif 'pageprops' in page:229 return {230 'id': int(id), 'title': title, 'page': page, 'redirected_from': redirected_from,231 'full_url': full_url, 'language': language, 'namespace': namespace, 'disambiguation': True232 }233 else:234 return {235 'id': int(id), 'title': title, 'page': page, 'redirected_from': redirected_from,236 'full_url': full_url, 'language': language, 'namespace': namespace, 'disambiguation': False237 }238 def _get_url_response(self):239 self.pensieve.store(240 key='url_response', precursors='url', evaluate=False,241 function=lambda x: self.request(url=x, format='response')242 )243 def _load_from_url(self):244 self._get_url_response()245 self.pensieve.store(246 key='original_id', precursors=['url_response'], evaluate=False,247 function=lambda x: get_wikipedia_id(x.text)248 )249 self.pensieve.store(250 key='search_result', precursors=['original_id', 'redirect'], evaluate=False,251 function=lambda x: self._search_page(252 title=None, id=x['original_id'], redirect=x['redirect'],253 redirected_from=None254 )255 )256 self.pensieve.store(257 key='id', precursors=['url_response'], evaluate=False,258 function=lambda x: get_wikipedia_id(x.text)259 )260 self.pensieve.store(261 key='title', precursors=['url_response'], evaluate=False,262 function=lambda x: get_page_title(x.text)263 )264 self.pensieve.store(265 key='language', precursors=['url_response'], evaluate=False,266 function=lambda x: get_page_language(x.text)267 )268 self.pensieve.store(269 key='namespace', precursors=['url_response'], evaluate=False,270 function=lambda x: get_page_namespace(x.text)271 )272 self.pensieve.store(273 key='full_url', precursors=['url'], evaluate=False,274 function=lambda x: x275 )276 self.pensieve['disambiguation'] = False277 self.pensieve['redirected_from'] = None278 def _load_from_id(self):279 self.pensieve.store(280 key='search_result', precursors=['original_id', 'redirect'], evaluate=False,281 function=lambda x: self._search_page(282 title=None, id=x['original_id'], redirect=x['redirect'],283 redirected_from=None284 )285 )286 self.pensieve.decouple(key='search_result', prefix='')287 try:288 self.pensieve.store(289 key='json', precursors=['id', 'title'], evaluate=False,290 function=lambda x: self._get_json(id=x['id'], title=x['title'])291 )292 except Exception as e:293 display(self.pensieve)294 raise e295 self.pensieve.store(key='url', precursors=['page'], function=lambda x: x['fullurl'], evaluate=False)296 self._get_url_response()297 def _load_from_title(self):298 self.pensieve.store(299 key='search_result', precursors=['original_title', 'redirect'], evaluate=False,300 function=lambda x: self._search_page(301 title=x['original_title'], id=None, redirect=x['redirect'],302 redirected_from=None303 )304 )305 self.pensieve.decouple(key='search_result')306 self.pensieve.store(307 key='json', precursors=['id', 'title'], evaluate=False,308 function=lambda x: self._get_json(id=x['id'], title=x['title'])309 )310 self.pensieve.store(311 key='url', precursors=['page'],312 function=lambda x: x['fullurl'], evaluate=False313 )314 self._get_url_response()315 def _load_the_rest(self):316 self.pensieve.store(317 key='base_url', precursors=['url'],318 evaluate=False, materialize=False,319 function=lambda x: x[:x.find('/wiki/')]320 )321 # main parts322 def _add_see_also_flag(x):323 x = re.sub(324 r'<h2>.*>(references|external\s+links|notes\s+and\s+references|see\s+also)<.*</h2>',325 '<h2>SEEALSO</h2><ul><li><a href="http://SEEALSO" title="SEEALSO">SEEALSO</a></li></ul>',326 x,327 flags=re.IGNORECASE328 )329 return x330 def _get_beautiful_soup(url_response):331 text = _add_see_also_flag(url_response.text.replace('\\u0026', 'and').replace('&', 'and'))332 soup = BeautifulSoup(text, 'lxml')333 for element in soup.find_all(name='div', attrs={'id': 'mw-panel'}):334 element.decompose()335 for element in soup.find_all(name='div', attrs={'id': 'mw-head'}):336 element.decompose()337 return soup338 self.pensieve.store(339 key='separated_body', precursors=['url_response'], evaluate=False,340 function=lambda url_response: separate_body_from_navigation_and_info_box(341 url_response=url_response342 )343 )344 self.pensieve.store(345 key='body',346 function=lambda separated_body: separated_body['body']347 )348 self.pensieve.store(349 key='headers', precursors=['body'], evaluate=False,350 function=lambda x: x.find_all(['h1', 'h2', 'h3'])351 )352 self.pensieve.store(353 key='info_box', evaluate=False,354 function=lambda separated_body: InfoBox(separated_body['info_box'])355 )356 self.pensieve.store(357 key='vertical_navigation_box', evaluate=False,358 function=lambda separated_body: separated_body['vertical_navigation_box']359 )360 self.pensieve.store(361 key='navigation_boxes', evaluate=False,362 function=lambda separated_body: [363 box for box in separated_body['navigation_boxes']364 ]365 )366 self.pensieve.store(367 key='category_box', evaluate=False,368 function=lambda separated_body: separated_body['category_box']369 )370 # end of main parts371 self.pensieve.store(372 key='paragraphs', precursors=['body'], evaluate=False,373 function=lambda x: get_main_paragraphs(body=x)374 )375 self.pensieve.store(376 key='paragraph_links', precursors=['paragraphs', 'base_url'], evaluate=False,377 function=lambda x: [378 [379 link.url380 for link in Spoon.find_links(element=paragraph, base_url=x['base_url'])381 if isinstance(link, Link)382 ]383 for paragraph in x['paragraphs']384 ]385 )386 self.pensieve.store(387 key='category_links', evaluate=False,388 function=lambda category_box, base_url: Spoon.find_links(389 element=category_box, base_url=base_url390 )391 )392 self.pensieve.store(393 key='categories', evaluate=False,394 function=get_categories395 )396 self.pensieve.store(397 key='disambiguation_results',398 precursors=['disambiguation', 'body', 'base_url'],399 evaluate=False,400 function=lambda x: get_disambiguation_results(401 disambiguation=x['disambiguation'], html=x['body'], base_url=x['base_url']402 )403 )404 self.pensieve.store(405 key='tables',406 precursors=['body', 'base_url'],407 evaluate=False,408 function=lambda x: [409 standardize_columns(data=table)410 for table in Spoon.filter(411 soup=x['body'], name='table', attributes={'class': 'wikitable'}412 ).read_tables(base_url=x['base_url'], parse_links=True)413 ]414 )415 self.pensieve.store(416 key='table_links',417 precursors=['tables'],418 evaluate=False,419 function=find_main_links_in_tables420 )421 self.pensieve.store(422 key='link_and_anchors', precursors=['body', 'base_url'], evaluate=False,423 function=lambda x: get_anchors_and_links(soup=x['body'], base_url=x['base_url'])424 )425 self.pensieve.store(426 key='link_and_anchor_list', precursors=['link_and_anchors'], evaluate=False,427 function=lambda x: x['list_link_and_anchors']428 )429 self.pensieve.store(430 key='nested_link_and_anchor_lists', precursors=['body', 'base_url'], evaluate=False,431 function=lambda x: Spoon.get_lists(element=x['body'], links_only=True, base_url=x['base_url'])432 )433 # LINKS IN A PAGE434 def _remove_anchors(link_lists):435 if isinstance(link_lists, list):436 return [_remove_anchors(x) for x in link_lists if x is not None]437 elif isinstance(link_lists, Link):438 if link_lists.url.startswith('#'):439 return None440 else:441 return link_lists442 # LINKS IN A PAGE443 def _remove_nonanchors(link_lists):444 if isinstance(link_lists, list):445 return [_remove_anchors(x) for x in link_lists if x is not None]446 elif isinstance(link_lists, Link):447 if not link_lists.url.startswith('#'):448 return None449 else:450 return link_lists451 self.pensieve.store(452 key='nested_anchor_lists', precursors=['nested_link_and_anchor_lists'], evaluate=False,453 function=_remove_nonanchors454 )455 self.pensieve.store(456 key='anchor_list', precursors=['link_and_anchor_list'], evaluate=False,457 function=_remove_nonanchors458 )459 self.pensieve.store(460 key='nested_link_lists', precursors=['nested_link_and_anchor_lists'], evaluate=False,461 function=_remove_anchors462 )463 self.pensieve.store(464 key='link_list', precursors=['link_and_anchor_list'], evaluate=False,465 function=_remove_anchors466 )467 self.pensieve.store(468 key='summary', precursors=['id', 'title'], evaluate=False,469 function=lambda x: get_page_summary(page=self, id=x['id'], title=x['title'])470 )471 self.pensieve.store(472 key='content', precursors=['id', 'title'], evaluate=False,473 function=lambda x: self._get_content(id=x['id'], title=x['title'])474 )475 self.pensieve.store(476 key='extract', precursors=['content'], evaluate=False, materialize=False,477 function=lambda x: x['extract']478 )479 self.pensieve.store(480 key='revision_id', precursors=['content'], evaluate=False, materialize=False,481 function=lambda x: x['revisions'][0]['revid']482 )483 self.pensieve.store(484 key='parent_id', precursors=['content'], evaluate=False, materialize=False,485 function=lambda x: x['revisions'][0]['parentid']486 )487 def _get_json(self, id, title):488 id = str(id)489 html_query_parameters = get_html_parameters(id=id, title=title)490 html_request = self.request(parameters=html_query_parameters, format='json')491 return html_request['query']['pages'][id]['revisions'][0]['*']492 def _get_content(self, id, title):493 id = str(id)494 content_parameters = get_content_parameters(id=id, title=title)495 content_request = self.request(parameters=content_parameters, format='json')496 return content_request['query']['pages'][id]497 @property498 def body(self):499 """500 :rtype: BeautifulSoup501 """502 return self['body']503 @property504 def tables(self):505 """506 :rtype: list[DataFrame]507 """508 return self['tables']509 def __hashkey__(self):...
views.py
Source:views.py
1from datetime import datetime2from django.shortcuts import render3from django.http import HttpResponseRedirect4from django.contrib import messages5from django.views.generic import TemplateView, ListView6from django.contrib.auth.decorators import login_required7from django.contrib.auth.models import User8from .models import (9 Room, Table, Task, Nessues_Group, 10 Nessues_Group_User, Invitation)11from .forms import (12 CreateRoomForm, CreateTableForm, CreateTaskForm, UpdateTaskForm, 13 CompleteTaskForm, DeleteRoomForm, CreateGroupForm, CloseGroupForm,14 AcceptInvitationForm, RejectInvitationForm, SendInvitationForm)15def home_view(request):16 title = "home"17 return render(request, 'nessues_app/home.html', {'title': title})18"""19 data that need to be rendered on the page: 20 1) title21 2) available_groups22 3) forms23 new group24"""25class GroupsView(TemplateView):26 template_name = 'nessues_app/groups.html'27 title = 'group'28 create_group_class = CreateGroupForm29 def get(self, request, *args, **kwargs):30 available_groups = Nessues_Group_User.objects.filter(user=self.request.user.id)31 create = self.create_group_class()32 return render(request, self.template_name, {'available_groups': available_groups, 'create': create})33 def post(self, request, *args, **kwargs):34 create = self.create_group_class(request.POST)35 if create.is_valid():36 create.save()37 current_group = Nessues_Group.objects.get(name=create.cleaned_data['name'])38 current_group.users.add(self.request.user.id, through_defaults={'role': 1})39 current_group.save()40 messages.success(request, "Group added")41 return HttpResponseRedirect('/groups')42 else:43 messages.warning(request, "Group with the same name already exists")44 return HttpResponseRedirect('/groups')45 return render(request, self.template_name, {'create': create})46"""47 data that need to be rendered on the page: 48 1) title49 2) available_rooms50 3) forms51 new room52"""53class RoomsView(TemplateView):54 template_name = 'nessues_app/mono_rooms.html'55 title = 'rooms'56 create_room_class = CreateRoomForm57 def get(self, request, *args, **kwargs):58 available_rooms = Room.objects.filter(owner=self.request.user.id)59 create = self.create_room_class(initial={'owner': self.request.user.id})60 return render(request, self.template_name, {'title': self.title, 'available_rooms': available_rooms, 'create': create})61 def post(self, request, *args, **kwargs):62 create = self.create_room_class(request.POST)63 if create.is_valid():64 create.save()65 return HttpResponseRedirect('/rooms')66 else: 67 messages.warning(request, "Something went wrong while creating room")68 return HttpResponseRedirect('/rooms')69 return render(request, self.template_name, {'create': create})70class TablesView(TemplateView):71 template_name = 'nessues_app/tables.html'72 title = 'tables'73 create_table_class = CreateTableForm74 delete_room_class = DeleteRoomForm75 close_group_class = CloseGroupForm76 invite_user_class = SendInvitationForm77 def get(self, request, *args, **kwargs):78 if self.kwargs['redirected_from'] == 'group':79 try:80 current = Nessues_Group.objects.get(id=self.kwargs['key_id'])81 user_role = Nessues_Group_User.objects.get(group=self.kwargs['key_id'], user=self.request.user.id).role82 try:83 available_tables = Table.objects.filter(group=current.id)84 except:85 available_tables = None86 create = self.create_table_class(initial={'group': current.id})87 delete = self.close_group_class(initial={'group': current.id})88 invite = self.invite_user_class(initial={'group': current.id})89 except:90 pass91 92 if self.kwargs['redirected_from'] == 'room':93 try:94 current = Room.objects.get(id=self.kwargs['key_id'])95 try:96 available_tables = Table.objects.filter(room=current.id)97 except:98 available_tables = None99 create = self.create_table_class(initial={'room': self.kwargs['key_id']})100 delete = self.delete_room_class(initial={'room': self.kwargs['key_id']})101 except:102 pass103 104 try:105 return render(request, self.template_name, {'title': self.title, 'current_type': self.kwargs['redirected_from'], 'current': current.id, 'current_name': current.name, 'available_tables': available_tables, 'create': create, 'delete': delete, 'invite': invite, 'user_role': user_role})106 except:107 return render(request, self.template_name, {'title': self.title, 'current_type': self.kwargs['redirected_from'], 'current': current.id, 'current_name': current.name, 'available_tables': available_tables, 'create': create, 'delete': delete})108 109 def post(self, request, *args, **kwargs):110 create = self.create_table_class(request.POST)111 invite = self.invite_user_class(request.POST)112 delete = self.delete_room_class(request.POST)113 if create.is_valid():114 create.save()115 return HttpResponseRedirect(f"/tables/{self.kwargs['redirected_from']}/{self.kwargs['key_id']}")116 if invite.is_valid():117 user_to_invite = invite.cleaned_data['user']118 group_send_invitation_from = invite.cleaned_data['group']119 print(f"CLEANED DATA: {invite.cleaned_data}")120 121 try:122 user_exists = User.objects.get(username=user_to_invite)123 try:124 if not Invitation.objects.filter(group=group_send_invitation_from, user=user_to_invite.id):125 try:126 if not Nessues_Group_User.objects.filter(group=group_send_invitation_from, user=user_to_invite.id):127 messages.success(request, "Invitation was sent")128 invite.save()129 except:130 messages.warning(request, "User already participate in the current group")131 except:132 messages.warning(request, "You cannot send invitation twice to the same user")133 except:134 messages.warning(request, "No user with this name")135 return HttpResponseRedirect(f"/tables/{self.kwargs['redirected_from']}/{self.kwargs['key_id']}")136 else:137 # messages.message("Something went wrong, please check provided username")138 print("INVALID INVITATION FORM", invite)139 140 if delete.is_valid():141 id_to_delete = delete.cleaned_data['id']142 if self.kwargs['redirected_from'] == 'room': 143 current_to_delete = Room.objects.get(id=self.kwargs['key_id']).id144 elif self.kwargs['redirected_from'] == 'group':145 current_to_delete = Nessues_Group.objects.get(id=self.kwargs['key_id']).id146 if current_to_delete != id_to_delete:147 messages.warning(request, f"You were restricted to delete other {{ self.kwargs['redirected_from'] }}s")148 return HttpResponseRedirect(f"/tables/{self.kwargs['redirected_from']}/{self.kwargs['key_id']}")149 try: 150 if self.kwargs['redirected_from'] == 'room':151 to_delete = Room.objects.get(id=id_to_delete) 152 elif self.kwargs['redirected_from'] == 'group':153 to_delete = Nessues_Group.objects.get(id=id_to_delete) 154 to_delete.delete()155 messages.success(request, f"{{ self.kwargs['redirected_from'] }} deleted successfully")156 return HttpResponseRedirect("/account")157 except Exception:158 messages.warning(request, 'Wrong id provided, try again')159 return HttpResponseRedirect(f"/tables/{self.kwargs['redirected_from']}/{self.kwargs['key_id']}")160 return render(request, self.template_name, {'create': create, 'delete': delete})161"""162 Forms to handle: 163 1) CreateTaskForm164 2) UpdateTaskForm165 3) CompleteTaskForm166"""167class TasksView(TemplateView):168 template_name = 'nessues_app/tasks.html'169 create_task_class = CreateTaskForm170 update_task_class = UpdateTaskForm171 complete_task_class = CompleteTaskForm172 def get(self, request, *args, **kwargs): 173 available_tasks = Task.objects.filter(table=self.kwargs['key_id'])174 create = self.create_task_class(initial={'table': self.kwargs['key_id'], 'created_by': self.request.user.id})175 update = self.update_task_class(initial={'table': self.kwargs['key_id'], 'created_by': self.request.user.id})176 complete = self.complete_task_class(initial={'table': self.kwargs['key_id'], 'created_by': self.request.user.id})177 return render(request, self.template_name, {'url_arguments': {'key_id': self.kwargs['key_id'], 'current_user': self.request.user.id}, 'available_tasks': available_tasks, 'create': create, 'update': update, 'complete': complete})178 def post(self, request, *args, **kwargs):179 create = self.create_task_class(request.POST)180 update = self.update_task_class(request.POST)181 complete = self.complete_task_class(request.POST)182 if create.is_valid():183 create.save()184 # messages.success(request, 'Task Successfully added')185 return HttpResponseRedirect(f'/tasks/{self.kwargs["key_id"]}')186 if update.is_valid():187 update.save()188 messages.success(request, 'Task successfully updated')189 return HttpResponseRedirect(f'/tasks/{self.kwargs["key_id"]}')190 if complete.is_valid():191 id_to_close = complete.cleaned_data['id']192 try: 193 task_to_complete = Task.objects.get(id=id_to_close) 194 task_to_complete.delete()195 except Exception:196 messages.warning(request, 'Wrong id provided, try again')197 return HttpResponseRedirect(f'/tasks/{self.kwargs["key_id"]}')198 messages.success(request, 'Task successfully completed')199 return HttpResponseRedirect(f'/tasks/{self.kwargs["key_id"]}')200 201 return render(request, self.template_name, {'create': create, 'update': update, 'complete': complete})202"""203 Forms to handle:204 1) accept invitation;205 2) refuse invitation.206"""207class InvitationsView(TemplateView): 208 template_name = 'nessues_app/invitations.html'209 accept_invitation_class = AcceptInvitationForm210 reject_invitation_class = RejectInvitationForm211 def get(self, request, *args, **kwargs):212 available_invitation = Invitation.objects.filter(user=self.request.user.id)213 accept = self.accept_invitation_class()214 reject = self.reject_invitation_class()215 return render(request, self.template_name, {'available_invitations': available_invitation, 'accept': accept, 'reject': reject})216 def post(self, request, *args, **kwargs):217 accept = self.accept_invitation_class(request.POST)218 reject = self.reject_invitation_class(request.POST)219 # create new relation between user and nessues_group, role = 3 (common user). At the end, if success - delete the invitation220 if accept.is_valid():221 try:222 current_user = User.objects.get(id=self.request.user.id)223 invitation_to_accept = Invitation.objects.get(id=accept.cleaned_data['id'])224 group_where_to_add = Nessues_Group.objects.get(id=invitation_to_accept.group.id)225 group_where_to_add.users.add(self.request.user.id, through_defaults={'role': 3}) # bug here226 group_where_to_add.save()227 invitation_to_accept.delete()228 message.success(request, "You successfully accepted the request to join the group")229 return HttpResponseRedirect('/invitations')230 except:231 messages.warning(request, "Something went wrong, please try again")232 # delete row from the invitations table233 if reject.is_valid():234 try:235 invitation_to_reject = Invitation.objects.get(id=accept.cleaned_data['id'])236 invitation_to_reject.delete()237 messages.success(request, "Request successfully rejected")238 return HttpResponseRedirect('/invitations')239 except: 240 pass241 return render(request, self.template_name, {'accept': accept, 'reject': reject})242def about_view(request):243 content = { }244 title = "about"...
page_context.py
Source:page_context.py
1from __future__ import annotations2import dataclasses3import datetime4import typing as typ5import django.core.handlers.wsgi as dj_wsgi6import django.core.paginator as dj_page7import django.template.context as dj_context8from . import models, settings, forms, skins9TemplateContext = dj_context.RequestContext10@dataclasses.dataclass11class PageContext:12 request: dj_wsgi.WSGIRequest13 project_name: str14 main_page_namespace: settings.Namespace15 main_page_title: str16 main_page_title_url: str17 main_page_full_title: str18 main_page_full_title_url: str19 page: models.Page20 page_namespace_gender: typ.Optional[models.Gender]21 suppages: typ.List[models.Page]22 mode: str23 noindex: bool24 show_title: bool25 user: models.User26 skin: skins.Skin27 default_language: settings.i18n.Language28 language: settings.i18n.Language29 content_language: settings.i18n.Language30 languages: typ.List[settings.i18n.Language]31 page_exists: bool32 user_can_read: bool33 user_can_edit: bool34 user_can_hide: bool35 date_time: datetime.datetime36 user_date_time: datetime.datetime37 date: datetime.date38 user_date: datetime.date39 time: datetime.time40 user_time: datetime.time41 revisions_list_page_min: int42 revisions_list_page_max: int43 rc_min_days: int44 rc_max_days: int45 rc_min_revisions: int46 rc_max_revisions: int47 def __post_init__(self):48 self._context = None49 def __getattr__(self, item):50 if self._context:51 return getattr(self._context, item)52 raise AttributeError(item)53@dataclasses.dataclass(init=False)54class RedirectPageContext(PageContext):55 redirect: str56 redirect_anchor: typ.Optional[str]57 is_path: bool58 display_redirect: bool59 def __init__(60 self,61 context: PageContext,62 /,63 to: str,64 anchor: str = None,65 is_path: bool = False,66 display: bool = False67 ):68 self._context = context69 self.redirect = to70 self.redirect_anchor = anchor71 self.is_path = is_path72 self.display_redirect = display73@dataclasses.dataclass(init=False)74class RevisionPageContext(PageContext):75 wikicode: str76 revision: typ.Optional[models.PageRevision]77 archived: bool78 def __init__(79 self,80 context: PageContext,81 /,82 wikicode: str,83 revision: typ.Any = None,84 archived: bool = False85 ):86 self._context = context87 self.wikicode = wikicode88 self.revision = revision89 self.archived = archived90@dataclasses.dataclass(init=False)91class ReadPageContext(RevisionPageContext):92 rendered_page_content: str93 is_redirection: bool94 redirected_from: typ.Optional[typ.Tuple[int, str]]95 page_categories: typ.List[typ.Tuple[models.Page, models.CategoryData]]96 def __init__(97 self,98 context: PageContext,99 /,100 wikicode: str = None,101 is_redirection: bool = None,102 revision: typ.Optional[object] = None,103 archived: bool = False,104 rendered_page_content: str = '',105 redirected_from: typ.Tuple[int, str] = None,106 page_categories: typ.List[typ.Tuple[models.Page, models.CategoryData]] = None107 ):108 super().__init__(context, wikicode=wikicode, revision=revision, archived=archived)109 self.rendered_page_content = rendered_page_content110 self.is_redirection = is_redirection111 self.redirected_from = redirected_from112 self.page_categories = page_categories113@dataclasses.dataclass(init=False)114class TalkPageContext(PageContext):115 user_can_edit_talk: bool116 rendered_page_content: typ.Optional[str]117 is_redirection: bool118 redirected_from: typ.Optional[typ.Tuple[int, str]]119 edit_protection_status: typ.Optional[models.PageProtectionStatus]120 edit_protection_log_entry: typ.Optional[models.PageProtectionLogEntry]121 new_topic_form: forms.NewTopicForm122 new_topic_form_global_errors: typ.List[str]123 edit_message_form: forms.EditMessageForm124 edit_message_form_global_errors: typ.List[str]125 def __init__(126 self,127 context: PageContext,128 /,129 can_edit_talk: bool,130 new_topic_form: forms.NewTopicForm,131 edit_message_form: forms.EditMessageForm,132 new_topic_form_global_errors: typ.List[str] = None,133 edit_message_form_global_errors: typ.List[str] = None,134 rendered_page_content: str = None,135 is_redirection: bool = None,136 redirected_from: typ.Tuple[int, str] = None,137 protection_status: models.PageProtectionStatus = None,138 protection_log_entry: models.PageProtectionLogEntry = None139 ):140 self._context = context141 self.user_can_edit_talk = can_edit_talk142 self.new_topic_form = new_topic_form143 self.edit_message_form = edit_message_form144 self.new_topic_form_global_errors = new_topic_form_global_errors145 self.edit_message_form_global_errors = edit_message_form_global_errors146 self.rendered_page_content = rendered_page_content147 self.is_redirection = is_redirection148 self.redirected_from = redirected_from149 self.edit_protection_status = protection_status150 self.edit_protection_log_entry = protection_log_entry151@dataclasses.dataclass(init=False)152class EditPageContext(RevisionPageContext):153 edit_notice: str154 error_notice: str155 edit_form: forms.EditPageForm156 edit_show_interface_warning: bool157 edit_protection_status: typ.Optional[models.PageProtectionStatus]158 edit_protection_log_entry: typ.Optional[models.PageProtectionLogEntry]159 def __init__(160 self,161 context: PageContext,162 /,163 wikicode: str,164 form: forms.EditPageForm,165 revision: typ.Optional[object] = None,166 archived: bool = False,167 edit_notice: str = '',168 error_notice: str = '',169 show_interface_warning: bool = False,170 protection_status: models.PageProtectionStatus = None,171 protection_log_entry: models.PageProtectionLogEntry = None172 ):173 super().__init__(context, wikicode=wikicode, revision=revision, archived=archived)174 self.edit_form = form175 self.edit_notice = edit_notice176 self.error_notice = error_notice177 self.edit_show_interface_warning = show_interface_warning178 self.edit_protection_status = protection_status179 self.edit_protection_log_entry = protection_log_entry180@dataclasses.dataclass(init=False)181class ListPageContext(PageContext):182 paginator: dj_page.Paginator183 paginator_page: int184 def __init__(self, context: PageContext, /, paginator: dj_page.Paginator, page: int):185 self._context = context186 self.paginator = paginator187 self.paginator_page = page188@dataclasses.dataclass(init=False)189class CategoryPageContext(ListPageContext):190 subcategories: typ.Sequence[typ.Tuple[models.Page, str]]191 def __init__(192 self,193 context: PageContext,194 /,195 paginator: dj_page.Paginator,196 page: int,197 subcategories: typ.Sequence[typ.Tuple[models.Page, str]]198 ):199 super().__init__(context, paginator, page)200 self.subcategories = subcategories201@dataclasses.dataclass(init=False)202class SetupPageContext(PageContext):203 setup_form: forms.SetupPageForm204 setup_form_global_errors: typ.List[str]205 def __init__(self, context: PageContext, /, form: forms.SetupPageForm, global_errors: typ.List[str] = None):206 self._context = context207 self.setup_form = form...
hackernews.py
Source:hackernews.py
1from bottle import (2 route, run, template, request, redirect, static_file3)4from scraputils import get_news5from db import News, session, engine6from bayes import NaiveBayesClassifier7import sqlalchemy8@route('/')9def root():10 handle_redirect(request.query.redirected_from)11@route('/all')12def all_news():13 s = session()14 count = count_news(s)15 rows = s.query(News).all()16 if rows == []:17 return template('./templates/news_empty', count=count)18 return template('./templates/news_all', rows=rows, count=count)19@route("/unlabeled")20def unlabeled_news():21 s = session()22 count = count_news(s)23 rows = s.query(News).filter(News.label == None).all()24 # Show 'empty template' if no news without a label is found25 if rows == []:26 return template('./templates/news_empty', count=count)27 return template('./templates/news_unlabeled', rows=rows, count=count)28@route("/upvoted")29def upvoted_news():30 s = session()31 count = count_news(s)32 rows = s.query(News).filter(News.label == 'upvote').all()33 # Show 'empty template' if no news without a label is foundsssss34 if rows == []:35 return template('./templates/news_empty', count=count)36 return template('./templates/news_upvoted', rows=rows, count=count)37@route("/maybe")38def maybe_news():39 s = session()40 count = count_news(s)41 rows = s.query(News).filter(News.label == 'maybe').all()42 # Show 'empty template' if no news without a label is foundsssss43 if rows == []:44 return template('./templates/news_empty', count=count)45 return template('./templates/news_maybe', rows=rows, count=count)46@route("/downvoted")47def downvoted_news():48 s = session()49 count = count_news(s)50 rows = s.query(News).filter(News.label == 'downvote').all()51 # Show 'empty template' if no news without a label is foundssssssssss52 if rows == []:53 return template('./templates/news_empty', count=count)54 return template('./templates/news_downvoted', rows=rows, count=count)55@route("/add_label/")56def add_label():57 s = session()58 # Find News row in db by id59 match = s.query(News).filter_by(id=request.query.id)60 if match:61 # Set label to News row from GET-request62 match[0].label = request.query.label63 s.commit()64 handle_redirect(request.query.redirected_from)65@route("/remove_label/")66def remove_label():67 s = session()68 # Find News row in db by id69 match = s.query(News).filter_by(id=request.query.id)70 if match:71 match[0].label = None72 s.commit()73 handle_redirect(request.query.redirected_from)74def handle_redirect(redirected_from):75 # Redirect page back76 if redirected_from == 'all':77 redirect("/all")78 elif redirected_from == 'upvoted':79 redirect("/upvoted")80 elif redirected_from == 'maybe':81 redirect("/maybe")82 elif redirected_from == 'downvoted':83 redirect("/downvoted")84 else:85 redirect("/unlabeled")86@route("/update")87def update_news():88 news_count = 089 page = 190 s = session()91 # Add more news until 30 new news is added92 while news_count < 30:93 news_list = get_news('https://news.ycombinator.com/newest', page)94 for news in news_list:95 # Check if news is already in db by its title and author96 if (not s.query(News).filter(News.title == news.title).filter(News.author == news.author).all() and97 news_count < 30):98 s.add(news)99 news_count += 1100 page += 1101 s.commit()102 redirect("/unlabeled")103@route("/drop")104def drop_table():105 s = session()106 s.query(News).delete()107 s.commit()108 redirect("/unlabeled")109def count_news(s) -> dict:110 """ Counts news in database111 s: sqlalchemy.session112 """113 count = {114 'all': s.query(News).count(),115 'unlabeled': s.query(News).filter(News.label == None).count(),116 'upvoted': s.query(News).filter(News.label == 'upvote').count(),117 'maybe': s.query(News).filter(News.label == 'maybe').count(),118 'downvoted': s.query(News).filter(News.label == 'downvote').count(),119 }120 return count121@route("/classify")122def classify_news(train_titles, train_labels, test_titles):123 bayers = NaiveBayesClassifier()124 bayers.fit(train_titles, train_labels)125 predictions = bayers.predict(test_titles)126 return predictions127 # bayers.score(predictions, labels)128@route('/recommendations')129def recommendations():130 s = session()131 count = count_news(s)132 # Data, used to fit the classificator133 train_titles = [title[0] for title in s.query(134 News.title).filter(News.label != None).all()]135 train_labels = [label[0] for label in s.query(136 News.label).filter(News.label != None).all()]137 # Data, label of which to be predicted138 test = s.query(News).filter(News.label == None).all()139 test_titles = [row.title for row in test]140 test_ids = [row.id for row in test]141 classified_news = classify_news(train_titles, train_labels, test_titles)142 for index, news in enumerate(classified_news):143 news.update({'id': test_ids[index]})144 # Sort by float element in dict (tuple)145 # ref: https://www.geeksforgeeks.org/python-sort-tuple-float-element/146 classified_news = sorted(147 classified_news, key=lambda x: float(x['prob_sum']), reverse=True)148 result_ids = []149 for news in classified_news:150 # if news['prob_sum'] > -10:151 if news['label'] == 'upvote':152 result_ids.append(news['id'])153 result_rows = s.query(News).filter(News.id.in_(result_ids)).all()154 return template('./templates/news_recommendations', rows=result_rows, count=count)155# For improting css and javascript files into templates156# Ref: https://stackoverflow.com/questions/6978603/how-to-load-a-javascript-or-css-file-into-a-bottlepy-template157@route('/static/:path#.+#', name='static')158def static(path):159 return static_file(path, root='static')160if __name__ == "__main__":161 run(host="localhost", port=8095, reloader=True)...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!