Browse Source

News annoucements

Added a feature to news stories so that they can be marked as
annoucements and shown on the homepage.
pull/58/head
Steven vanZyl 1 month ago
parent
commit
549adb0ad8

+ 2
- 0
migrations/sqlite/2019-08-29-143242_alter_news_annoucement/down.sql View File

@@ -0,0 +1,2 @@
1
+-- Alter the table to remove the column
2
+ALTER TABLE news DROP COLUMN announcement;

+ 2
- 0
migrations/sqlite/2019-08-29-143242_alter_news_annoucement/up.sql View File

@@ -0,0 +1,2 @@
1
+-- Add the announcement column
2
+ALTER TABLE news ADD announcement boolean NOT NULL DEFAULT false;

+ 11
- 1
src/handlers.rs View File

@@ -27,9 +27,19 @@ pub use crate::users::handlers::*;
27 27
 ///
28 28
 /// The index page of the site
29 29
 #[get("/")]
30
-pub fn index(l: MaybeLoggedIn) -> IndexTemplate {
30
+pub fn index(conn: ObservDbConn, l: MaybeLoggedIn) -> IndexTemplate {
31 31
     IndexTemplate {
32 32
         logged_in: l.user(),
33
+        announcement: {
34
+            use crate::news::models::NewsStory;
35
+            use crate::schema::news::dsl::*;
36
+            use diesel::prelude::*;
37
+            news.filter(announcement.eq(true))
38
+                .order(happened_at.desc())
39
+                .first::<NewsStory>(&*conn)
40
+                .optional()
41
+                .expect("Failed to get latest annoucement from news table of database")
42
+        },
33 43
         version: env!("CARGO_PKG_VERSION"),
34 44
     }
35 45
 }

+ 4
- 4
src/news/handlers.rs View File

@@ -21,7 +21,7 @@ pub fn news(conn: ObservDbConn, l: MaybeLoggedIn) -> NewsTemplate {
21 21
     NewsTemplate {
22 22
         logged_in: l.user(),
23 23
         stories: news
24
-            .order(happened_at.asc())
24
+            .order(happened_at.desc())
25 25
             .load(&*conn)
26 26
             .expect("Failed to get news from database"),
27 27
     }
@@ -31,7 +31,7 @@ pub fn news(conn: ObservDbConn, l: MaybeLoggedIn) -> NewsTemplate {
31 31
 pub fn news_json(conn: ObservDbConn, _l: MaybeLoggedIn) -> Json<Vec<NewsStory>> {
32 32
     use crate::schema::news::dsl::*;
33 33
     Json(
34
-        news.order(happened_at.asc())
34
+        news.order(happened_at.desc())
35 35
             .load(&*conn)
36 36
             .expect("Failed to get news from database"),
37 37
     )
@@ -183,7 +183,7 @@ pub fn news_summary(conn: &SqliteConnection) -> (Vec<Event>, Vec<NewsStory>) {
183 183
             use crate::schema::events::dsl::*;
184 184
             let now = chrono::offset::Local::now().format("%F %R").to_string();
185 185
             events
186
-                .order(start.asc())
186
+                .order(start.desc())
187 187
                 .filter(start.gt(now))
188 188
                 .limit(5)
189 189
                 .load(&*conn)
@@ -191,7 +191,7 @@ pub fn news_summary(conn: &SqliteConnection) -> (Vec<Event>, Vec<NewsStory>) {
191 191
         },
192 192
         {
193 193
             use crate::schema::news::dsl::*;
194
-            news.order(happened_at.asc())
194
+            news.order(happened_at.desc())
195 195
                 .limit(5)
196 196
                 .load(&*conn)
197 197
                 .expect("Failed to get news from database")

+ 2
- 0
src/news/models.rs View File

@@ -10,6 +10,7 @@ pub struct NewsStory {
10 10
     pub title: String,
11 11
     pub description: String,
12 12
     pub color: Option<String>,
13
+    pub announcement: bool,
13 14
 }
14 15
 
15 16
 #[derive(Debug, Default, Clone, FromForm, Insertable, AsChangeset)]
@@ -19,6 +20,7 @@ pub struct NewNewsStory {
19 20
     pub title: String,
20 21
     pub description: String,
21 22
     pub color: Option<String>,
23
+    pub announcement: bool,
22 24
 }
23 25
 
24 26
 impl NewNewsStory {

+ 1
- 0
src/schema.rs View File

@@ -48,6 +48,7 @@ table! {
48 48
         title -> Text,
49 49
         description -> Text,
50 50
         color -> Nullable<Text>,
51
+        announcement -> Bool,
51 52
     }
52 53
 }
53 54
 

+ 2
- 0
src/templates.rs View File

@@ -7,6 +7,7 @@
7 7
 //! by a handler since they can be rendered to HTML.
8 8
 
9 9
 use crate::models::User;
10
+use crate::news::models::NewsStory;
10 11
 
11 12
 /// Companion to `MaybeLoggedIn`
12 13
 ///
@@ -20,6 +21,7 @@ pub type OptUser = Option<User>;
20 21
 #[template(path = "index.html")]
21 22
 pub struct IndexTemplate {
22 23
     pub logged_in: OptUser,
24
+    pub announcement: Option<NewsStory>,
23 25
     pub version: &'static str,
24 26
 }
25 27
 

+ 1
- 1
templates/base.html View File

@@ -37,7 +37,7 @@
37 37
             </div>
38 38
         </div>
39 39
     </main>
40
-    <footer class="text-center footer mt-auto py-3">
40
+    <footer class="footer text-center mt-auto py-3">
41 41
         <div class="container">
42 42
             {% block footer %}{% endblock %}
43 43
             <a href="https://github.com/rcos/observatory-new/">

+ 9
- 1
templates/index.html View File

@@ -20,6 +20,14 @@
20 20
     <!-- <p class="lead">Rensselaer Center for Open Source</p> -->
21 21
 </div>
22 22
 
23
+{% match announcement %}
24
+{% when Some with (a) %}
25
+<section class="alert alert-info">
26
+    <a href="/news/{{ a.id }}">{{ a.title }}</a>
27
+</section>
28
+{% when None %}
29
+{% endmatch %}
30
+
23 31
 <section class="about">
24 32
     <h2>About</h2>
25 33
     <p>
@@ -50,7 +58,7 @@
50 58
             <h5 class="card-title">Wiki</h5>
51 59
             <p class="card-text">
52 60
                 The official RCOS wiki full of the knowledge of our members.
53
-                Add yours to help others!
61
+                Add what you know to help others!
54 62
             </p>
55 63
 	    <!-- This URL needs to be updated when the wiki moves -->
56 64
             <a class="card-link stretched-link" href="https://rcos-wiki.cs.rpi.edu">rcos-wiki.cs.rpi.edu</a>

+ 7
- 1
templates/news/edit-newsstory.html View File

@@ -24,6 +24,12 @@
24 24
         <label for="start">Happened At</label>
25 25
         <input type="datetime-local" name="happened_at" class="form-control" value="{{ story.happened_at }}" required>
26 26
     </div>
27
+    <div class="form-group form-check">
28
+        <input type="checkbox" name="announcement" id="announcement" class="form-check-input" />
29
+        <label for="announcement" class="form-check-label">
30
+            Announcement (will be shown on homepage)
31
+        </label>
32
+    </div>
27 33
     <div class="form-group">
28 34
         <label for="color">Calendar Color</label>
29 35
         <select name="color" class="custom-select" required>
@@ -38,4 +44,4 @@
38 44
     </div>
39 45
     <button type="submit" class="btn btn-primary">Submit</button>
40 46
 </form>
41
-{% endblock %}
47
+{% endblock %}

+ 7
- 1
templates/news/new-newsstory.html View File

@@ -24,6 +24,12 @@
24 24
         <label for="start">Happened At</label>
25 25
         <input type="datetime-local" name="happened_at" class="form-control" required>
26 26
     </div>
27
+    <div class="form-group form-check">
28
+        <input type="checkbox" name="announcement" id="announcement" class="form-check-input" />
29
+        <label for="announcement" class="form-check-label">
30
+            Announcement (will be shown on homepage)
31
+        </label>
32
+    </div>
27 33
     <div class="form-group">
28 34
         <label for="color">Calendar Color</label>
29 35
         <select name="color" class="custom-select" required>
@@ -38,4 +44,4 @@
38 44
     </div>
39 45
     <button type="submit" class="btn btn-primary">Submit</button>
40 46
 </form>
41
-{% endblock %}
47
+{% endblock %}

Loading…
Cancel
Save