Example #1
0
        private SecurePage.UpdateStatus DoAnswerUpdate(IList <IGrouping <string, AnswersViewRow> > table,
                                                       string questionKey, bool reportUnchanged, ref int updateCount)
        {
            var page         = VotePage.GetPage <SecurePoliticianPage>();
            var updateStatus = SecurePage.UpdateStatus.Failure; // default
            var description  = string.Empty;

            var feedback =
                page.Master.FindMainContentControl("Feedback" + questionKey) as
                FeedbackContainerControl;
            var updatePanel =
                page.Master.FindMainContentControl("UpdatePanel" + questionKey) as UpdatePanel;
            var textAnswerBox =
                page.Master.FindMainContentControl("TextBox" + questionKey) as TextBox;
            var textSourceBox =
                page.Master.FindMainContentControl("Source" + questionKey) as TextBox;
            var textDateBox   = page.Master.FindMainContentControl("Date" + questionKey) as TextBox;
            var youTubeUrlBox =
                page.Master.FindMainContentControl("YouTubeUrl" + questionKey) as TextBox;
            var youTubeSourceBox      = page.Master.FindMainContentControl("YouTubeSource" + questionKey) as TextBox;
            var youTubeSourceUrlBox   = page.Master.FindMainContentControl("YouTubeSourceUrl" + questionKey) as TextBox;
            var youTubeDateBox        = page.Master.FindMainContentControl("YouTubeDate" + questionKey) as TextBox;
            var youTubeDescriptionBox =
                page.Master.FindMainContentControl("YouTubeDescription" + questionKey) as TextBox;
            var youTubeRunningTimeBox =
                page.Master.FindMainContentControl("YouTubeRunningTime" + questionKey) as TextBox;
            var youTubeFromCandidate =
                page.Master.FindMainContentControl("YouTubeFromCandidate" + questionKey) as HtmlInputCheckBox;
            var youTubeFromVoteUsa =
                page.Master.FindMainContentControl("YouTubeFromVoteUSA" + questionKey) as HtmlInputCheckBox;
            var sequenceHidden = page.Master.FindMainContentControl("Sequence" + questionKey) as HtmlInputHidden;
            var hasValue       =
                page.Master.FindMainContentControl("HasValue" + questionKey) as HtmlInputHidden;

            var textIsFromCandidate = SecurePage.IsPoliticianUser;

            var youTubeIsFromCandidate = SecurePage.IsPoliticianUser || youTubeFromCandidate.Checked ||
                                         youTubeSourceBox.Text.Trim() == YouTubeInfo.VideoUploadedByCandidateMessage;

            try
            {
                textAnswerBox.AddCssClasses("badupdate");

                FeedbackContainerControl.ClearValidationErrors(textAnswerBox, textSourceBox, textDateBox,
                                                               youTubeUrlBox, youTubeDateBox);

                var sequence = sequenceHidden.Value == "?"
          ? Answers.GetNextSequence(page.PoliticianKey, questionKey)
          : int.Parse(sequenceHidden.Value);

                var      newTextAnswer = textAnswerBox.GetValue();
                string   newTextSource;
                DateTime newTextDate;
                var      newYouTubeSource    = string.Empty;
                var      newYouTubeSourceUrl = string.Empty;
                var      newYouTubeDate      = VotePage.DefaultDbDate;
                bool     success;
                var      textDateWasEmpty    = true;
                var      youTubeDateWasEmpty = true;
                youTubeDescriptionBox.Text = string.Empty;
                youTubeRunningTimeBox.Text = string.Empty;

                newTextAnswer = feedback.StripHtml(newTextAnswer);
                newTextAnswer = newTextAnswer.StripRedundantSpaces();
                var oldResponses = table.Where(g => g.Key.IsEqIgnoreCase(questionKey))
                                   .SelectMany(g => g);
                var question = oldResponses.First();
                var oldRow   = table.Where(g => g.Key.IsEqIgnoreCase(questionKey))
                               .SelectMany(g => g)
                               .FirstOrDefault(r => r.Sequence == sequence);
                description = '"' + question.Question + '"';

                var isAnswerChanged = oldRow == null || /*newTextAnswer*/ textAnswerBox.GetValue().Trim() != oldRow.Answer.Trim();

                if (string.IsNullOrWhiteSpace(newTextAnswer))
                {
                    newTextSource = string.Empty;
                    newTextDate   = VotePage.DefaultDbDate;
                }
                else if (textIsFromCandidate)
                {
                    newTextSource = page.PageCache.Politicians.GetLastName(page.PoliticianKey);
                    newTextDate   = DateTime.UtcNow.Date;
                }
                else
                {
                    newTextSource    = textSourceBox.Text;
                    newTextSource    = feedback.StripHtml(newTextSource);
                    newTextSource    = newTextSource.StripRedundantSpaces();
                    textDateWasEmpty = string.IsNullOrWhiteSpace(textDateBox.Text);
                    newTextDate      =
                        feedback.ValidateDateOptional(textDateBox, out success, "Text Date",
                                                      isAnswerChanged ? DateTime.UtcNow.Date : VotePage.DefaultDbDate)
                        .Date;
                }

                var newYouTubeUrl = youTubeUrlBox.GetValue();
                newYouTubeUrl = feedback.StripHtml(newYouTubeUrl);
                newYouTubeUrl = newYouTubeUrl.StripRedundantSpaces();

                YouTubeInfo youTubeInfo = null;
                if (!string.IsNullOrWhiteSpace(newYouTubeUrl))
                {
                    var youTubeId = newYouTubeUrl.GetYouTubeVideoId();
                    if (youTubeFromCandidate != null && !youTubeFromCandidate.Checked &&
                        youTubeFromVoteUsa != null && !youTubeFromVoteUsa.Checked)
                    {
                        feedback.PostValidationError(new[] { youTubeFromCandidate, youTubeFromVoteUsa }, "Please select a type of video");
                    }
                    if (string.IsNullOrWhiteSpace(youTubeId))
                    {
                        feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.InvalidVideoUrlMessage);
                    }
                    else
                    {
                        youTubeInfo = YouTubeUtility.GetVideoInfo(youTubeId, true, 1);
                        if (!youTubeInfo.IsValid)
                        {
                            feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.VideoIdNotFoundMessage);
                        }
                        else if (!youTubeInfo.IsPublic)
                        {
                            feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.VideoNotPublicMessage);
                        }
                        else
                        {
                            youTubeDateWasEmpty = string.IsNullOrWhiteSpace(youTubeDateBox?.Text);
                            if (youTubeIsFromCandidate)
                            {
                                newYouTubeSource    = YouTubeInfo.VideoUploadedByCandidateMessage;
                                newYouTubeSourceUrl = string.Empty;
                                newYouTubeDate      = youTubeInfo.PublishedAt;
                            }
                            else
                            {
                                newYouTubeSource    = youTubeSourceBox.Text;
                                newYouTubeSource    = feedback.StripHtml(newYouTubeSource);
                                newYouTubeSource    = newYouTubeSource.StripRedundantSpaces();
                                newYouTubeSourceUrl = youTubeSourceUrlBox == null
                  ? string.Empty
                  : youTubeSourceUrlBox.Text;
                                newYouTubeSourceUrl = feedback.StripHtml(newYouTubeSourceUrl);
                                newYouTubeSourceUrl = Validation.StripWebProtocol(newYouTubeSourceUrl);
                                newYouTubeDate      = youTubeDateWasEmpty
                  ? youTubeInfo.PublishedAt
                  : feedback.ValidateDate(youTubeDateBox, out success, "YouTube Date",
                                          new DateTime(2004, 1, 1), DateTime.UtcNow).Date;
                            }
                        }
                    }
                }

                if (feedback.ValidationErrorCount == 0)
                {
                    var oldTextAnswer       = string.Empty;
                    var oldTextSource       = string.Empty;
                    var oldTextDate         = VotePage.DefaultDbDate;
                    var oldYouTubeUrl       = string.Empty;
                    var oldYouTubeSource    = string.Empty;
                    var oldYouTubeSourceUrl = string.Empty;
                    var oldYouTubeDate      = VotePage.DefaultDbDate;

                    if (oldRow != null)
                    {
                        oldTextAnswer       = oldRow.Answer.SafeString();
                        oldTextSource       = oldRow.Source.SafeString();
                        oldTextDate         = oldRow.DateStamp.SafeDbDate();
                        oldYouTubeUrl       = oldRow.YouTubeUrl.SafeString();
                        oldYouTubeSource    = oldRow.YouTubeSource();
                        oldYouTubeSourceUrl = oldRow.YouTubeSourceUrl().SafeString();
                        oldYouTubeDate      = oldRow.YouTubeDate.SafeDbDate()
                                              .Date;
                    }

                    var unchanged = oldTextAnswer == newTextAnswer &&
                                    oldYouTubeUrl == newYouTubeUrl &&
                                    oldYouTubeSource == newYouTubeSource &&
                                    oldYouTubeSourceUrl == newYouTubeSourceUrl &&
                                    (oldYouTubeDate == newYouTubeDate || youTubeDateWasEmpty);
                    if (unchanged && !textIsFromCandidate)
                    {
                        unchanged = oldTextSource == newTextSource &&
                                    (oldTextDate == newTextDate || textDateWasEmpty);
                    }

                    if (unchanged)
                    {
                        if (reportUnchanged)
                        {
                            feedback.AddInfo("Your " + description + " entry was unchanged.");
                            updatePanel.Update();
                        }
                        updateStatus = SecurePage.UpdateStatus.Unchanged;
                    }
                    else
                    {
                        if (!textIsFromCandidate && !question.IsTextSourceOptional.GetValueOrDefault() &&
                            !string.IsNullOrWhiteSpace(newTextAnswer) &&
                            (isAnswerChanged || !string.IsNullOrWhiteSpace(oldTextSource)))
                        {
                            feedback.ValidateLength(textSourceBox, "Text Source", 1, 255, out success);
                        }
                        if (!string.IsNullOrWhiteSpace(newYouTubeUrl))
                        {
                            if (!youTubeIsFromCandidate)
                            {
                                feedback.ValidateRequired(youTubeSourceBox, "YouTube Source", out success);
                            }
                        }
                        if (feedback.ValidationErrorCount == 0)
                        {
                            string videoDescription = null;
                            var    videoRunningTime = default(TimeSpan);
                            if (youTubeInfo != null)
                            {
                                videoDescription           = youTubeInfo.ShortDescription;
                                videoRunningTime           = youTubeInfo.Duration;
                                youTubeDescriptionBox.Text = videoDescription;
                                youTubeRunningTimeBox.Text = videoRunningTime.FormatRunningTime();
                            }

                            if (youTubeSourceBox != null)
                            {
                                youTubeSourceBox.Text = newYouTubeSource;
                            }
                            if (youTubeSourceUrlBox != null)
                            {
                                youTubeSourceUrlBox.Text = newYouTubeSourceUrl;
                            }
                            if (youTubeDateBox != null)
                            {
                                youTubeDateBox.Text = newYouTubeDate.DbDateToShortDate();
                            }
                            if (youTubeFromCandidate != null)
                            {
                                youTubeFromCandidate.Checked = youTubeIsFromCandidate;
                            }
                            if (youTubeFromVoteUsa != null)
                            {
                                youTubeFromVoteUsa.Checked = !youTubeIsFromCandidate;
                            }

                            hasValue.Value = string.IsNullOrWhiteSpace(newTextAnswer) &&
                                             string.IsNullOrWhiteSpace(newYouTubeUrl)
                ? string.Empty
                : "Y";
                            page.LogPoliticianAnswerChange(questionKey, sequence, oldTextAnswer, newTextAnswer,
                                                           newTextSource);
                            page.UpdatePoliticianAnswer(questionKey, sequence, question.IssueKey, newTextAnswer,
                                                        newTextSource, newTextDate, newYouTubeUrl, videoDescription, videoRunningTime,
                                                        newYouTubeSource, newYouTubeSourceUrl, newYouTubeDate);
                            UpdateQuestion(questionKey, sequence);
                            feedback.AddInfo("Your " + description + " entry was updated.");
                            updateStatus = SecurePage.UpdateStatus.Success;
                            updateCount++;
                        }
                        updatePanel.Update();
                    }
                }

                if (updateStatus != SecurePage.UpdateStatus.Failure)
                {
                    if (newTextAnswer != textAnswerBox.Text)
                    {
                        updatePanel.Update();
                    }
                    textAnswerBox.SetValue(newTextAnswer);
                    if (!textIsFromCandidate)
                    {
                        if (string.IsNullOrWhiteSpace(newTextAnswer))
                        {
                            newTextSource = string.Empty;
                            newTextDate   = VotePage.DefaultDbDate;
                        }
                        var newDateText = newTextDate.DbDateToShortDate();
                        Debug.Assert(textSourceBox != null, "sourceBox != null");
                        Debug.Assert(textDateBox != null, "dateBox != null");
                        if (newTextSource != textSourceBox.Text || newDateText != textDateBox.Text)
                        {
                            updatePanel.Update();
                        }
                        textSourceBox.SetValue(newTextSource);
                        textDateBox.SetValue(newDateText);
                    }
                    if (!youTubeIsFromCandidate)
                    {
                        var newYouTubeDateText = newYouTubeDate == VotePage.DefaultDbDate
              ? string.Empty
              : newYouTubeDate.DbDateToShortDate();
                        if (newYouTubeDateText != youTubeDateBox.Text)
                        {
                            updatePanel.Update();
                        }
                        youTubeDateBox.SetValue(newYouTubeDateText);
                    }
                }

                textAnswerBox.RemoveCssClass("badupdate");
            }
            catch (Exception ex)
            {
                if (description == string.Empty)
                {
                    description = "your response";
                }
                feedback.AddError("There was an unexpected error updating " + description);
                feedback.HandleException(ex);
                updatePanel.Update();
            }

            return(updateStatus);
        }
