<commit_before>from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.detail import DetailView
from django.http.response import HttpResponseRedirect
from profiles.models import UserProfile
from django.contrib.auth.models import User
from feeds.forms import FeedForm
import feedparser
from feeds.models import Feed
from django.views.generic.edit import FormView
from django.core.exceptions import ObjectDoesNotExist


class HomeView(TemplateView):
    template_name = 'home.html'


class NoEntriesView(TemplateView):
    template_name = 'noentries.html'


class NextView(RedirectView):
    permanent = False

    def get_redirect_url(self, **kwargs):
        subscription = self.request.GET['subscription']
        profile = UserProfile.objects.get(next_slug=subscription)
        entries = profile.entries.filter(userentrydetail__read=False)
        if not entries.exists():
            return '/noentries'
        entry = entries.order_by('-published')[0]
        user_entry = entry.userentrydetail_set.get(profile=profile)
        user_entry.read = True
        user_entry.save()
        return entry.link


class DashboardView(FormView):
    template_name = 'dashboard.html'
    form_class = FeedForm
    success_url = '/dashboard'

    def form_valid(self, form):
        user = self.request.user
        link = form.cleaned_data['link']
        feed = feedparser.parse(link).feed
        title = feed.title
        try:
            feed_obj = Feed.objects.get(link=link)
        except ObjectDoesNotExist:
            feed_obj = Feed(link=link, title=title)
            feed_obj.save()
        user.get_profile().feeds.add(feed_obj)
        return super(DashboardView, self).form_valid(form)

    def get_context_data(self, *args, **kwargs):
        user = self.request.user
        try:
            user.get_profile()
        except:
            UserProfile(user=user).save()
        return super(DashboardView, self).get_context_data(*args, **kwargs)
<commit_msg>When adding a new feed, add entries to current user.<commit_after>from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.detail import DetailView
from django.http.response import HttpResponseRedirect
from profiles.models import UserProfile, UserEntryDetail
from django.contrib.auth.models import User
from feeds.forms import FeedForm
import feedparser
from feeds.models import Feed, Entry
from django.views.generic.edit import FormView
from django.core.exceptions import ObjectDoesNotExist
import time


class HomeView(TemplateView):
    template_name = 'home.html'


class NoEntriesView(TemplateView):
    template_name = 'noentries.html'


class NextView(RedirectView):
    permanent = False

    def get_redirect_url(self, **kwargs):
        subscription = self.request.GET['subscription']
        profile = UserProfile.objects.get(next_slug=subscription)
        entries = profile.entries.filter(userentrydetail__read=False)
        if not entries.exists():
            return '/noentries'
        entry = entries.order_by('-published')[0]
        user_entry = entry.userentrydetail_set.get(profile=profile)
        user_entry.read = True
        user_entry.save()
        return entry.link


class DashboardView(FormView):
    template_name = 'dashboard.html'
    form_class = FeedForm
    success_url = '/dashboard'

    def form_valid(self, form):

        # Add feed
        user = self.request.user
        link = form.cleaned_data['link']
        parser = feedparser.parse(link)
        feed = parser.feed
        title = feed.title
        try:
            feed_obj = Feed.objects.get(link=link)
        except ObjectDoesNotExist:
            feed_obj = Feed(link=link, title=title)
            feed_obj.save()
        user.get_profile().feeds.add(feed_obj)

        # Add entries from feed
        entries = parser.entries
        for entry in entries:
            published = time.strftime('%Y-%m-%d %H:%M', entry.published_parsed)
            entry_obj = Entry(feed=feed_obj,
                              title=entry.title,
                              link=entry.link,
                              published=published)
            entry_obj.save()
            UserEntryDetail(entry=entry_obj,
                            profile=user.get_profile(),
                            read=False).save()

        return super(DashboardView, self).form_valid(form)

    def get_context_data(self, *args, **kwargs):
        user = self.request.user
        try:
            user.get_profile()
        except:
            UserProfile(user=user).save()
        return super(DashboardView, self).get_context_data(*args, **kwargs)

{'fraction_non_alphanumeric': 0.09193927731451786, 'fraction_numerical': 0.00042762454564892024, 'mean_word_length': 2.715647339158062, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4778194', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1291, 'n_words': 280}