コード例 #1
0
    def test_str(self):
        """Ensure that an Announcement object's string representation is simply its title.
        """
        announcement = Announcement(title='Title')
        announcement.save()

        self.assertEqual(announcement.title, str(announcement))
コード例 #2
0
def project_created(name, authors, description, url):
    """Creates a new Announcement indicating that a new Project was created.

    Args:
        name: the name of the project
        authors: a list of the project's authors
        description: a description of the project
        url: the relative url for the project's individual detail page
    """
    announcement = Announcement(title='New Project!',
                                body=[{
                                    'element': 'h3',
                                    'content': name
                                }, {
                                    'element': 'h6',
                                    'content': ', '.join(authors)
                                }, {
                                    'element': 'p',
                                    'content': description
                                }, {
                                    'element': 'hr'
                                }, {
                                    'element': 'a',
                                    'href': url,
                                    'content': 'Read More'
                                }])
    announcement.save()
コード例 #3
0
ファイル: viewset.py プロジェクト: mehdi1361/backend_aghigh
    def create(self, request, *args, **kwargs):

        # custom parameters
        post_req = self.cleaned_data(request=request)

        # validate data enter
        v = Validator(announcements_schema())
        v.allow_unknown = True
        if not v.validate(post_req):
            return Response({"message": v.errors},
                            status=status.HTTP_400_BAD_REQUEST)

        # add announcement
        announcement = Announcement(
            title=post_req['title'],
            description=post_req['description'],
            date=post_req['date']
            if post_req.get('date', None) else datetime.datetime.now(),
            release_time=post_req['release_time'] if post_req.get(
                'release_time', None) else datetime.datetime.now(),
            has_date=post_req.get('has_date', False),
            image=request.data.get('image')
            if request.data.get('image') else "",
            creator_id=self.request.user.id)
        announcement.save()
        AnnouncementViewSet.last_announcement_date = get_last_announcement_publish_date(
        )
        AnnouncementViewSet.near_next_announcement_date = get_near_next_announcement_publish_date(
        )
        # add files for announcement
        self.save_file(announcement, request)

        # add announcement receiver
        self.add_announcement_receiver(announcement, post_req)
        return Response(status=status.HTTP_201_CREATED)
コード例 #4
0
    def test_list_action_valid_count_less_than_num_in_db(self):
        """Ensure that specifying a valid count does not cause any issues.
        """
        e = Announcement(
            title='Second Announcement',
            body=[{'element': 'hr'}]
        )
        e.save()

        url = reverse('announcement-list')
        response = self.client.get(f'{url}?count=1')
        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assertEqual(1, len(response.data))
コード例 #5
0
 def test_hr_with_valid_element_invalid_prop_invalid(self):
     """Ensure that attempting to validate a list that contains a horizontal rule object that has an 'element'
     property with the value 'hr' and some other arbitrary property raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'hr',
                                     'chandelier': 'barnacle'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #6
0
 def test_a_valid_element_whitespace_char_content_invalid(self):
     """Ensure that attempting to validate a list that contains an anchor object with a valid element property
     value but a content property with a value of a string of length 0 raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'a',
                                     'content': ' \t \f \n'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #7
0
 def test_p_valid_element_len_0_content_invalid(self):
     """Ensure that attempting to validate a list that contains a paragraph object with element property value 'p'
     and a content property value with a length of 0 raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'p',
                                     'content': ''
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #8
0
 def test_img_valid_element_valid_alt_no_other_prop_invalid(self):
     """Ensure that attempting to validate a list that contains an image object with a valid element property
     value of 'img' and a valid alt property value but no other properties raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'img',
                                     'alt': 'AI Club at NC State Photo 1'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #9
0
 def test_h_valid_element_h6_invalid_content_0_len_invalid(self):
     """Ensure that attempting to validate a list that contains a header object with a valid element property
     value h6 and an invalid content property with a string value of length 0 raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'h6',
                                     'content': ''
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #10
0
 def test_a_valid_element_arbitrary_prop_invalid(self):
     """Ensure that attempting to validate a list that contains an anchor object with a valid element property
     value but an arbitrary property with an arbitrary value raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'a',
                                     'arbitraryprop': 'arbitraryvalue'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #11
