diff --git a/config/settings.py b/config/settings.py index 19a356b..1d6b4d2 100644 --- a/config/settings.py +++ b/config/settings.py @@ -175,6 +175,10 @@ STATIC_URL = '/static/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static"), +] + CRISPY_TEMPLATE_PACK = 'bootstrap4' LOGIN_URL = 'login' diff --git a/config/urls.py b/config/urls.py index f0c3811..2f27212 100644 --- a/config/urls.py +++ b/config/urls.py @@ -24,6 +24,8 @@ from django.contrib.auth import views as auth_views from users import views as user_views urlpatterns = [ + path('', include('news.urls')), + path('admin/', admin.site.urls), path('register/', user_views.register, name='register'), @@ -31,12 +33,11 @@ urlpatterns = [ path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'), path('profile/', user_views.profile, name='profile'), path('profile/password/', user_views.password, name='password'), + path('values/', user_views.values, name='values'), path('oauth/', include('social_django.urls', namespace='social')), - path('', include('news.urls')), ] - if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/media/article-headers/techarmour.png b/media/article-headers/techarmour.png new file mode 100644 index 0000000..3e77f14 Binary files /dev/null and b/media/article-headers/techarmour.png differ diff --git a/news/migrations/0001_initial.py b/news/migrations/0001_initial.py new file mode 100644 index 0000000..aa90545 --- /dev/null +++ b/news/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 3.1 on 2020-08-16 06:32 + +import ckeditor.fields +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Article', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('headline', models.CharField(max_length=100)), + ('content', ckeditor.fields.RichTextField(blank=True, null=True)), + ('date_published', models.DateTimeField(auto_now_add=True)), + ('likes', models.IntegerField(default=0)), + ('header', models.ImageField(blank=True, default='default-header.jpg', null=True, upload_to='article-headers')), + ('header_caption', models.CharField(default='', max_length=100)), + ('tag', models.CharField(choices=[('authoritarian_left', 'Authoritarian Left'), ('authoritarian_right', 'Authoritarian Right'), ('libertarian_left', 'Libertarian Left'), ('libertarian_right', 'Libertarian Right')], default='libertarian_left', max_length=19)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/news/migrations/__init__.py b/news/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/news/models.py b/news/models.py index e14bb31..8515a99 100644 --- a/news/models.py +++ b/news/models.py @@ -29,12 +29,12 @@ class Article(models.Model): date_published = models.DateTimeField(auto_now_add=True) author = models.ForeignKey(User, on_delete=models.CASCADE) likes = models.IntegerField(default=0) - header = models.ImageField(default='default-header.jpg', upload_to='article-headers') + header = models.ImageField(null=True, blank=True, default='default-header.jpg', upload_to='article-headers') header_caption = models.CharField(max_length=100, default="") - tag = models.CharField(max_length=19,choices=POLITICAL_CHOICES,default=A_L) + tag = models.CharField(max_length=19,choices=POLITICAL_CHOICES,default=L_L) def __str__(self): - return f"{self.author}'s article on {self.date_published}" + return f"{self.headline} by {self.author}" def get_absolute_url(self): return reverse('article-detail', kwargs={'pk': self.pk}) diff --git a/news/static/news/css/res/chris.jpg b/news/static/news/css/res/chris.jpg new file mode 100644 index 0000000..0003d8f Binary files /dev/null and b/news/static/news/css/res/chris.jpg differ diff --git a/news/static/news/css/res/josh.jpg b/news/static/news/css/res/josh.jpg new file mode 100644 index 0000000..9ea02a8 Binary files /dev/null and b/news/static/news/css/res/josh.jpg differ diff --git a/news/static/news/css/res/rushil.jpg b/news/static/news/css/res/rushil.jpg new file mode 100644 index 0000000..160d279 Binary files /dev/null and b/news/static/news/css/res/rushil.jpg differ diff --git a/news/static/news/css/res/spekr.png b/news/static/news/css/res/spekr.png new file mode 100644 index 0000000..f410627 Binary files /dev/null and b/news/static/news/css/res/spekr.png differ diff --git a/news/static/news/css/res/zaeem.jpg b/news/static/news/css/res/zaeem.jpg new file mode 100644 index 0000000..642eb19 Binary files /dev/null and b/news/static/news/css/res/zaeem.jpg differ diff --git a/news/templates/news/about.html b/news/templates/news/about.html index 67a10fa..049e7b3 100644 --- a/news/templates/news/about.html +++ b/news/templates/news/about.html @@ -1,5 +1,68 @@ {% extends 'news/base.html' %} +{% load static %} {% block content %}