Example #2
0
        public static void RefreshYouTubePoliticians()
        {
            // this is so we only do it every three days
            if (DateTime.UtcNow.Day % 3 != 0)
            {
                return;
            }

            string message;

            try
            {
                VotePage.LogInfo("RefreshYouTubePoliticians", "Started");

                var politiciansUpdated = 0;
                var table = Politicians.GetYouTubeRefreshData();

                foreach (var row in table)
                {
                    YouTubeInfo info      = null;
                    var         url       = row.YouTubeWebAddress;
                    var         videoType = string.Empty;
                    if (url.IsValidYouTubeVideoUrl())
                    {
                        var id = url.GetYouTubeVideoId();
                        info      = GetVideoInfo(id, false, 1);
                        videoType = "video";
                    }
                    else if (url.IsValidYouTubePlaylistUrl())
                    {
                        var id = url.GetYouTubePlaylistId();
                        info      = GetPlaylistInfo(id, false, 1);
                        videoType = "playlist";
                    }
                    else if (url.IsValidYouTubeChannelUrl() || url.IsValidYouTubeCustomChannelUrl() ||
                             url.IsValidYouTubeUserChannelUrl())
                    {
                        var id = LookupChannelId(url, 1);
                        if (!string.IsNullOrWhiteSpace(id))
                        {
                            info = GetChannelInfo(id, false, 1);
                        }
                        videoType = "channel";
                    }

                    var error = string.Empty;
                    if (info == null || !info.IsValid)
                    {
                        switch (videoType)
                        {
                        case "video":
                            error = YouTubeInfo.VideoIdNotFoundMessage;
                            break;

                        case "playlist":
                            error = YouTubeInfo.PlaylistIdNotFoundMessage;
                            break;

                        case "channel":
                            error = YouTubeInfo.ChannelIdNotFoundMessage;
                            break;

                        default:
                            error = YouTubeInfo.InvalidVideoUrlMessage;
                            break;
                        }

                        if (row.YouTubeAutoDisable != error)
                        {
                            row.YouTubeAutoDisable = error;
                            politiciansUpdated++;
                        }
                        continue;
                    }

                    if (!info.IsPublic)
                    {
                        switch (videoType)
                        {
                        case "video":
                            error = YouTubeInfo.VideoNotPublicMessage;
                            break;

                        case "playlist":
                            error = YouTubeInfo.PlaylistNotPublicMessage;
                            break;

                        case "channel":
                            error = YouTubeInfo.ChannelNotPublicMessage;
                            break;
                        }
                        if (row.YouTubeAutoDisable != error)
                        {
                            row.YouTubeAutoDisable = error;
                            politiciansUpdated++;
                        }
                        continue;
                    }

                    if (info.ShortDescription != row.YouTubeDescription ||
                        info.Duration != row.YouTubeRunningTime ||
                        info.PublishedAt != row.YouTubeDate ||
                        row.YouTubeAutoDisable != null)
                    {
                        row.YouTubeDescription = info.ShortDescription;
                        row.YouTubeRunningTime = info.Duration;
                        row.YouTubeAutoDisable = null;
                        row.YouTubeDate        = info.PublishedAt;
                        politiciansUpdated++;
                    }
                }

                Politicians.UpdateTable(table, PoliticiansTable.ColumnSet.YouTubeRefresh);

                message =
                    $"{table.Count} Expired YouTube Politicians found, {politiciansUpdated} YouTube Politicians updated";
            }
            catch (Exception ex)
            {
                VotePage.LogException("RefreshYouTubePoliticians", ex);
                message = $"Exception: {ex.Message} [see exception log for details]";
            }

            VotePage.LogInfo("RefreshYouTubePoliticians", message);
        }