0
def event_created(event_type, start):
    """Creates a new Announcement indicating that a new Event was created.

    Args:
        event_type: the type of Event that was created
        start: the start date and time of the event
    """
    announcement = Announcement(title='New Event!',
                                body=[{
                                    'element':
                                    'p',
                                    'content':
                                    render_to_string('event/created_body.txt',
                                                     context={
                                                         'type': event_type,
                                                         'start': start
                                                     })
                                }])
    announcement.save()
コード例 #12
0
 def test_h_valid_element_h6_invalid_content_whitespace_char_invalid(self):
     """Ensure that attempting to validate a list that contains a header object with a valid element property
     value h6 and an invalid content property with a string value containing only whitespace characters raises a
     validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'h6',
                                     'content': ' \t \f \n'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #13
0
 def test_p_valid_element_whitespace_content_invalid(self):
     """Ensure that attempting to validate a list that contains a paragraph object with element property value 'p'
     and a content property value with only whitespace characters (space, '\t', '\f', '\n') raises a validation
     error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'p',
                                     'content': "  \t \f \n"
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #14
0
 def test_h_valid_element_h5_valid_content_arbitrary_prop_invalid(self):
     """Ensure that attempting to validate a list that contains a header object with a valid element property
     value h5 and a valid content property but with another arbitrary property raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'h5',
                                     'content':
                                     'AI at NC State Upcoming Event',
                                     'arbitrary': 'arbitraryvalue'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #15
0
 def test_h_valid_element_h6_valid_content_valid(self):
     """Ensure that attempting to validate a list that contains a header object with a valid element property
     value h6 and a valid content property does not raise a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element':
                                     'h6',
                                     'content':
                                     'AI at NC State Upcoming Event'
                                 }])
     self.assertNotRaises(ValidationError, announcement.full_clean)
