Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8728: Upgrade/datetimefield format #8729

Conversation

AgostiniAlessandro
Copy link
Contributor

@AgostiniAlessandro AgostiniAlessandro commented Oct 30, 2023

Fixes #8728

Added the AllowDisplayOptionsOverride property in DateTimeFieldSetting.

Handled this setting in DateTimeField so that it returns the overriding value (if set) or (if there's no overriding value set) the setting value.

Handles a new case in in DefaultDateLocalizationServices.cs: when converting a DateTime to and from UTC with the IgnoreDate setting, if the DateTime had a valid Date (!= DateTime.min) the code was returning a wrong value since it didn't handle the solar time correctly.
Probably this case wasn't handled correctly since DateTimeFields were only allowing the user to insert the time when their Display property was set to TimeOnly, but we decided to show both the Date and Time editors when the AllowDisplayOptionsOverride is set to true so now it's possibile to have to convert DateTime with a valid date and the IgnoreDate option set to true.

@AgostiniAlessandro
Copy link
Contributor Author

2 tests failed but when I tried to rerun them locally I got no problem. Also, since the tests that failed don't have nothing to do with the files I worked on I guess there was some problem running the test, probably some connection issues.

Here is the code for the two tests that are failing:

    [Test]
    public void StatusCodeShouldBe404ForUnexistingResources() {
        var download = _webDownloader.Download("http://orchardproject.net/yepyep");
        Assert.That(download, Is.Not.Null);
        Assert.That(download.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
        Assert.That(download.Content, Is.Null);
    }

    [Test]
    public void StatusCodeShouldBe200ForValidRequests() {
        var download = _webDownloader.Download("http://orchardproject.net/");
        Assert.That(download, Is.Not.Null);
        Assert.That(download.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        Assert.That(download.Content, Is.Not.Empty);
    }

@BenedekFarkas BenedekFarkas linked an issue Apr 15, 2024 that may be closed by this pull request
@BenedekFarkas BenedekFarkas changed the title Upgrade/datetimefield format #8728: Upgrade/datetimefield format Apr 18, 2024
@BenedekFarkas
Copy link
Member

BenedekFarkas commented Nov 18, 2024

Indeed, the test failures were caused by the domain being unreachable, I've fixed the test since then. Please merge from 1.10.x.

Copy link
Member

@BenedekFarkas BenedekFarkas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you agree with the suggestions, please be sure to commit them from the UI in bulk (= one commit), so that the corresponding conversation is also resolved, then proceed with follow-up changes (due to property name changes), if necessary.

@@ -1,7 +1,9 @@
using System;
using System.Runtime.InteropServices.WindowsRuntime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using System.Runtime.InteropServices.WindowsRuntime;

using Orchard.ContentManagement;
using Orchard.ContentManagement.FieldStorage;
using Orchard.Fields.Settings;
using Orchard.UI.Notify;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using Orchard.UI.Notify;

@@ -10,15 +12,15 @@ public DateTime DateTime {
get {
var settings = this.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var settings = this.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();

@@ -10,15 +12,15 @@ public DateTime DateTime {
get {
var settings = this.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();
var value = Storage.Get<DateTime>();
if (settings.Display == DateTimeFieldDisplays.DateOnly) {
if (Display == DateTimeFieldDisplays.DateOnly) {
return new DateTime(value.Year, value.Month, value.Day);
}
return value;
}

set {
var settings = this.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var settings = this.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();

Editor = new DateTimeEditor() {
Date = showDate ? DateLocalizationServices.ConvertToLocalizedDateString(value, options) : null,
Time = showTime ? DateLocalizationServices.ConvertToLocalizedTimeString(value, options) : null,
ShowDate = showDate,
ShowTime = showTime,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

@@ -42,6 +42,7 @@ public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPart
builder.WithSetting("DateTimeFieldSettings.Required", model.Required.ToString(CultureInfo.InvariantCulture));
model.DefaultValue = model.Editor == null ? model.DefaultValue : _dateLocalizationServices.ConvertFromLocalizedString(model.Editor.Date, model.Editor.Time);
builder.WithSetting("DateTimeFieldSettings.DefaultValue", model.DefaultValue.HasValue ? model.DefaultValue.Value.ToString(CultureInfo.InvariantCulture) : String.Empty);
builder.WithSetting("DateTimeFieldSettings.AllowDisplayOptionsOverride", model.AllowDisplayOptionsOverride.ToString(CultureInfo.InvariantCulture));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
builder.WithSetting("DateTimeFieldSettings.AllowDisplayOptionsOverride", model.AllowDisplayOptionsOverride.ToString(CultureInfo.InvariantCulture));
builder.WithSetting("DateTimeFieldSettings.AllowDisplayOptionOverride", model.AllowDisplayOptionsOverride.ToString(CultureInfo.InvariantCulture));


<fieldset>
<label for="@Html.FieldIdFor(m => Model.Editor.Date)" @if (Model.IsRequired) { <text> class="required" </text> }>@Model.Name</label>
@Html.EditorFor(m => m.Editor)
@if (Model.AllowDisplayOptionsOverride) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@if (Model.AllowDisplayOptionsOverride) {
@if (Model.AllowDisplayOptionOverride) {

Comment on lines +48 to +53
get {
return Storage.Get<string>("DisplayOption");
}
set {
Storage.Set("DisplayOption", value ?? String.Empty);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
get {
return Storage.Get<string>("DisplayOption");
}
set {
Storage.Set("DisplayOption", value ?? String.Empty);
}
get => Storage.Get<string>("DisplayOption");
set => Storage.Set("DisplayOption", value ?? string.Empty);

Comment on lines +42 to +44
set {
DisplayOption = value.ToString();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set {
DisplayOption = value.ToString();
}
set => DisplayOption = value.ToString();

Comment on lines +35 to 40
if (settings.AllowDisplayOptionsOverride) {
if (Enum.TryParse(DisplayOption, out DateTimeFieldDisplays displayOption)){
return displayOption;
}
}
return settings.Display;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (settings.AllowDisplayOptionsOverride) {
if (Enum.TryParse(DisplayOption, out DateTimeFieldDisplays displayOption)){
return displayOption;
}
}
return settings.Display;
return settings.AllowDisplayOptionOverride && Enum.TryParse(DisplayOption, out DateTimeFieldDisplays displayOption)
? displayOption
: settings.Display;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Display option override in DateTimeField
3 participants