About NewViewsNews

-

In this day and age, close-mindedness and echochambers prevent others from being exposed to people who offer varying perspectives. This has caused a lot of conflict within our society. Our solution: NewViewsNews.

NewViewsNews is a crowdsourcing news site that allows you to view the news and opinions of people who have opposing political views. To get started, you simply make an account and specify your position on the political compass. After that, you can write your own posts and view other posts! Our algorithms will display posts from individuals on the opposite side of the compass on your home page.

NewViewsNews is paving the way for an open minded and more understanding community. Join us today!

+
In this day and age, close-mindedness and echochambers prevent others from being exposed to people who offer varying perspectives. This has caused a lot of conflict within our society. Our solution: NewViewsNews.
+logo +


+

NewViewsNews is a crowdsourcing news site that allows you to view the news and opinions of people who have opposing political views. To get started, you simply make an account and specify your position on the political compass. After that, you can write your own posts and view other posts! Our algorithms will display posts from individuals on the opposite side of the compass on your home page.

+

NewViewsNews is paving the way for an open minded and more understanding community. Join us today!

+ +
+ +
+
+
Rushil Umaretiya
developer - team member + +
+
+ + + +
+
+
Christopher Arraya
developer - team member + +
+
+ + + +
+
+
Zaeem Qureshi
developer - team member + +
+
+ + + +
+
+
Joshua Lim
developer - team member + +
+
+ +
{% endblock content %} diff --git a/news/templates/news/article_form.html b/news/templates/news/article_form.html index 78e2892..b9ff7d0 100644 --- a/news/templates/news/article_form.html +++ b/news/templates/news/article_form.html @@ -2,7 +2,7 @@ {% load crispy_forms_tags %} {% block content %}
-
+ {% csrf_token %}
diff --git a/news/templates/news/base.html b/news/templates/news/base.html index 93d5560..de2feb5 100644 --- a/news/templates/news/base.html +++ b/news/templates/news/base.html @@ -11,6 +11,8 @@ + + diff --git a/news/templates/news/home.html b/news/templates/news/home.html index 21da872..5c0d554 100644 --- a/news/templates/news/home.html +++ b/news/templates/news/home.html @@ -1,19 +1,30 @@ {% extends "news/base.html" %} {% block content %} + +
{% for article in articles %} - - {% endfor %} {% if is_paginated %} + + + {% if page_obj.has_previous %} First Previous diff --git a/news/templates/news/policy.html b/news/templates/news/policy.html new file mode 100644 index 0000000..d0e2f34 --- /dev/null +++ b/news/templates/news/policy.html @@ -0,0 +1,153 @@ +

Privacy Policy

+

Last updated: August 16, 2020

+

This Privacy Policy describes Our policies and procedures on the collection, use and disclosure of Your information when You use the Service and tells You about Your privacy rights and how the law protects You.

+

We use Your Personal data to provide and improve the Service. By using the Service, You agree to the collection and use of information in accordance with this Privacy Policy. This Privacy Policy has been created with the help of the Privacy Policy Generator.

+

Interpretation and Definitions

+

Interpretation

+

The words of which the initial letter is capitalized have meanings defined under the following conditions. The following definitions shall have the same meaning regardless of whether they appear in singular or in plural.

+

Definitions

+

For the purposes of this Privacy Policy:

+
    +
  • +

    Account means a unique account created for You to access our Service or parts of our Service.

    +
  • +
  • +

    Company (referred to as either "the Company", "We", "Us" or "Our" in this Agreement) refers to NewViewsNews.

    +
  • +
  • +

    Cookies are small files that are placed on Your computer, mobile device or any other device by a website, containing the details of Your browsing history on that website among its many uses.

    +
  • +
  • +

    Country refers to: Virginia, United States

    +
  • +
  • +

    Device means any device that can access the Service such as a computer, a cellphone or a digital tablet.

    +
  • +
  • +

    Personal Data is any information that relates to an identified or identifiable individual.

    +
  • +
  • +

    Service refers to the Website.

    +
  • +
  • +

    Service Provider means any natural or legal person who processes the data on behalf of the Company. It refers to third-party companies or individuals employed by the Company to facilitate the Service, to provide the Service on behalf of the Company, to perform services related to the Service or to assist the Company in analyzing how the Service is used.

    +
  • +
  • +

    Third-party Social Media Service refers to any website or any social network website through which a User can log in or create an account to use the Service.

    +
  • +
  • +

    Usage Data refers to data collected automatically, either generated by the use of the Service or from the Service infrastructure itself (for example, the duration of a page visit).

    +
  • +
  • +

    Website refers to NewViewsNews, accessible from https://newviewsnews.crucialnet.org

    +
  • +
  • +

    You means the individual accessing or using the Service, or the company, or other legal entity on behalf of which such individual is accessing or using the Service, as applicable.

    +
  • +
