Exemple #1
0
def signup(request):
    signup_methods = Signup()
    error_message = ''
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            ip_address = get_client_ip(request)
            if signup_methods.email_exist(user):
                error_message = f'A user with email {user.email} already exists'
                logger.info(
                    f'user {user.username} (ip: {ip_address}) tries to use '
                    f'existing email: {user.email}')
                form = SignUpForm()

            else:
                user.save()
                login(request, user)
                signup_methods.send_welcome_email(user)
                logger.info(
                    f'user {user.username} (ip: {ip_address}) '
                    f'has succesfully signed up, email sent to {user.email}')
                return redirect('home')

    else:
        form = SignUpForm()

    return render(request, 'accounts/signup.html', {
        'form': form,
        'error_message': error_message
    })
Exemple #2
0
def mynewsitems(request):
    ''' views function to render mynewsitems.html
    '''
    ip_address = get_client_ip(request)
    logger.info(
        f'user {request.user}, watching personal newsitems, ip: {ip_address}')

    deleted_item_pk = request.POST.get('deleted_item_pk')
    if deleted_item_pk:
        get_object_or_404(UserNewsItem, pk=deleted_item_pk).delete()

    button_site = request.POST.get('site_btn')
    if button_site:
        ns = NewsStatus(current_news_site=button_site,
                        item=0,
                        news_site='',
                        updated='',
                        news_items=0,
                        banner=0,
                        scroll=0,
                        error_message='')
        set_session_newsstatus(request, ns)
        return redirect(reverse('newspage'))

    # order reversed by publishing date
    context = {
        'newsitems':
        UserNewsItem.objects.filter(user=request.user).order_by('-published')
    }
    return render(request, 'newsfeed/mynewsitems.html', context)
Exemple #3
0
    def get_queryset(self):
        self.board = get_object_or_404(Board, pk=self.kwargs.get('board_pk'))
        queryset = self.board.topics.order_by('-last_updated', )\
                                    .annotate(contributions=Count('posts'))

        log_record(self.request.user, 'showing topics for board ',
                   self.board.name, get_client_ip(self.request))

        return queryset
Exemple #4
0
    def get_queryset(self):
        self.topic = get_object_or_404(Topic,
                                       board__pk=self.kwargs.get('board_pk'),
                                       pk=self.kwargs.get('topic_pk'))

        queryset = self.topic.posts.order_by('-updated_at')

        log_record(self.request.user, 'showing posts for topic ',
                   self.topic.topic_subject, get_client_ip(self.request))

        return queryset
Exemple #5
0
 def get_context_data(self, **kwargs):  #pylint: disable=arguments-differ
     log_record(self.request.user, 'showing boards', '',
                get_client_ip(self.request))
     return super().get_context_data(**kwargs)
Exemple #6
0
def newspage(request):
    ''' views function to render newspage.html
    '''
    user = request.user
    news_sites, ns = obtain_news_sites_and_news_status_for_user(request, user)
    if news_sites == []:
        return redirect(reverse('newssites'))

    button_cntr = request.POST.get('control_btn')
    button_site = request.POST.get('site_btn')
    button_title = request.POST.get('title_btn')
    if not button_cntr or button_cntr == cntr.next:
        ns.item += 1
    elif button_cntr == cntr.previous:
        ns.item -= 1
    elif button_cntr == cntr.banner:
        # true/ false for javascript
        ns.banner = 0 if ns.banner else 1
    elif button_cntr == cntr.scroll:
        # true/ false for javascript
        ns.scroll = 0 if ns.scroll else 1
    elif button_cntr == cntr.store:
        pass
    else:
        logger.warning(f'ValueError {button_cntr}: check template')

    if ns.news_items != 0:
        ns.item = ns.item % ns.news_items
    else:
        ns.item = 0

    if button_site:
        ns.current_news_site = button_site

    if button_title and button_title != 'refresh':
        ns.item = int(button_title)

    today = datetime.date.today().strftime("%d-%m-%Y")
    update_news_true = (ns.current_news_site != ns.news_site) or \
                       (ns.item == 0 and not (button_cntr or button_title)) or \
                       (ns.updated != today) or \
                       (button_title == 'refresh')
    if update_news_true:
        feed_items = update_news(NewsSite.objects.get(
            news_site=ns.current_news_site).news_url)
        request.session['feed'] = feed_items
        ns.news_site = ns.current_news_site
        ns.updated = today
        ns.item = 0

    else:
        feed_items = request.session['feed']

    # restore feed items to FeedParserDict - for some reason Django sessions
    # converts them to Dict; sort by date
    feed_items = restore_sort_feedparserdict(feed_items)

    # test if newsfeed is not empty, if it is return to defauilt newssite
    # and reset session
    ns.news_items = len(feed_items)
    if ns.news_items == 0:
        default_site = str(UserNewsSite.objects.get(
            user__username='******').news_sites.first())
        ns.error_message = f'Newssite {ns.current_news_site} is not available, '\
                           f'revert to default site {default_site}'
        logger.warning(f'{ns.current_news_site} is not available, revert to default site')
        ns.current_news_site = default_site
        set_session_newsstatus(request, ns)
        return redirect(reverse('newspage'))

    # if button was pressed to store the news item then this is done here
    ip_address = get_client_ip(request)
    if button_cntr == cntr.store and user.is_authenticated:
        store_news_item(user, ns, feed_items, ip_address)

    context = create_news_context(ns, news_sites, feed_items)

    # store status session for next time newsfeed is called and remove
    # the error message
    ns.error_message = ''
    set_session_newsstatus(request, ns)

    context['controls'] = cntr

    logger.info(f'user {user}, browsing news: {ns.current_news_site}, ip: {ip_address}')
    return render(request, 'newsfeed/newspage.html', context)