示例#1
0
文件: views.py 项目: jordezz/Webfront
    def get_context_data(self, **kwargs):
        context = super(LandingPage, self).get_context_data(**kwargs)

        tweets = cache.get('latest_tweets')

        if tweets:
            context['latest_tweets'] = tweets

        else:
            try:
                tweets = settings.TWITTER_AGENT.statuses.user_timeline()[:6]
            except URLError:
                tweets = []
            for tweet in tweets:
                tweet['date'] = pytz.utc.localize(datetime.datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S +0000 %Y"))

            cache.set('latest_tweets', tweets, settings.TWITTER_CACHE_TIMEOUT)

            context['latest_tweets'] = tweets

        if self.request.user.is_authenticated():
            if self.request.user.is_superuser:
                latest_entries = Entry.published.all()
            else:
                user_dorm = UserRoom.get_current_room(self.request.user)
                # if they're off campus, make sure the right official dorm is used:
                if not user_dorm.room.dorm.official_dorm:
                    user_dorm = UserRoom.objects.get(room__dorm__code="OFF", room__number="Symbolic Room", user=self.request.user)

                if user_dorm:
                    latest_entries = Entry.published.exclude(
                        dorms_hidden_from__id=user_dorm.room.dorm.id,
                    )[:3]
                else:
                    latest_entries = Entry.published.filter(
                        dorms_hidden_from=None,
                    )

        else:
            latest_entries = Entry.published.filter(
                dorms_hidden_from=None,
            )

        context['latest_entries'] = latest_entries

        context['top_stories'] = TopNewsItem.objects.filter(
            date_expired__gt=datetime.datetime.now(pytz.utc),
            date_published__lte=datetime.datetime.now(pytz.utc),
            should_display=True
        ).order_by('-date_published')[:7]

        #print context['top_stories']
        return context
示例#2
0
    def get_context_data(self, **kwargs):
        context = super(LandingPage, self).get_context_data(**kwargs)

        tweets = cache.get('latest_tweets')

        if tweets:
            context['latest_tweets'] = tweets

        else:
            try:
                tweets = settings.TWITTER_AGENT.statuses.user_timeline()[:6]
            except URLError:
                tweets = []
            for tweet in tweets:
                tweet['date'] = pytz.utc.localize(
                    datetime.datetime.strptime(tweet['created_at'],
                                               "%a %b %d %H:%M:%S +0000 %Y"))

            cache.set('latest_tweets', tweets, settings.TWITTER_CACHE_TIMEOUT)

            context['latest_tweets'] = tweets

        if self.request.user.is_authenticated():
            if self.request.user.is_superuser:
                latest_entries = Entry.published.all()
            else:
                user_dorm = UserRoom.get_current_room(self.request.user)

                if user_dorm:
                    latest_entries = Entry.published.exclude(
                        dorms_hidden_from__id=user_dorm.room.dorm.id, )[:3]
                else:
                    latest_entries = Entry.published.filter(
                        dorms_hidden_from=None, )

        else:
            latest_entries = Entry.published.filter(dorms_hidden_from=None, )

        context['latest_entries'] = latest_entries

        context['top_stories'] = TopNewsItem.objects.filter(
            date_expired__gt=datetime.datetime.now(pytz.utc),
            date_published__lte=datetime.datetime.now(pytz.utc),
            should_display=True).order_by('-date_published')[:7]

        #print context['top_stories']
        return context
示例#3
0
def current_suites(user, type="transient"):

    room = UserRoom.get_current_room(user)

    if type == 'dorm':
        # By definition, a room is in one *physical* suite, so
        # return that - but in list format, so that it's consistent
        # with the transient form
        if room.room.suite:
            suites = [room.room.suite]
        else:
            suites = []
        return suites

    elif type == 'transient':
        sem = Semester.get_this_semester()
        tsms = TransientSuiteMembership.objects.filter(
            user=user,
            semesters__id=sem.id,
        )
        return [tsm.tsuite for tsm in tsms]

    return []
示例#4
0
def current_suites(user, type="transient"):

    room = UserRoom.get_current_room(user)

    if type == 'dorm':
        # By definition, a room is in one *physical* suite, so
        # return that - but in list format, so that it's consistent
        # with the transient form
        if room.room.suite:
            suites = [room.room.suite]
        else:
            suites = []
        return suites

    elif type == 'transient':
        sem = Semester.get_this_semester()
        tsms = TransientSuiteMembership.objects.filter(
            user=user,
            semesters__id=sem.id,
        )
        return [tsm.tsuite for tsm in tsms]

    return []
示例#5
0
def get_living_situation(user):
    return UserRoom.get_current_room(user)
示例#6
0
def get_living_situation(user):
    return UserRoom.get_current_room(user)
示例#7
0
    def get_context_data(self, **kwargs):
        context = super(LandingPage, self).get_context_data(**kwargs)

        tweets = cache.get('latest_tweets')
        semester = Semester.get_this_semester()
        if tweets:
            context['latest_tweets'] = tweets

        else:
            try:
                tweets = settings.TWITTER_AGENT.statuses.user_timeline()[:6]
            except:
                tweets = []
            for tweet in tweets:
                tweet['date'] = pytz.utc.localize(datetime.datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S +0000 %Y"))

            cache.set('latest_tweets', tweets, settings.TWITTER_CACHE_TIMEOUT)

            context['latest_tweets'] = tweets

        if self.request.user.is_authenticated():
            if self.request.user.is_superuser:
                latest_entries = Entry.published.all()
            else:
                user_dorm = UserRoom.get_current_room(self.request.user)
                try:
                    # if they're off campus, make sure the right official dorm is used:
                    if not user_dorm.room.dorm.official_dorm:
                        user_dorm = UserRoom.objects.get(
                            user=self.request.user,
                            semesters__id=semester.id,
                            room__dorm__code="OFF",
                            room__number="Symbolic Room",
                        )
                except AttributeError:
                    user_dorm = None

                except UserRoom.DoesNotExist:
                    # No offcampus room, then they're probably abroad.
                    try:
                        user_dorm = UserRoom.objects.get(
                            user=self.request.user,
                            room__dorm__code="ABR",
                            semesters__id=semester.id,
                        )

                    except UserRoom.DoesNotExist:
                        user_dorm = None

                if user_dorm:
                    latest_entries = Entry.published.exclude(
                        dorms_hidden_from__id=user_dorm.room.dorm.id,
                    )[:3]
                else:
                    latest_entries = Entry.published.filter(
                        dorms_hidden_from=None,
                    )

        else:
            latest_entries = Entry.published.filter(
                dorms_hidden_from=None,
            )

        paginator = Paginator(latest_entries, 7)
        page = self.request.GET.get('page', None)
        try:
            latest_entries = paginator.page(page)
        except PageNotAnInteger:
            latest_entries = paginator.page(1)
        except EmptyPage:
            # page out of range
            latest_entries = paginator.page(paginator.num_pages)

        context['latest_entries'] = latest_entries
        context['page'] = page

        context['top_stories'] = TopNewsItem.objects.filter(
            date_expired__gt=datetime.datetime.now(pytz.utc),
            date_published__lte=datetime.datetime.now(pytz.utc),
            should_display=True
        ).order_by('-date_published')[:7]

        #print context['top_stories']
        return context
示例#8
0
    def get_context_data(self, **kwargs):
        context = super(LandingPage, self).get_context_data(**kwargs)

        tweets = cache.get('latest_tweets')
        semester = Semester.get_this_semester()
        if tweets:
            context['latest_tweets'] = tweets

        else:
            try:
                tweets = settings.TWITTER_AGENT.statuses.user_timeline()[:6]
            except URLError:
                tweets = []
            for tweet in tweets:
                tweet['date'] = pytz.utc.localize(
                    datetime.datetime.strptime(tweet['created_at'],
                                               "%a %b %d %H:%M:%S +0000 %Y"))

            cache.set('latest_tweets', tweets, settings.TWITTER_CACHE_TIMEOUT)

            context['latest_tweets'] = tweets

        if self.request.user.is_authenticated():
            if self.request.user.is_superuser:
                latest_entries = Entry.published.all()
            else:
                user_dorm = UserRoom.get_current_room(self.request.user)
                try:
                    # if they're off campus, make sure the right official dorm is used:
                    if not user_dorm.room.dorm.official_dorm:
                        user_dorm = UserRoom.objects.get(
                            user=self.request.user,
                            semesters__id=semester.id,
                            room__dorm__code="OFF",
                            room__number="Symbolic Room",
                        )
                except AttributeError:
                    user_dorm = None

                except UserRoom.DoesNotExist:
                    # No offcampus room, then they're probably abroad.
                    try:
                        user_dorm = UserRoom.objects.get(
                            user=self.request.user,
                            room__dorm__code="ABR",
                            semesters__id=semester.id,
                        )

                    except UserRoom.DoesNotExist:
                        user_dorm = None

                if user_dorm:
                    latest_entries = Entry.published.exclude(
                        dorms_hidden_from__id=user_dorm.room.dorm.id, )[:3]
                else:
                    latest_entries = Entry.published.filter(
                        dorms_hidden_from=None, )

        else:
            latest_entries = Entry.published.filter(dorms_hidden_from=None, )

        paginator = Paginator(latest_entries, 7)
        page = self.request.GET.get('page', None)
        try:
            latest_entries = paginator.page(page)
        except PageNotAnInteger:
            latest_entries = paginator.page(1)
        except EmptyPage:
            # page out of range
            latest_entries = paginator.page(paginator.num_pages)

        context['latest_entries'] = latest_entries
        context['page'] = page

        context['top_stories'] = TopNewsItem.objects.filter(
            date_expired__gt=datetime.datetime.now(pytz.utc),
            date_published__lte=datetime.datetime.now(pytz.utc),
            should_display=True).order_by('-date_published')[:7]

        #print context['top_stories']
        return context