+

Collecting and Using Your Personal Data

+

Types of Data Collected

+

Personal Data

+

While using Our Service, We may ask You to provide Us with certain personally identifiable information that can be used to contact or identify You. Personally identifiable information may include, but is not limited to:

+
    +
  • +

    Email address

    +
  • +
  • +

    First name and last name

    +
  • +
  • +

    Usage Data

    +
  • +
+

Usage Data

+

Usage Data is collected automatically when using the Service.

+

Usage Data may include information such as Your Device's Internet Protocol address (e.g. IP address), browser type, browser version, the pages of our Service that You visit, the time and date of Your visit, the time spent on those pages, unique device identifiers and other diagnostic data.

+

When You access the Service by or through a mobile device, We may collect certain information automatically, including, but not limited to, the type of mobile device You use, Your mobile device unique ID, the IP address of Your mobile device, Your mobile operating system, the type of mobile Internet browser You use, unique device identifiers and other diagnostic data.

+

We may also collect information that Your browser sends whenever You visit our Service or when You access the Service by or through a mobile device.

+

Tracking Technologies and Cookies

+

We use Cookies and similar tracking technologies to track the activity on Our Service and store certain information. Tracking technologies used are beacons, tags, and scripts to collect and track information and to improve and analyze Our Service.

+

You can instruct Your browser to refuse all Cookies or to indicate when a Cookie is being sent. However, if You do not accept Cookies, You may not be able to use some parts of our Service.

+

Cookies can be "Persistent" or "Session" Cookies. Persistent Cookies remain on your personal computer or mobile device when You go offline, while Session Cookies are deleted as soon as You close your web browser. Learn more about cookies: All About Cookies.

+

We use both session and persistent Cookies for the purposes set out below:

+
    +
  • +

    Necessary / Essential Cookies

    +

    Type: Session Cookies

    +

    Administered by: Us

    +

    Purpose: These Cookies are essential to provide You with services available through the Website and to enable You to use some of its features. They help to authenticate users and prevent fraudulent use of user accounts. Without these Cookies, the services that You have asked for cannot be provided, and We only use these Cookies to provide You with those services.

    +
  • +
  • +

    Cookies Policy / Notice Acceptance Cookies

    +

    Type: Persistent Cookies

    +

    Administered by: Us

    +

    Purpose: These Cookies identify if users have accepted the use of cookies on the Website.

    +
  • +
  • +

    Functionality Cookies

    +

    Type: Persistent Cookies

    +

    Administered by: Us

    +

    Purpose: These Cookies allow us to remember choices You make when You use the Website, such as remembering your login details or language preference. The purpose of these Cookies is to provide You with a more personal experience and to avoid You having to re-enter your preferences every time You use the Website.

    +
  • +
+

For more information about the cookies we use and your choices regarding cookies, please visit our Cookies Policy or the Cookies section of our Privacy Policy.

+

Use of Your Personal Data

+

The Company may use Personal Data for the following purposes:

+
    +
  • To provide and maintain our Service, including to monitor the usage of our Service.
  • +
  • To manage Your Account: to manage Your registration as a user of the Service. The Personal Data You provide can give You access to different functionalities of the Service that are available to You as a registered user.
  • +
  • For the performance of a contract: the development, compliance and undertaking of the purchase contract for the products, items or services You have purchased or of any other contract with Us through the Service.
  • +
  • To contact You: To contact You by email, telephone calls, SMS, or other equivalent forms of electronic communication, such as a mobile application's push notifications regarding updates or informative communications related to the functionalities, products or contracted services, including the security updates, when necessary or reasonable for their implementation.
  • +
  • To provide You with news, special offers and general information about other goods, services and events which we offer that are similar to those that you have already purchased or enquired about unless You have opted not to receive such information.
  • +
  • To manage Your requests: To attend and manage Your requests to Us.
  • +
+

We may share your personal information in the following situations:

+
    +
  • With Service Providers: We may share Your personal information with Service Providers to monitor and analyze the use of our Service, to contact You.
  • +
  • For Business transfers: We may share or transfer Your personal information in connection with, or during negotiations of, any merger, sale of Company assets, financing, or acquisition of all or a portion of our business to another company.
  • +
  • With Affiliates: We may share Your information with Our affiliates, in which case we will require those affiliates to honor this Privacy Policy. Affiliates include Our parent company and any other subsidiaries, joint venture partners or other companies that We control or that are under common control with Us.
  • +
  • With Business partners: We may share Your information with Our business partners to offer You certain products, services or promotions.
  • +
  • With other users: when You share personal information or otherwise interact in the public areas with other users, such information may be viewed by all users and may be publicly distributed outside. If You interact with other users or register through a Third-Party Social Media Service, Your contacts on the Third-Party Social Media Service may see Your name, profile, pictures and description of Your activity. Similarly, other users will be able to view descriptions of Your activity, communicate with You and view Your profile.
  • +
+

Retention of Your Personal Data

+

The Company will retain Your Personal Data only for as long as is necessary for the purposes set out in this Privacy Policy. We will retain and use Your Personal Data to the extent necessary to comply with our legal obligations (for example, if we are required to retain your data to comply with applicable laws), resolve disputes, and enforce our legal agreements and policies.

+

The Company will also retain Usage Data for internal analysis purposes. Usage Data is generally retained for a shorter period of time, except when this data is used to strengthen the security or to improve the functionality of Our Service, or We are legally obligated to retain this data for longer time periods.

+

Transfer of Your Personal Data

+

Your information, including Personal Data, is processed at the Company's operating offices and in any other places where the parties involved in the processing are located. It means that this information may be transferred to — and maintained on — computers located outside of Your state, province, country or other governmental jurisdiction where the data protection laws may differ than those from Your jurisdiction.

+

Your consent to this Privacy Policy followed by Your submission of such information represents Your agreement to that transfer.

+

The Company will take all steps reasonably necessary to ensure that Your data is treated securely and in accordance with this Privacy Policy and no transfer of Your Personal Data will take place to an organization or a country unless there are adequate controls in place including the security of Your data and other personal information.

+

Disclosure of Your Personal Data

+

Business Transactions

+

If the Company is involved in a merger, acquisition or asset sale, Your Personal Data may be transferred. We will provide notice before Your Personal Data is transferred and becomes subject to a different Privacy Policy.

+

Law enforcement

+

Under certain circumstances, the Company may be required to disclose Your Personal Data if required to do so by law or in response to valid requests by public authorities (e.g. a court or a government agency).

+

Other legal requirements

+

The Company may disclose Your Personal Data in the good faith belief that such action is necessary to:

+
    +
  • Comply with a legal obligation
  • +
  • Protect and defend the rights or property of the Company
  • +
  • Prevent or investigate possible wrongdoing in connection with the Service
  • +
  • Protect the personal safety of Users of the Service or the public
  • +
  • Protect against legal liability
  • +
+

Security of Your Personal Data

+

The security of Your Personal Data is important to Us, but remember that no method of transmission over the Internet, or method of electronic storage is 100% secure. While We strive to use commercially acceptable means to protect Your Personal Data, We cannot guarantee its absolute security.

+