コード例 #16
0
 def test_img_valid_element_valid_alt_arbitrary_prop_invalid(self):
     """Ensure that attempting to validate a list that contains an image object with a valid element property
     value of 'img', a valid alt property value, and an invalid url property value raises a validation error
     """
     announcement = Announcement(title="Title",
                                 body=[{
                                     'element': 'img',
                                     'alt': 'AI at NC State Photo 1',
                                     'url': 'therevin',
                                     'arbitrary': 'arbitraryvalue'
                                 }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #17
0
    def test_valid_body_valid_object(self):
        """Ensure that attempting to validate a list that contains a single valid object does not raise a validation
        error
        """
        announcement = Announcement(title='Title',
                                    body=[{
                                        'element':
                                        'h2',
                                        'content':
                                        'The quick fox jumps over the dog.'
                                    }])

        self.assertNotRaises(ValidationError, announcement.full_clean)
コード例 #18
0
 def test_img_valid_element_valid_url_no_other_prop_invalid(self):
     """Ensure that attempting to validate a list that contains an image object with a valid element property
     value of 'img' and a valid url property value but no other properties raises a validation error
     """
     announcement = Announcement(
         title="Title",
         body=[{
             'element':
             'img',
             'url':
             'https://github.com/vyathakavilocana/AIatNCStateWebsite'
         }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #19
0
 def test_p_valid_element_valid_content_valid(self):
     """Ensure that attempting to validate a list that contains a paragraph object with element property value 'p'
     and a content property value with a length greater than 1/not only comprised of whitespace characters is valid
     """
     announcement = Announcement(
         title="Title",
         body=[{
             'element':
             'p',
             'content':
             "AI Club is inviting you to the new workshop held on March 6."
         }])
     self.assertNotRaises(ValidationError, announcement.full_clean)
コード例 #20
0
 def test_p_valid_element_valid_content_arbitrary_prop_invalid(self):
     """Ensure that attempting to validate a list that contains a paragraph object with a valid element property
     value 'p' and a valid content property value but with another arbitrary property/value pair raises a
     validation error
     """
     announcement = Announcement(
         title="Title",
         body=[{
             'element': 'p',
             'content':
             'AI Club is inviting you to the new workshop held on March 6.',
             'arbitrary': 'arbitrary property and value'
         }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #21
0
 def test_valid_body_valid_objects(self):
     """Ensure that attempting to validate a list that contains multiple valid objects does not raise a validation
     error
     """
     announcement = Announcement(
         title='Title',
         body=[{
             'element': 'hr'
         }, {
             'element': 'a',
             'href':
             'https://github.com/vyathakavilocana/AIatNCStateWebsite',
             'content': 'The quick fox jumps over the dog.'
         }])
     self.assertNotRaises(ValidationError, announcement.full_clean)
コード例 #22
0
 def test_img_valid_element_valid_url_valid_alt_arbitrary_prop_invalid(
         self):
     """Ensure that attempting to validate a list that contains an image object with a valid element property
     value of 'img', a valid alt property value, and a valid url property value but has some other arbitrary
     property with arbitrary value raises a validation error
     """
     announcement = Announcement(
         title="Title",
         body=[{
             'element': 'img',
             'alt': 'AI Club at NC State Photo 1',
             'url':
             'https://github.com/vyathakavilocana/AIatNCStateWebsite',
             'copacabana': 'Barry Manlow'
         }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #23
0
 def test_img_valid_element_whitespace_char_alt_valid_url_invalid(self):
     """Ensure that attempting to validate a list that contains an image object with a valid element property
     value of 'img', an alt property value that is a string consisting of only whitespace characters, and a valid
     url property value raises a validation error
     """
     announcement = Announcement(
         title="Title",
         body=[{
             'element':
             'img',
             'alt':
             ' \t \f \n',
             'url':
             'https://github.com/vyathakavilocana/AIatNCStateWebsite'
         }])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #24
0
    def test_invalid_body_valid_and_invalid_object(self):
        """Ensure that attempting to validate a list that contains a valid object and invalid object raises a
        validation error
        """
        announcement = Announcement(
            title='Title',
            body=[{
                'element': 'p',
                'content': 'paragraph content'
            }, {
                'element':
                'img',
                'alt':
                0,
                'url':
                'https://github.com/vyathakavilocana/AIatNCStateWebsite'
            }])

        self.assertRaises(ValidationError, announcement.full_clean)
コード例 #25
0
 def setUpTestData(cls):
     """Set up the test data for the test case once when the test case class is being prepared to run.
     """
     cls.announcement = Announcement(
         title='Announcement Title',
         body=[
             {
                 'element': 'h3',
                 'content': 'Header Text'
             },
             {
                 'element': 'hr'
             },
             {
                 'element': 'p',
                 'content': 'Paragraph text content'
             }
         ]
     )
     cls.announcement.save()
コード例 #26
0
def announcements(request):
    if request.POST:
        form = request.POST
        body = form['Body']
        from_number = form['From']
        account_sid = form['AccountSid']
        if from_number in APPROVED_NUMBERS and \
                account_sid == os.environ['TWILIO_SID']:
            a = Announcement()
            a.message = body
            a.time = datetime.datetime.now()
            a.save()
            device = FCMDevice.objects.all()
            device.send_message(message=a.message)
        return success_data_jsonify({})
    else:
        all_announcements = Announcement.objects.all()
        announcement_array = []
        for announcement in all_announcements:
            announcement_array.append(announcement.dictionary_representation())
        return success_data_jsonify(announcement_array)
コード例 #27
0
 def test_hr_with_valid_element_valid(self):
     """Ensure that attempting to validate a list that contains a horizontal rule object that has an 'element'
     property with the value 'hr' and no other properties does not raise a validation error
     """
     announcement = Announcement(title="Title", body=[{'element': 'hr'}])
     self.assertNotRaises(ValidationError, announcement.full_clean)
コード例 #28
0
    def test_invalid_body_empty_list(self):
        """Ensure that attempting to validate an empty list raises a validation error
        """
        announcement = Announcement(title='Title', body=[])

        self.assertRaises(ValidationError, announcement.full_clean)
コード例 #29
0
 def test_a_valid_element_no_other_prop_invalid(self):
     """Ensure that attempting to validate a list that contains an anchor object with a valid element property
     value a and no other property raises a validation error
     """
     announcement = Announcement(title="Title", body=[{'element': 'a'}])
     self.assertRaises(ValidationError, announcement.full_clean)
コード例 #30
0
 def test_invalid_body_object_no_props(self):
     """Ensure that attempting to validate a list that contains an object with no properties raises a validation
     error
     """
     announcement = Announcement(title='Title', body=[{}])
     self.assertRaises(ValidationError, announcement.full_clean)