Exemplo n.º 1
0
 def dispatch(self, request, *args, **kwargs):
     try:
         slug = self.kwargs.get('slug', '')
         module_logger.debug(
             f'diagnostic dispatcher attempting to use slug: {slug}')
         app_name = self.kwargs.get('app_name', '')
         module_logger.debug(
             f'diagnostic dispatcher attempting to use app_name: {app_name}'
         )
         if slug_re.match(slug) and slug_re.match(app_name):
             registry_key = slugify(f'{app_name} {slug}',
                                    allow_unicode=True)
             module_logger.debug(
                 f'retrieving entry with registry key: {registry_key}')
             entry = Diagnostic.registry[registry_key]
             module_name = entry['module']
             function_name = entry['name']
             my_module = importlib.import_module(module_name)
             my_klass = getattr(my_module, function_name)
             return my_klass.as_view()(request, *args, **kwargs)
         else:
             return HttpResponseRedirect(reverse('django_diagnostic:index'))
     except KeyError:
         return HttpResponseRedirect(reverse('django_diagnostic:index'))
     except Exception as e:
         module_logger.error(
             f'Rendering diagnostic page resulted in error: {str(e)}')
         return HttpResponseRedirect(reverse('django_diagnostic:index'))
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        # get username and password
        username = request.data.get("username")
        password = request.data.get("password")

        # set email
        email = username + "@p2photo.com"

        if not username or len(username) > 30 or not slug_re.match(username):
            return Response(
                {"error": "Username is too big or has invalid characters."},
                status=status.HTTP_400_BAD_REQUEST)
        elif not password or len(password) > 30:
            return Response({"error": "Password is invalid."},
                            status=status.HTTP_400_BAD_REQUEST)

        try:
            user = User.objects.create_user(username=username,
                                            password=password,
                                            email=email)
        except IntegrityError:
            return Response({"error": "Username is already in use."},
                            status=status.HTTP_409_CONFLICT)
        except Exception as e:
            return Response({"error": str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        return Response(status=status.HTTP_201_CREATED)
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        # get catalog URL
        name = request.data.get("name")

        if not slug_re.match(name):
            return Response({'error': 'Album Name is invalid.'},
                            status=status.HTTP_400_BAD_REQUEST)

        user = request.user
        if isinstance(user, AnonymousUser):
            return Response({'error': 'User is invalid.'},
                            status=status.HTTP_401_UNAUTHORIZED)

        # save album and membership on database
        try:
            album = Album.objects.create(name=name)
            membership = Membership.objects.create(album=album,
                                                   user=user,
                                                   catalog="0",
                                                   key=generate_key())

            # open and write url to album catalog file
            open("catalogs/catalog_" + name, 'a').close()

        except IntegrityError:
            return Response({"error": "Album name is already in use."},
                            status=status.HTTP_409_CONFLICT)
        except Exception as e:
            return Response({"error": str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        return Response(status=status.HTTP_201_CREATED)
Exemplo n.º 4
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        registry_dict = dict()
        # for key, value in sorted(Diagnostic.registry.items(),
        #                          key=lambda item: item[1].get('kwargs', dict()).get('app_name', '')):
        for key, value in sorted(Diagnostic.registry.items()):
            try:
                entry = dict()
                module_name = value['module']
                function_name = value['name']
                my_module = importlib.import_module(module_name)
                my_klass = getattr(my_module, function_name)
                app_name = my_module.__package__
                entry['doc'] = my_klass.__doc__

                slug_value = value['slug']
                slug = slugify(slug_value, allow_unicode=True)
                if slug_re.match(slug):
                    entry['slug'] = slug

                app_name_slug = slugify(app_name)
                if slug_re.match(app_name_slug):
                    entry['app_name'] = app_name_slug

                if 'app_name' in entry and 'slug' in entry:
                    module_logger.debug(
                        f'Adding diagnostic to index: {app_name} {slug}')

                    entry['link_name'] = value['kwargs']['link_name']
                    registry_key = slugify(f'{app_name} {slug_value}',
                                           allow_unicode=True)

                    if registry_key not in registry_dict:
                        registry_dict[registry_key] = entry

            except Exception:
                pass

        context['registry'] = registry_dict
        return context
Exemplo n.º 5
0
 def validate(self, data):
     slug = data['slug']
     name = data['name']
     pk = -1 if self.instance is None else self.instance.pk
     if not slug:
         logger.debug("slugifying name %s", name)
         slug = text.slugify(name)
     # check if slug exists in project table already
     if not slug_re.match(slug):
         raise serializers.ValidationError("Please enter a valid short name (no spaces) or leave blank to autogenerate one.")
     elif Project.objects.filter(slug=slug).exclude(pk=pk).exists():
         raise serializers.ValidationError("Sorry, this short name has already been taken. Please choose another.")
     else:
         data.update(slug=slug)
     return data
Exemplo n.º 6
0
def create_room(request):
    parsed = json.loads(request.body)
    if not parsed['room_name']:
        return JsonResponse({'error': 'Please fill out room name'})
    if not slug_re.match(parsed['room_name']):
        return JsonResponse({'error': 'Please input a room name that is url safe(no spaces or special characters)'})
    if not parsed['template']:
        return JsonResponse({'error': 'Please pick a template'})
    room_name = parsed['room_name']
    if GameRoom.objects.filter(name=room_name).exists():
        return JsonResponse({'error': 'A room with that name already exists.'})
    template = BoardTemplate.objects.filter(name=parsed['template'])
    if template.exists():
        new = GameRoom(name=room_name, admin=request.user, base_template=template[0], board_state=template[0].board_state, board_width=template[0].board_width, board_height=template[0].board_height)
        new.save()
        new.whitelist.add(request.user)
        resp = {'redirect': reverse('tabletops:room', args=(new.name,))}
        return JsonResponse(resp)
    return JsonResponse({'error': 'Template with that name does not exist.'})
Exemplo n.º 7
0
    def clean(self, value):
        subdomain = value.lower()
        if not slug_re.match(subdomain):
            raise forms.ValidationError(
                'Invalid subdomain. Please only use the following characters: '
                'a-z, 0-9, -, _.')

        try:
            Website.objects.get(subdomain=subdomain)
            msg = 'The domain %s.%s has already been taken. Try another one.' \
                  % (subdomain, settings.DEFAULT_SITE_DOMAIN)
            raise forms.ValidationError(msg)
        except Website.DoesNotExist:
            pass
        try:
            User.objects.get(username=subdomain)
            msg = 'The domain %s.%s has already been taken. Try another one.' \
                  % (subdomain, settings.DEFAULT_SITE_DOMAIN)
            raise forms.ValidationError(msg)
        except User.DoesNotExist:
            pass
        return subdomain
Exemplo n.º 8
0
        def decorator(fn):
            module_logger.debug(f'args: {args}')
            module_logger.debug(f'kwargs: {kwargs}')
            module_logger.debug(
                f'registry dict currently has {len(cls.registry.keys())}')
            registration = dict()
            registration['name'] = fn.__name__
            registration['module'] = fn.__module__
            try:
                app_name = fn.__module__.split('.')[0]
                registration['app_name'] = app_name
            except:
                pass
            registration['args'] = args
            registration['kwargs'] = kwargs
            slug = kwargs['slug']

            if slug_re.match(slug):
                module_logger.debug(f'slug matched ok: {slug}')
                module_logger.debug(
                    f'Storing dict in registry: {registration}')
                registration['slug'] = slugify(slug, allow_unicode=True)

                registry_key = slugify(f'{app_name} {slug}',
                                       allow_unicode=True)
                if registry_key and registry_key not in cls.registry:
                    cls.registry[registry_key] = registration
                module_logger.debug(f'registry key is {registry_key}')
                module_logger.debug(
                    f'registry dict now has {len(cls.registry.keys())} items')
            else:
                module_logger.debug(
                    f'unable to register diagnostic registry key: {regsitry_key}'
                )

            return fn
Exemplo n.º 9
0
 def validate_deckSlug(self, value):
     if value == 'undefined':
         raise ValidationError("Shorter than minimum length 1.")
     if not slug_re.match(value):
         raise ValidationError("Enter a valid 'slug' ")
Exemplo n.º 10
0
    def handle(self, *args, **options):
        """ 
        Process: We need to change the blogs landing page from a Components Page here to a Landing Page type

        Landing pages: have a different layout better catered for with a separate page type
        """

        # its title here it 'Blogs' its slug is 'blogs' and it's a component page type
        # there should only be one...
        blog_landing_page = ComponentsPage.objects.get(
            wp_template='page-blog-landing.php')
        # first create the new Components Pages

        # make a new page and place it under the same parent page
        first_published_at = blog_landing_page.first_published_at
        last_published_at = blog_landing_page.last_published_at
        latest_revision_created_at = blog_landing_page.latest_revision_created_at

        slug = URLParser(blog_landing_page.wp_link).find_slug()

        # sometimes there's external links with params so fall back to the slug fomr wordpress
        if not slug_re.match(slug):
            slug = blog_landing_page.slug

        # the body field is left blank for now
        obj = LandingPage(
            title=blog_landing_page.title,
            slug=self.unique_slug(slug),
            excerpt=strip_tags(blog_landing_page.excerpt),
            # raw_content=blog_landing_page.content,
            show_in_menus=True,
            author=blog_landing_page.author,
            md_owner=blog_landing_page.md_owner,
            md_description=blog_landing_page.md_description,
            md_gateway_ref=blog_landing_page.md_gateway_ref,
            md_pcc_reference=blog_landing_page.md_pcc_reference,
            # start wordpress fields we can delete later
            wp_id=blog_landing_page.wp_id,
            parent=blog_landing_page.parent,
            source=blog_landing_page.source,
            wp_template=blog_landing_page.wp_template,
            wp_slug=blog_landing_page.wp_slug,
            real_parent=blog_landing_page.real_parent,
            wp_link=blog_landing_page.wp_link,
            model_fields=blog_landing_page.model_fields,
            content_fields=blog_landing_page.content_fields,
            content_field_blocks=blog_landing_page.content_field_blocks,
            component_fields=blog_landing_page.component_fields,
        )
        blog_landing_page.get_parent().add_child(instance=obj)

        rev = obj.save_revision()  # this needs to run here

        obj.first_published_at = first_published_at
        obj.last_published_at = last_published_at
        obj.latest_revision_created_at = latest_revision_created_at

        obj.save()
        rev.publish()

        blogs_page = LandingPage.objects.get(
            wp_template='page-blog-landing.php')
        print('Moving all blog posts to new parent page, Takes a while...')

        # find base page with that wp_id and source so we can move it's children
        old_blog_index_base_page = BlogIndexPage.objects.get(slug='blog')
        # blog_pages = old_blog_index_base_page.get_children()

        # for blog in blog_pages:
        old_blog_index_base_page.move(blogs_page, pos='last-child')

        # delete the old blog-items-index now dont need it
        blog_landing_page.delete()

        # rename the slug for the new blogs page now we deleted the old one
        blogs_page.slug = 'blogs'
        rev = blogs_page.save_revision()
        blogs_page.save()
        rev.publish()
        sys.stdout.write('\n✅  Blogs Page Now Set Up\n')
Exemplo n.º 11
0
def is_valid_slug(slug):
    """Uses Django's slug regex to test if id is valid"""
    return slug_re.match(slug)
Exemplo n.º 12
0
    def handle(self, *args, **options):
        """ 
        Process: We need to change some pages from a base page to another page type
        based on it's page type from wordpress

        Component pages: have a different layout better catered for with a separate page type
        """

        base_pages = BasePage.objects.filter(wp_template='page-components.php') | BasePage.objects.filter(wp_template='page-blog-landing.php')
        # first create the new Components Pages

        for page in base_pages:


            # print(page)
            # make a new page and place it under the same parent page
            first_published_at = page.first_published_at
            last_published_at = page.last_published_at
            latest_revision_created_at = page.latest_revision_created_at
            # print('{}'.format(page.title))

            slug = URLParser(page.wp_link).find_slug()
            # sometimes there's external links with params so fall back to the slug fomr wordpress
            if not slug_re.match(slug): 
                slug = page.slug

            # the body field is left blank for now
            obj = ComponentsPage(
                title=page.title,
                slug=self.unique_slug(slug),
                excerpt=strip_tags(page.excerpt),
                # raw_content=page.content,
                show_in_menus=True,
                author=page.author,
                md_owner=page.md_owner,
                md_description=page.md_description,
                md_gateway_ref=page.md_gateway_ref,
                md_pcc_reference=page.md_pcc_reference,
                # start wordpress fields we can delete later
                wp_id=page.wp_id,
                parent=page.parent,
                source=page.source,
                wp_template=page.wp_template,
                wp_slug=page.wp_slug,
                real_parent=page.real_parent,
                wp_link=page.wp_link,
                model_fields=page.model_fields,
                content_fields=page.content_fields,
                content_field_blocks=page.content_field_blocks,
                component_fields=page.component_fields,
            )

            page.get_parent().add_child(instance=obj)

            rev = obj.save_revision()  # this needs to run here

            obj.first_published_at = first_published_at
            obj.last_published_at = last_published_at
            obj.latest_revision_created_at = latest_revision_created_at

            obj.save()
            rev.publish()
        
        components_pages = ComponentsPage.objects.all()

        for component_page in components_pages:
            # print(component_page.id)
            wp_id = component_page.wp_id
            source = component_page.source
            # find base page with that wp_id and source so we can move it's children
            old_base_page = BasePage.objects.get(source=source, wp_id=wp_id)
            children = old_base_page.get_children()

            for child in children:
                child.move(component_page, pos='last-child')
            old_base_page.delete() 
Exemplo n.º 13
0
    def parse_results(self):

        home_page = Page.objects.filter(title='Home')[0]
        pages = self.results  # this is json result set

        for page in pages:

            first_published_at = page.get('date')
            last_published_at = page.get('modified')
            latest_revision_created_at = page.get('modified')
            """ 
            Process: We need to import the pages at the top level under the home page as we don't know the 
            page sitemap structure until all pages have been imported.
            
            Problem: using the wordpress slugs here means wagtail wrangles them to be unique at the top level
            
            Solution: we need to be able to fix these slugs later on still run into slugs we are again
            duplicating so lets set our own unique slug here so we can change back later without
            issue.
            """
            # these are fields that are meta data to be saved
            model_fields = {
                'owner': '',
                'description': '',
                'gateway_ref': '',
                'pcc_reference': ''
            }
            for item in page.get('model_fields'):
                for k, v in item.items():
                    model_fields[k] = v

            slug = URLParser(page.get('link')).find_slug()
            # sometimes there's external links with params so fall back to the slug fomr wordpress
            if not slug_re.match(slug):
                slug = page.get('slug')

            obj = BasePage(
                title=page.get('title'),
                slug=self.unique_slug(slug),
                excerpt=strip_tags(page.get('excerpt')),
                raw_content=page.get('content'),
                show_in_menus=True,
                author=page.get('author'),
                md_owner=model_fields['owner'],
                md_description=model_fields['description'],
                md_gateway_ref=model_fields['gateway_ref'],
                md_pcc_reference=model_fields['pcc_reference'],
                # start wordpress fields we can delete later
                wp_id=page.get('wp_id'),
                parent=page.get('parent'),
                source=page.get('source'),
                wp_template=page.get('template'),
                wp_slug=page.get('slug'),
                real_parent=page.get('real_parent') or 0,
                wp_link=page.get('link'),
                model_fields=page.get('model_fields'),
                content_fields=page.get('content_fields'),
                content_field_blocks=page.get('content_fields_blocks'),
                component_fields=page.get('component_fields'),
            )

            home_page.add_child(instance=obj)
            rev = obj.save_revision()  # this needs to run here

            obj.first_published_at = first_published_at
            obj.last_published_at = last_published_at
            obj.latest_revision_created_at = latest_revision_created_at
            # probably not the best way to do this but need to update the dates on the page record.
            obj.save()
            rev.publish()
            sys.stdout.write('.')

        if self.next:
            time.sleep(self.sleep_between_fetches)
            self.fetch_url(self.next)
            self.parse_results()
        return BasePage.objects.live().descendant_of(
            home_page).count(), self.count
Exemplo n.º 14
0
 def slug(self):
     _ls = self.lexeme.strip()
     slug = slugify(_ls)
     if slug_re.match(slug):
         return slug
     return 'NA'