Your California Privacy Rights (California's Shine the Light law)

+

Under California Civil Code Section 1798 (California's Shine the Light law), California residents with an established business relationship with us can request information once a year about sharing their Personal Data with third parties for the third parties' direct marketing purposes.

+

If you'd like to request more information under the California Shine the Light law, and if you are a California resident, You can contact Us using the contact information provided below.

+

California Privacy Rights for Minor Users (California Business and Professions Code Section 22581)

+

California Business and Professions Code section 22581 allow California residents under the age of 18 who are registered users of online sites, services or applications to request and obtain removal of content or information they have publicly posted.

+

To request removal of such data, and if you are a California resident, You can contact Us using the contact information provided below, and include the email address associated with Your account.

+

Be aware that Your request does not guarantee complete or comprehensive removal of content or information posted online and that the law may not permit or require removal in certain circumstances.

+

Links to Other Websites

+

Our Service may contain links to other websites that are not operated by Us. If You click on a third party link, You will be directed to that third party's site. We strongly advise You to review the Privacy Policy of every site You visit.

+

We have no control over and assume no responsibility for the content, privacy policies or practices of any third party sites or services.

+

Changes to this Privacy Policy

+

We may update our Privacy Policy from time to time. We will notify You of any changes by posting the new Privacy Policy on this page.

+

We will let You know via email and/or a prominent notice on Our Service, prior to the change becoming effective and update the "Last updated" date at the top of this Privacy Policy.

+

You are advised to review this Privacy Policy periodically for any changes. Changes to this Privacy Policy are effective when they are posted on this page.

+

Contact Us

+

If you have any questions about this Privacy Policy, You can contact us:

+
    +
  • By email: r@crucialnet.org
  • +
diff --git a/news/urls.py b/news/urls.py index e45daed..5a1b7e3 100644 --- a/news/urls.py +++ b/news/urls.py @@ -12,6 +12,7 @@ from . import views urlpatterns = [ path('', views.home, name='home'), path('about/', views.about, name='about'), + path('policy/', views.policy, name='privacy-policy'), path('user/', UserArticleListView.as_view(), name='user-articles'), path('article//', ArticleDetailView.as_view(), name='article-detail'), diff --git a/news/views.py b/news/views.py index 8883068..07bc38c 100644 --- a/news/views.py +++ b/news/views.py @@ -1,5 +1,6 @@ -from django.shortcuts import render, get_object_or_404 +from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.models import User +from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.views.generic import ( ListView, @@ -15,16 +16,69 @@ from users.models import Profile # Create your views here. +@login_required def about (request): createProfileIfNotExist(request) + if checkValues(request) is False: + return redirect('values') return render (request, 'news/about.html') +def policy (request): + return render (request, 'news/policy.html') + +@login_required def home (request): createProfileIfNotExist(request) + if checkValues(request) is False: + return redirect('values') + + profile = request.user.profile + social = profile.socialScore + economic = profile.economicScore + + A_L = 'authoritarian_left' + A_R = 'authoritarian_right' + L_L = 'libertarian_left' + L_R = 'libertarian_right' + + if social > 0: + # Auth + if economic > 0: + #AuthRight + articles = Article.objects.exclude(tag=A_R) + elif economic < 0: + #AuthLeft + articles = Article.objects.exclude(tag=A_L) + else: + #AuthCenter + articles = Article.objects.exclude(tag=A_L).exclude(tag=A_R) + elif social < 0: + # Lib + if social > 0: + #LibRight + articles = Article.objects.exclude(tag=L_R) + elif social < 0: + #LibLeft + articles = Article.objects.exclude(tag=L_L) + else: + #LibCenter + articles = Article.objects.exclude(tag=L_L).exclude(tag=L_R) + else: + #___Center + if social > 0: + # RightCenter + articles = Article.objects.exclude(tag=L_L).exclude(tag=A_L) + if social < 0: + # LeftCenter + articles = Article.objects.exclude(tag=L_R).exclude(tag=A_R) + else: + # Center + articles = Article.objects.all() + context = { - 'articles': Article.objects.all() + 'articles': articles } return render (request, 'news/home.html', context) @@ -81,3 +135,9 @@ class ArticleDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): def createProfileIfNotExist (request): if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() < 1: Profile.objects.create(user=request.user).save() + +def checkValues (request): + if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() is 1: + profile = Profile.objects.filter(user=request.user).first() + return profile.gaveValues + return True diff --git a/users/forms.py b/users/forms.py index 82b0c8d..e9d37b3 100644 --- a/users/forms.py +++ b/users/forms.py @@ -24,3 +24,12 @@ class ProfileUpdateForm(forms.ModelForm): class Meta: model = Profile fields = ['profile_pic'] + +class ValuesUpdateForm(forms.ModelForm): + + economicScore = forms.IntegerField(label="Economic Score") + socialScore = forms.IntegerField(label="Social Score") + + class Meta: + model = Profile + fields = ['economicScore', 'socialScore'] diff --git a/users/signals.py b/users/signals.py index c0d507c..a76859e 100644 --- a/users/signals.py +++ b/users/signals.py @@ -9,11 +9,9 @@ from .models import Profile @receiver(post_save, sender=User) def create_profile(sender, instance, created, **kwargs): if created: - print ("CREATED") Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_profile(sender, instance, **kwargs): - print ("SAVED") instance.profile.save() diff --git a/users/templates/users/login.html b/users/templates/users/login.html index 9148a8c..7c58680 100644 --- a/users/templates/users/login.html +++ b/users/templates/users/login.html @@ -24,15 +24,15 @@
- + Sign in with Google - + Sign in with Github - + Sign in with Facebook diff --git a/users/templates/users/profile.html b/users/templates/users/profile.html index 32c2096..90c5ce7 100644 --- a/users/templates/users/profile.html +++ b/users/templates/users/profile.html @@ -81,5 +81,59 @@ {{ passwordForm|crispy }} +
+ + Your browser does not support the HTML canvas tag. + + + + +
+
+ {% csrf_token %} +
+ Retake the political compass test at spekr.org + {{ valuesForm|crispy}} +
+
+ +
+
+
{% endblock content %} diff --git a/users/templates/users/register.html b/users/templates/users/register.html index fc16e5a..05409a3 100644 --- a/users/templates/users/register.html +++ b/users/templates/users/register.html @@ -19,4 +19,3 @@
{% endblock content %} -© 2020 GitHub, Inc. diff --git a/users/templates/users/values.html b/users/templates/users/values.html new file mode 100644 index 0000000..31c3b88 --- /dev/null +++ b/users/templates/users/values.html @@ -0,0 +1,22 @@ +{% extends "news/base.html" %} +{% load static %} +{% load crispy_forms_tags %} +{% block content %} +

Before we start, let's get some info:

+
+
+
+

First, take a political compass test at:

+ https://spekr.org +
+
+
+ {% csrf_token %} + What were your results? + {{ form|crispy }} + +
+
+
+
+{% endblock content %} diff --git a/users/views.py b/users/views.py index 9562ffe..6d63862 100644 --- a/users/views.py +++ b/users/views.py @@ -4,13 +4,15 @@ from django.contrib.auth import update_session_auth_hash, login, authenticate from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import AdminPasswordChangeForm, PasswordChangeForm, UserCreationForm -from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm +from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, ValuesUpdateForm from social_django.models import UserSocialAuth from .models import Profile def register(request): createProfileIfNotExist(request) + if checkValues(request) is False: + return redirect('values') if request.method == 'POST': form = UserRegisterForm(request.POST) @@ -27,6 +29,8 @@ def register(request): @login_required def profile(request): createProfileIfNotExist(request) + if checkValues(request) is False: + return redirect('values') user = request.user @@ -66,6 +70,7 @@ def profile(request): instance=request.user.profile) passwordForm = PasswordForm(request.user, request.POST) + valuesForm = ValuesUpdateForm(request.POST, instance=request.user.profile) if userForm.is_valid() and profileForm.is_valid(): userForm.save() @@ -77,6 +82,10 @@ def profile(request): update_session_auth_hash(request, passwordForm.user) messages.success(request, 'Your password was successfully updated!') return redirect('profile') + elif valuesForm.is_valid(): + valuesForm.save() + messages.success(request, "Thanks for your input! We'll now be serving articles of exactly differing opinons, enjoy!") + return redirect('profile') else: messages.error(request, 'Please correct the errors below.') @@ -84,11 +93,19 @@ def profile(request): userForm = UserUpdateForm(instance=request.user) profileForm = ProfileUpdateForm(instance=request.user.profile) passwordForm = PasswordForm(request.user) + valuesForm = ValuesUpdateForm(instance=request.user) + + valueX = user.profile.economicScore * 2.5 + 250 + valueY = user.profile.socialScore * -2.5 + 250 + print("VALUEX IS " + str(valueX)) context = { 'userForm': userForm, 'profileForm': profileForm, 'passwordForm': passwordForm, + 'valuesForm': valuesForm, + 'valueX': valueX, + 'valueY': valueY, 'github_login': github_login, 'google_login': google_login, 'facebook_login': facebook_login, @@ -97,9 +114,35 @@ def profile(request): return render(request, 'users/profile.html', context) +@login_required +def values (request): + user = request.user + + if request.method == "POST": + userForm = ValuesUpdateForm(request.POST, instance=request.user.profile) + if userForm.is_valid(): + userForm.save() + profile = request.user.profile + profile.gaveValues = True + profile.save() + messages.success(request, "Thanks for your input! We'll now be serving articles of exactly differing opinons, enjoy!") + return redirect('home') + else: + messages.error(request, 'Please correct the errors below.') + else: + userForm = ValuesUpdateForm(instance=request.user.profile) + + context = { + 'form': userForm + } + + return render(request, 'users/values.html', context) + @login_required def password(request): createProfileIfNotExist(request) + if checkValues(request) is False: + return redirect('values') if request.user.has_usable_password(): PasswordForm = PasswordChangeForm @@ -123,3 +166,10 @@ def password(request): def createProfileIfNotExist (request): if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() < 1: Profile.objects.create(user=request.user).save() + + +def checkValues (request): + if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() is 1: + profile = Profile.objects.filter(user=request.user).first() + return profile.gaveValues + return True